X-Git-Url: http://nitlanguage.org diff --git a/lib/standard/collection/array.nit b/lib/standard/collection/array.nit index 1fb8581..3cc0b57 100644 --- a/lib/standard/collection/array.nit +++ b/lib/standard/collection/array.nit @@ -13,7 +13,9 @@ # This module introduces the standard array structure. # It also implements two other abstract collections : ArrayMap and ArraySet -module array +module array is + no_warning "useless-type-test" # to avoid warning with nitc while compiling with c_src +end import abstract_collection @@ -130,7 +132,20 @@ abstract class AbstractArrayRead[E] end end - redef fun iterator: ArrayIterator[E] do return new ArrayIterator[E](self) + redef fun iterator: ArrayIterator[E] do + var res = _free_iterator + if res == null then return new ArrayIterator[E](self) + res._index = 0 + _free_iterator = null + return res + end + + # An old iterator, free to reuse. + # Once an iterator is `finish`, it become reusable. + # Since some arrays are iterated a lot, this avoid most of the + # continuous allocation/garbage-collection of the needed iterators. + private var free_iterator: nullable ArrayIterator[E] = null + redef fun reverse_iterator do return new ArrayReverseIterator[E](self) end @@ -477,6 +492,8 @@ private class ArrayIterator[E] redef var index = 0 var array: AbstractArrayRead[E] + + redef fun finish do _array._free_iterator = self end private class ArrayReverseIterator[E] @@ -618,8 +635,8 @@ class ArrayMap[K, E] end end - redef var keys: RemovableCollection[K] = new ArrayMapKeys[K, E](self) - redef var values: RemovableCollection[E] = new ArrayMapValues[K, E](self) + redef var keys: RemovableCollection[K] = new ArrayMapKeys[K, E](self) is lazy + redef var values: RemovableCollection[E] = new ArrayMapValues[K, E](self) is lazy # O(1) redef fun length do return _items.length