Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions app/lib/search/mem_index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
Expand Down Expand Up @@ -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);
Expand Down
67 changes: 30 additions & 37 deletions app/lib/search/token_index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class TokenIndex<K> {

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;
Expand All @@ -116,7 +116,7 @@ class TokenIndex<K> {
_scorePool._release(s);
}
}
score ??= _scorePool._acquire(0.0);
score ??= _scorePool._acquire();
final r = fn(score);
_scorePool._release(score);
return r;
Expand Down Expand Up @@ -151,64 +151,57 @@ extension StringTokenIndexExt on TokenIndex<String> {
}
}

abstract class _AllocationPool<T> {
final _pool = <T>[];

T _acquire();

void _release(T item) {
_pool.add(item);
}

R withPoolItem<R>({
required R Function(T array) fn,
}) {
final item = _acquire();
final r = fn(item);
_release(item);
return r;
}
}

/// A reusable pool for [IndexedScore] instances to spare some memory allocation.
class ScorePool<K> {
class ScorePool<K> extends _AllocationPool<IndexedScore<K>> {
final List<K> _keys;
final _pool = <IndexedScore<K>>[];

ScorePool(this._keys);

IndexedScore<K> _acquire(double value) {
@override
IndexedScore<K> _acquire() {
late IndexedScore<K> score;
if (_pool.isNotEmpty) {
score = _pool.removeLast();
score._values.setAll(0, Iterable.generate(score.length, (_) => value));
score._values.setAll(0, Iterable.generate(score.length, (_) => 0.0));
} else {
score = IndexedScore<K>(_keys, value);
score = IndexedScore<K>(_keys);
}
return score;
}

void _release(IndexedScore<K> score) {
_pool.add(score);
}

R withScore<R>({
required double value,
required R Function(IndexedScore<K> score) fn,
}) {
final score = _acquire(value);
final r = fn(score);
_release(score);
return r;
}
}

/// A reusable pool for [BitArray] instances to spare some memory allocation.
class BitArrayPool<K> {
class BitArrayPool extends _AllocationPool<BitArray> {
final int _length;
final _pool = <BitArray>[];

BitArrayPool(this._length);

BitArray _acquireAllSet() {
@override
BitArray _acquire() {
final array = _pool.isNotEmpty ? _pool.removeLast() : BitArray(_length);
// Sets all the bits to 1.
array.setRange(0, _length);
return array;
}

void _release(BitArray array) {
_pool.add(array);
}

R withBitArrayAllSet<R>({
required R Function(BitArray array) fn,
}) {
final array = _acquireAllSet();
final r = fn(array);
_release(array);
return r;
}
}

/// Mutable score list that can accessed via integer index.
Expand Down