lib/standard: optimize `Array::add_all`
authorAlexis Laferrière <alexis.laf@xymus.net>
Tue, 23 Dec 2014 03:35:51 +0000 (22:35 -0500)
committerAlexis Laferrière <alexis.laf@xymus.net>
Sat, 27 Dec 2014 22:57:16 +0000 (17:57 -0500)
The number of instructions to run `bench_add_all` was at 42 mIr, and
after optimization at 17 mIr.

Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/standard/collection/array.nit

index 4b73471..e70acd2 100644 (file)
@@ -281,6 +281,32 @@ class Array[E]
                _items[l] = item
        end
 
+       # Slight optimization for arrays
+       redef fun add_all(items)
+       do
+               var l = _length
+               var nl = l + items.length
+               if _capacity < nl then
+                       enlarge nl
+               end
+
+               if items isa Array[E] then
+                       var k = 0
+                       while l < nl do
+                               _items[l] = items._items[k]
+                               l += 1
+                               k += 1
+                       end
+               else
+                       for item in items do
+                               _items[l] = item
+                               l += 1
+                       end
+               end
+
+               _length = nl
+       end
+
        redef fun enlarge(cap)
        do
                var c = _capacity