diff --git a/app/lib/search/mem_index.dart b/app/lib/search/mem_index.dart index a852e96545..2acd5d3527 100644 --- a/app/lib/search/mem_index.dart +++ b/app/lib/search/mem_index.dart @@ -141,9 +141,8 @@ class InMemoryPackageIndex { if ((query.offset ?? 0) >= _documents.length) { return PackageSearchResult.empty(); } - return _bitArrayPool.withBitArrayAllSet(fn: (array) { - return _scorePool.withScore( - value: 0.0, + return _bitArrayPool.withPoolItem(fn: (array) { + return _scorePool.withPoolItem( fn: (score) { return _search(query, array, score); }, @@ -400,8 +399,7 @@ class InMemoryPackageIndex { final matchApi = textMatchExtent.shouldMatchApi(); for (final word in words) { - _scorePool.withScore( - value: 0.0, + _scorePool.withPoolItem( fn: (wordScore) { _packageNameIndex.searchWord(word, score: wordScore, filterOnNonZeros: packageScores); diff --git a/app/lib/search/token_index.dart b/app/lib/search/token_index.dart index 49267c19e4..9dcdb3d045 100644 --- a/app/lib/search/token_index.dart +++ b/app/lib/search/token_index.dart @@ -107,7 +107,7 @@ class TokenIndex { weight = math.pow(weight, 1 / words.length).toDouble(); for (final w in words) { - final s = _scorePool._acquire(0.0); + final s = _scorePool._acquire(); searchAndAccumulate(w, score: s, weight: weight); if (score == null) { score = s; @@ -116,7 +116,7 @@ class TokenIndex { _scorePool._release(s); } } - score ??= _scorePool._acquire(0.0); + score ??= _scorePool._acquire(); final r = fn(score); _scorePool._release(score); return r; @@ -151,64 +151,62 @@ extension StringTokenIndexExt on TokenIndex { } } -/// A reusable pool for [IndexedScore] instances to spare some memory allocation. -class ScorePool { - final List _keys; - final _pool = >[]; +abstract class _AllocationPool { + final _pool = []; + + /// Creates a ready-to-use item for the pool. + final T Function() _allocate; - ScorePool(this._keys); + /// Resets a previously used item to its initial state. + final void Function(T) _reset; - IndexedScore _acquire(double value) { - late IndexedScore score; + _AllocationPool(this._allocate, this._reset); + + T _acquire() { + final T t; if (_pool.isNotEmpty) { - score = _pool.removeLast(); - score._values.setAll(0, Iterable.generate(score.length, (_) => value)); + t = _pool.removeLast(); + _reset(t); } else { - score = IndexedScore(_keys, value); + t = _allocate(); } - return score; + return t; } - void _release(IndexedScore score) { - _pool.add(score); + void _release(T item) { + _pool.add(item); } - R withScore({ - required double value, - required R Function(IndexedScore score) fn, + R withPoolItem({ + required R Function(T array) fn, }) { - final score = _acquire(value); - final r = fn(score); - _release(score); + final item = _acquire(); + final r = fn(item); + _release(item); return r; } } -/// A reusable pool for [BitArray] instances to spare some memory allocation. -class BitArrayPool { - final int _length; - final _pool = []; - - BitArrayPool(this._length); - - BitArray _acquireAllSet() { - final array = _pool.isNotEmpty ? _pool.removeLast() : BitArray(_length); - array.setRange(0, _length); - return array; - } - - void _release(BitArray array) { - _pool.add(array); - } +/// A reusable pool for [IndexedScore] instances to spare some memory allocation. +class ScorePool extends _AllocationPool> { + ScorePool(List keys) + : super( + () => IndexedScore(keys), + // sets all values to 0.0 + (score) => score._values + .setAll(0, Iterable.generate(score.length, (_) => 0.0)), + ); +} - R withBitArrayAllSet({ - required R Function(BitArray array) fn, - }) { - final array = _acquireAllSet(); - final r = fn(array); - _release(array); - return r; - } +/// A reusable pool for [BitArray] instances to spare some memory allocation. +class BitArrayPool extends _AllocationPool { + BitArrayPool(int length) + : super( + // sets all bits to 1 + () => BitArray(length)..setRange(0, length), + // sets all bits to 1 + (array) => array.setRange(0, length), + ); } /// Mutable score list that can accessed via integer index. diff --git a/app/lib/third_party/bit_array/bit_array.dart b/app/lib/third_party/bit_array/bit_array.dart index 1bd12c04e4..3934f8081b 100644 --- a/app/lib/third_party/bit_array/bit_array.dart +++ b/app/lib/third_party/bit_array/bit_array.dart @@ -212,7 +212,7 @@ class BitArray extends BitSet { } } - /// Sets the bits from [start] (inclusive) up to [end] (exclusive) to false. + /// Sets the bits from [start] (inclusive) up to [end] (exclusive) to true. void setRange(int start, int end) { assert(start >= 0 && start < _length); assert(start <= end && end <= _length);