Skip to content

Commit da83304

Browse files
authored
Implement bitarray pool in package search index. (#8797)
1 parent 18b1763 commit da83304

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

app/lib/search/mem_index.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class InMemoryPackageIndex {
2929
late final TokenIndex<String> _descrIndex;
3030
late final TokenIndex<String> _readmeIndex;
3131
late final TokenIndex<IndexedApiDocPage> _apiSymbolIndex;
32+
late final _bitArrayPool = BitArrayPool(_documents.length);
3233
late final _scorePool = ScorePool(_packageNameIndex._packageNames);
3334

3435
/// Maps the tag strings to a list of document index values using bit arrays.
@@ -140,22 +141,21 @@ class InMemoryPackageIndex {
140141
if ((query.offset ?? 0) >= _documents.length) {
141142
return PackageSearchResult.empty();
142143
}
143-
return _scorePool.withScore(
144-
value: 0.0,
145-
fn: (score) {
146-
return _search(query, score);
147-
},
148-
);
144+
return _bitArrayPool.withBitArrayAllSet(fn: (array) {
145+
return _scorePool.withScore(
146+
value: 0.0,
147+
fn: (score) {
148+
return _search(query, array, score);
149+
},
150+
);
151+
});
149152
}
150153

151154
PackageSearchResult _search(
152155
ServiceSearchQuery query,
156+
BitArray packages,
153157
IndexedScore<String> packageScores,
154158
) {
155-
// TODO: implement pooling of this object similarly to [ScorePool].
156-
final packages = BitArray(_documents.length)
157-
..setRange(0, _documents.length);
158-
159159
// filter on tags
160160
final combinedTagsPredicate =
161161
query.tagsPredicate.appendPredicate(query.parsedQuery.tagsPredicate);

app/lib/search/token_index.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:math' as math;
66

77
import 'package:meta/meta.dart';
8+
import 'package:pub_dev/third_party/bit_array/bit_array.dart';
89

910
import 'text_utils.dart';
1011

@@ -183,6 +184,33 @@ class ScorePool<K> {
183184
}
184185
}
185186

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+
}
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+
}
212+
}
213+
186214
/// Mutable score list that can accessed via integer index.
187215
class IndexedScore<K> {
188216
final List<K> _keys;

0 commit comments

Comments
 (0)