@@ -154,7 +154,24 @@ extension StringTokenIndexExt on TokenIndex<String> {
154154abstract class _AllocationPool <T > {
155155 final _pool = < T > [];
156156
157- T _acquire ();
157+ /// Creates a ready-to-use item for the pool.
158+ final T Function () _allocate;
159+
160+ /// Resets a previously used item to its initial state.
161+ final void Function (T ) _reset;
162+
163+ _AllocationPool (this ._allocate, this ._reset);
164+
165+ T _acquire () {
166+ final T t;
167+ if (_pool.isNotEmpty) {
168+ t = _pool.removeLast ();
169+ _reset (t);
170+ } else {
171+ t = _allocate ();
172+ }
173+ return t;
174+ }
158175
159176 void _release (T item) {
160177 _pool.add (item);
@@ -172,36 +189,21 @@ abstract class _AllocationPool<T> {
172189
173190/// A reusable pool for [IndexedScore] instances to spare some memory allocation.
174191class ScorePool <K > extends _AllocationPool <IndexedScore <K >> {
175- final List <K > _keys;
176-
177- ScorePool (this ._keys);
178-
179- @override
180- IndexedScore <K > _acquire () {
181- late IndexedScore <K > score;
182- if (_pool.isNotEmpty) {
183- score = _pool.removeLast ();
184- score._values.setAll (0 , Iterable .generate (score.length, (_) => 0.0 ));
185- } else {
186- score = IndexedScore <K >(_keys);
187- }
188- return score;
189- }
192+ ScorePool (List <K > keys)
193+ : super (
194+ () => IndexedScore (keys),
195+ (score) => score._values
196+ .setAll (0 , Iterable .generate (score.length, (_) => 0.0 )),
197+ );
190198}
191199
192200/// A reusable pool for [BitArray] instances to spare some memory allocation.
193201class BitArrayPool extends _AllocationPool <BitArray > {
194- final int _length;
195-
196- BitArrayPool (this ._length);
197-
198- @override
199- BitArray _acquire () {
200- final array = _pool.isNotEmpty ? _pool.removeLast () : BitArray (_length);
201- // Sets all the bits to 1.
202- array.setRange (0 , _length);
203- return array;
204- }
202+ BitArrayPool (int length)
203+ : super (
204+ () => BitArray (length)..setRange (0 , length),
205+ (array) => array.setRange (0 , length),
206+ );
205207}
206208
207209/// Mutable score list that can accessed via integer index.
0 commit comments