lib/core: Optimized generation of Boxes in to_s_with_copy
authorLucas Bajolet <r4pass@hotmail.com>
Fri, 25 Sep 2015 18:25:30 +0000 (14:25 -0400)
committerLucas Bajolet <r4pass@hotmail.com>
Mon, 28 Sep 2015 14:00:26 +0000 (10:00 -0400)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

lib/core/text/abstract_text.nit
lib/core/text/flat.nit

index 1fcb0d9..01d0eab 100644 (file)
@@ -975,9 +975,6 @@ abstract class FlatText
        # if set before using it.
        private var items: NativeString is noinit
 
-       # Real items, used as cache for to_cstring is called
-       private var real_items: nullable NativeString = null
-
        # Returns a char* starting at position `first_byte`
        #
        # WARNING: If you choose to use this service, be careful of the following.
index 4d2ff48..5bc76fc 100644 (file)
@@ -200,6 +200,14 @@ class FlatString
                return _items.utf8_length(_first_byte, _last_byte)
        end
 
+       redef var to_cstring is lazy do
+               var blen = _bytelen
+               var new_items = new NativeString(blen + 1)
+               _items.copy_to(new_items, blen, _first_byte, 0)
+               new_items[blen] = 0u8
+               return new_items
+       end
+
        redef fun reversed
        do
                var b = new FlatBuffer.with_capacity(_bytelen + 1)
@@ -304,16 +312,6 @@ class FlatString
                _bytepos = from
        end
 
-       redef fun to_cstring do
-               if real_items != null then return real_items.as(not null)
-               var blen = _bytelen
-               var new_items = new NativeString(blen + 1)
-               _items.copy_to(new_items, blen, _first_byte, 0)
-               new_items[blen] = 0u8
-               real_items = new_items
-               return new_items
-       end
-
        redef fun ==(other)
        do
                if not other isa FlatString then return super
@@ -582,6 +580,9 @@ class FlatBuffer
 
        private var capacity = 0
 
+       # Real items, used as cache for when to_cstring is called
+       private var real_items: NativeString is noinit
+
        redef fun fast_cstring do return _items.fast_cstring(0)
 
        redef fun substrings do return new FlatSubstringsIter(self)
@@ -702,7 +703,7 @@ class FlatBuffer
                        real_items = new_native
                        is_dirty = false
                end
-               return real_items.as(not null)
+               return real_items
        end
 
        # Create a new empty string.
@@ -1002,7 +1003,7 @@ redef class NativeString
                copy_to(new_self, length, 0, 0)
                var str = new FlatString.with_infos(new_self, length, 0, length - 1)
                new_self[length] = 0u8
-               str.real_items = new_self
+               str.to_cstring = new_self
                return str
        end