Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
88 changes: 43 additions & 45 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,62 @@ extension StringTokenIndexExt on TokenIndex<String> {
}
}

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

/// 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<K> _acquire(double value) {
late IndexedScore<K> 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<K>(_keys, value);
t = _allocate();
}
return score;
return t;
}

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

R withScore<R>({
required double value,
required R Function(IndexedScore<K> score) fn,
R withPoolItem<R>({
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<K> {
final int _length;
final _pool = <BitArray>[];

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<K> extends _AllocationPool<IndexedScore<K>> {
ScorePool(List<K> keys)
: super(
() => IndexedScore(keys),
// sets all values to 0.0
(score) => score._values
.setAll(0, Iterable.generate(score.length, (_) => 0.0)),
);
}

R withBitArrayAllSet<R>({
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<BitArray> {
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.
Expand Down
2 changes: 1 addition & 1 deletion app/lib/third_party/bit_array/bit_array.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down