Merge: Text optimization
authorJean Privat <jean@pryen.org>
Sat, 29 Aug 2015 16:12:03 +0000 (12:12 -0400)
committerJean Privat <jean@pryen.org>
Sat, 29 Aug 2015 16:12:03 +0000 (12:12 -0400)
This PR introduces some more bookkeeping to Texts variants, some more work is to be expected at compilation-time for the generation of FlatStrings with all the information, but this should improve execution speeds in clients.

`./chain_concat -m string --loops 1000000 --strlen 15 --ccts 20`

* Before:
4.42 4.48 4.51 4.5025 "20"
* After:
2.40 2.36 2.38 2.37 "20"

`time ./bin/nitc --semi-global src/nitc.nit -o bin/nitc` best of 10:

* Before
~~~
real  0m6.075s
user  0m4.624s
sys   0m0.826s
~~~

* After
~~~
real  0m6.172s
user  0m4.751s
sys   0m0.870s
~~~

Valgrind:

* Before; GIr: 14,768,723,784
* After; GIr: 14,775,119,149

Pull-Request: #1663
Reviewed-by: Jean Privat <jean@pryen.org>
Reviewed-by: Alexis Laferrière <alexis.laf@xymus.net>

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

@@@ -231,6 -231,15 +231,6 @@@ abstract class Tex
        #     assert "abcd".has_suffix("bcd")        ==  true
        fun has_suffix(suffix: String): Bool do return has_substring(suffix, length - suffix.length)
  
 -      # Returns a copy of `self` minus all occurences of `c`
 -      #
 -      #     assert "__init__".remove_all('_') == "init"
 -      fun remove_all(c: Char): String do
 -              var b = new Buffer
 -              for i in chars do if i != c then b.add i
 -              return b.to_s
 -      end
 -
        # Returns `self` as the corresponding integer
        #
        #     assert "123".to_i        == 123
@@@ -1808,6 -1817,12 +1808,12 @@@ redef class NativeStrin
  
        # Returns `self` as a String of `length`.
        fun to_s_with_length(length: Int): String is abstract
+       # Returns `self` as a String with `bytelen` and `length` set
+       #
+       # SEE: `abstract_text::Text` for more infos on the difference
+       # between `Text::bytelen` and `Text::length`
+       fun to_s_full(bytelen, unilen: Int): String is abstract
  end
  
  redef class NativeArray[E]
diff --combined lib/core/text/flat.nit
@@@ -84,43 -84,6 +84,43 @@@ redef class FlatTex
                return ns_i
        end
  
 +      private fun byte_to_char_index(index: Int): Int do
 +              var ln = bytelen
 +              assert index >= 0
 +              assert index < bytelen
 +
 +              # Find best insertion point
 +              var delta_begin = index
 +              var delta_end = (ln - 1) - index
 +              var delta_cache = (bytepos - index).abs
 +              var min = delta_begin
 +              var its = items
 +
 +              if delta_cache < min then min = delta_cache
 +              if delta_end < min then min = delta_end
 +
 +              var ns_i: Int
 +              var my_i: Int
 +
 +              if min == delta_begin then
 +                      ns_i = first_byte
 +                      my_i = 0
 +              else if min == delta_cache then
 +                      ns_i = bytepos
 +                      my_i = position
 +              else
 +                      ns_i = its.find_beginning_of_char_at(last_byte)
 +                      my_i = length - 1
 +              end
 +
 +              my_i = its.byte_to_char_index_cached(index, my_i, ns_i)
 +
 +              position = my_i
 +              bytepos = index
 +
 +              return my_i
 +      end
 +
        redef fun [](index) do return items.char_at(char_to_byte_index(index))
  end
  
@@@ -626,7 -589,7 +626,7 @@@ class FlatBuffe
        do
                written = true
                if bytelen == 0 then items = new NativeString(1)
-               return new FlatString.with_infos(items, bytelen, 0, bytelen - 1)
+               return new FlatString.full(items, bytelen, 0, bytelen - 1, length)
        end
  
        redef fun to_cstring
  
        redef fun times(repeats)
        do
-               var x = new FlatString.with_infos(items, bytelen, 0, bytelen - 1)
+               var x = new FlatString.full(items, bytelen, 0, bytelen - 1, length)
                for i in [1 .. repeats[ do
                        append(x)
                end
@@@ -922,6 -885,10 +922,10 @@@ redef class NativeStrin
                return str
        end
  
+       redef fun to_s_full(bytelen, unilen) do
+               return new FlatString.full(self, bytelen, 0, bytelen - 1, unilen)
+       end
        # Returns `self` as a new String.
        redef fun to_s_with_copy: FlatString
        do