Skip to content

Commit e276217

Browse files
authored
Shared code for ScorePool and BitArrayPool. (#8798)
1 parent da83304 commit e276217

File tree

3 files changed

+47
-51
lines changed

3 files changed

+47
-51
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: 43 additions & 45 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,62 @@ extension StringTokenIndexExt on TokenIndex<String> {
151151
}
152152
}
153153

154-
/// A reusable pool for [IndexedScore] instances to spare some memory allocation.
155-
class ScorePool<K> {
156-
final List<K> _keys;
157-
final _pool = <IndexedScore<K>>[];
154+
abstract class _AllocationPool<T> {
155+
final _pool = <T>[];
156+
157+
/// Creates a ready-to-use item for the pool.
158+
final T Function() _allocate;
158159

159-
ScorePool(this._keys);
160+
/// Resets a previously used item to its initial state.
161+
final void Function(T) _reset;
160162

161-
IndexedScore<K> _acquire(double value) {
162-
late IndexedScore<K> score;
163+
_AllocationPool(this._allocate, this._reset);
164+
165+
T _acquire() {
166+
final T t;
163167
if (_pool.isNotEmpty) {
164-
score = _pool.removeLast();
165-
score._values.setAll(0, Iterable.generate(score.length, (_) => value));
168+
t = _pool.removeLast();
169+
_reset(t);
166170
} else {
167-
score = IndexedScore<K>(_keys, value);
171+
t = _allocate();
168172
}
169-
return score;
173+
return t;
170174
}
171175

172-
void _release(IndexedScore<K> score) {
173-
_pool.add(score);
176+
void _release(T item) {
177+
_pool.add(item);
174178
}
175179

176-
R withScore<R>({
177-
required double value,
178-
required R Function(IndexedScore<K> score) fn,
180+
R withPoolItem<R>({
181+
required R Function(T array) fn,
179182
}) {
180-
final score = _acquire(value);
181-
final r = fn(score);
182-
_release(score);
183+
final item = _acquire();
184+
final r = fn(item);
185+
_release(item);
183186
return r;
184187
}
185188
}
186189

187-
/// A reusable pool for [BitArray] instances to spare some memory allocation.
188-
class BitArrayPool<K> {
189-
final int _length;
190-
final _pool = <BitArray>[];
191-
192-
BitArrayPool(this._length);
193-
194-
BitArray _acquireAllSet() {
195-
final array = _pool.isNotEmpty ? _pool.removeLast() : BitArray(_length);
196-
array.setRange(0, _length);
197-
return array;
198-
}
199-
200-
void _release(BitArray array) {
201-
_pool.add(array);
202-
}
190+
/// A reusable pool for [IndexedScore] instances to spare some memory allocation.
191+
class ScorePool<K> extends _AllocationPool<IndexedScore<K>> {
192+
ScorePool(List<K> keys)
193+
: super(
194+
() => IndexedScore(keys),
195+
// sets all values to 0.0
196+
(score) => score._values
197+
.setAll(0, Iterable.generate(score.length, (_) => 0.0)),
198+
);
199+
}
203200

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-
}
201+
/// A reusable pool for [BitArray] instances to spare some memory allocation.
202+
class BitArrayPool extends _AllocationPool<BitArray> {
203+
BitArrayPool(int length)
204+
: super(
205+
// sets all bits to 1
206+
() => BitArray(length)..setRange(0, length),
207+
// sets all bits to 1
208+
(array) => array.setRange(0, length),
209+
);
212210
}
213211

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

app/lib/third_party/bit_array/bit_array.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ class BitArray extends BitSet {
212212
}
213213
}
214214

215-
/// Sets the bits from [start] (inclusive) up to [end] (exclusive) to false.
215+
/// Sets the bits from [start] (inclusive) up to [end] (exclusive) to true.
216216
void setRange(int start, int end) {
217217
assert(start >= 0 && start < _length);
218218
assert(start <= end && end <= _length);

0 commit comments

Comments
 (0)