lib/standard/string: Array.to_s micro-optimization
authorLucas Bajolet <r4pass@hotmail.com>
Tue, 26 Aug 2014 18:27:57 +0000 (14:27 -0400)
committerLucas Bajolet <r4pass@hotmail.com>
Tue, 26 Aug 2014 18:40:16 +0000 (14:40 -0400)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

lib/standard/string.nit

index 3e330f3..1720d40 100644 (file)
@@ -1821,18 +1821,50 @@ redef class Collection[E]
 end
 
 redef class Array[E]
+
        # Fast implementation
        redef fun to_s
        do
-               var s = new FlatBuffer
-               var i = 0
                var l = length
+               if l == 0 then return ""
+               if l == 1 then if self[0] == null then return "" else return self[0].to_s
+               var its = _items
+               var na = new NativeArray[String](l)
+               var i = 0
+               var sl = 0
+               var mypos = 0
                while i < l do
-                       var e = self[i]
-                       if e != null then s.append(e.to_s)
+                       var itsi = its[i]
+                       if itsi == null then
+                               i += 1
+                               continue
+                       end
+                       var tmp = itsi.to_s
+                       sl += tmp.length
+                       na[mypos] = tmp
                        i += 1
+                       mypos += 1
                end
-               return s.to_s
+               var ns = new NativeString(sl + 1)
+               ns[sl] = '\0'
+               i = 0
+               var off = 0
+               while i < mypos do
+                       var tmp = na[i]
+                       var tpl = tmp.length
+                       if tmp isa FlatString then
+                               tmp.items.copy_to(ns, tpl, tmp.index_from, off)
+                               off += tpl
+                       else
+                               for j in tmp.substrings do
+                                       var s = j.as(FlatString)
+                                       s.items.copy_to(ns, tpl, s.index_from, off)
+                                       off += tpl
+                               end
+                       end
+                       i += 1
+               end
+               return ns.to_s_with_length(sl)
        end
 end