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
20 changes: 10 additions & 10 deletions app/lib/search/mem_index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class InMemoryPackageIndex {
late final TokenIndex<String> _descrIndex;
late final TokenIndex<String> _readmeIndex;
late final TokenIndex<IndexedApiDocPage> _apiSymbolIndex;
late final _bitArrayPool = BitArrayPool(_documents.length);
late final _scorePool = ScorePool(_packageNameIndex._packageNames);

/// Maps the tag strings to a list of document index values using bit arrays.
Expand Down Expand Up @@ -140,22 +141,21 @@ class InMemoryPackageIndex {
if ((query.offset ?? 0) >= _documents.length) {
return PackageSearchResult.empty();
}
return _scorePool.withScore(
value: 0.0,
fn: (score) {
return _search(query, score);
},
);
return _bitArrayPool.withBitArrayAllSet(fn: (array) {
return _scorePool.withScore(
value: 0.0,
fn: (score) {
return _search(query, array, score);
},
);
});
}

PackageSearchResult _search(
ServiceSearchQuery query,
BitArray packages,
IndexedScore<String> packageScores,
) {
// TODO: implement pooling of this object similarly to [ScorePool].
final packages = BitArray(_documents.length)
..setRange(0, _documents.length);

// filter on tags
final combinedTagsPredicate =
query.tagsPredicate.appendPredicate(query.parsedQuery.tagsPredicate);
Expand Down
28 changes: 28 additions & 0 deletions app/lib/search/token_index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'dart:math' as math;

import 'package:meta/meta.dart';
import 'package:pub_dev/third_party/bit_array/bit_array.dart';

import 'text_utils.dart';

Expand Down Expand Up @@ -183,6 +184,33 @@ class ScorePool<K> {
}
}

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

BitArrayPool(this._length);
Comment on lines +188 to +192
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't matter too much, but I guess it could be generic, sharing implentation with ScorePool (if you pass a constructor).

Suggested change
class BitArrayPool<K> {
final int _length;
final _pool = <BitArray>[];
BitArrayPool(this._length);
class AllocationPool<T> {
final _pool = <T>[];
final T Function() constructor;
AllocationPool(this.constructor);

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've checked but with the ScorePool's variable initialization parameter, it requires a bit of refactoring first. I'll do that in a follow-up PR.


BitArray _acquireAllSet() {
final array = _pool.isNotEmpty ? _pool.removeLast() : BitArray(_length);
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.
class IndexedScore<K> {
final List<K> _keys;
Expand Down