From: Alexis Laferrière Date: Tue, 23 Dec 2014 03:35:51 +0000 (-0500) Subject: lib/standard: optimize `Array::add_all` X-Git-Tag: v0.7~2^2~1 X-Git-Url: http://nitlanguage.org lib/standard: optimize `Array::add_all` 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 --- diff --git a/lib/standard/collection/array.nit b/lib/standard/collection/array.nit index 4b73471..e70acd2 100644 --- a/lib/standard/collection/array.nit +++ b/lib/standard/collection/array.nit @@ -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