Skip to content

Commit a315f9d

Browse files
committed
Shared code for ScorePool and BitArrayPool.
1 parent da83304 commit a315f9d

File tree

2 files changed

+33
-42
lines changed

2 files changed

+33
-42
lines changed

app/lib/search/mem_index.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,8 @@ class InMemoryPackageIndex {
141141
if ((query.offset ?? 0) >= _documents.length) {
142142
return PackageSearchResult.empty();
143143
}
144-
return _bitArrayPool.withBitArrayAllSet(fn: (array) {
145-
return _scorePool.withScore(
146-
value: 0.0,
144+
return _bitArrayPool.withPoolItem(fn: (array) {
145+
return _scorePool.withPoolItem(
147146
fn: (score) {
148147
return _search(query, array, score);
149148
},
@@ -400,8 +399,7 @@ class InMemoryPackageIndex {
400399
final matchApi = textMatchExtent.shouldMatchApi();
401400

402401
for (final word in words) {
403-
_scorePool.withScore(
404-
value: 0.0,
402+
_scorePool.withPoolItem(
405403
fn: (wordScore) {
406404
_packageNameIndex.searchWord(word,
407405
score: wordScore, filterOnNonZeros: packageScores);

app/lib/search/token_index.dart

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class TokenIndex<K> {
107107

108108
weight = math.pow(weight, 1 / words.length).toDouble();
109109
for (final w in words) {
110-
final s = _scorePool._acquire(0.0);
110+
final s = _scorePool._acquire();
111111
searchAndAccumulate(w, score: s, weight: weight);
112112
if (score == null) {
113113
score = s;
@@ -116,7 +116,7 @@ class TokenIndex<K> {
116116
_scorePool._release(s);
117117
}
118118
}
119-
score ??= _scorePool._acquire(0.0);
119+
score ??= _scorePool._acquire();
120120
final r = fn(score);
121121
_scorePool._release(score);
122122
return r;
@@ -151,64 +151,57 @@ extension StringTokenIndexExt on TokenIndex<String> {
151151
}
152152
}
153153

154+
abstract class _AllocationPool<T> {
155+
final _pool = <T>[];
156+
157+
T _acquire();
158+
159+
void _release(T item) {
160+
_pool.add(item);
161+
}
162+
163+
R withPoolItem<R>({
164+
required R Function(T array) fn,
165+
}) {
166+
final item = _acquire();
167+
final r = fn(item);
168+
_release(item);
169+
return r;
170+
}
171+
}
172+
154173
/// A reusable pool for [IndexedScore] instances to spare some memory allocation.
155-
class ScorePool<K> {
174+
class ScorePool<K> extends _AllocationPool<IndexedScore<K>> {
156175
final List<K> _keys;
157-
final _pool = <IndexedScore<K>>[];
158176

159177
ScorePool(this._keys);
160178

161-
IndexedScore<K> _acquire(double value) {
179+
@override
180+
IndexedScore<K> _acquire() {
162181
late IndexedScore<K> score;
163182
if (_pool.isNotEmpty) {
164183
score = _pool.removeLast();
165-
score._values.setAll(0, Iterable.generate(score.length, (_) => value));
184+
score._values.setAll(0, Iterable.generate(score.length, (_) => 0.0));
166185
} else {
167-
score = IndexedScore<K>(_keys, value);
186+
score = IndexedScore<K>(_keys);
168187
}
169188
return score;
170189
}
171-
172-
void _release(IndexedScore<K> score) {
173-
_pool.add(score);
174-
}
175-
176-
R withScore<R>({
177-
required double value,
178-
required R Function(IndexedScore<K> score) fn,
179-
}) {
180-
final score = _acquire(value);
181-
final r = fn(score);
182-
_release(score);
183-
return r;
184-
}
185190
}
186191

187192
/// A reusable pool for [BitArray] instances to spare some memory allocation.
188-
class BitArrayPool<K> {
193+
class BitArrayPool extends _AllocationPool<BitArray> {
189194
final int _length;
190-
final _pool = <BitArray>[];
191195

192196
BitArrayPool(this._length);
193197

194-
BitArray _acquireAllSet() {
198+
@override
199+
BitArray _acquire() {
195200
final array = _pool.isNotEmpty ? _pool.removeLast() : BitArray(_length);
201+
// Sets all the bits to 1.
196202
array.setRange(0, _length);
197203
return array;
198204
}
199-
200-
void _release(BitArray array) {
201-
_pool.add(array);
202-
}
203-
204-
R withBitArrayAllSet<R>({
205-
required R Function(BitArray array) fn,
206-
}) {
207-
final array = _acquireAllSet();
208-
final r = fn(array);
209-
_release(array);
210-
return r;
211-
}
212205
}
213206

214207
/// Mutable score list that can accessed via integer index.

0 commit comments

Comments
 (0)