Skip to content

Commit ab0c022

Browse files
authored
search: swich Set.contains to index-based lookup. (#8391)
1 parent aa4563c commit ab0c022

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

app/lib/search/mem_index.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class InMemoryPackageIndex {
7272
if (apiDocPages != null) {
7373
for (final page in apiDocPages) {
7474
if (page.symbols != null && page.symbols!.isNotEmpty) {
75-
apiDocPageKeys.add(IndexedApiDocPage(i, doc.package, page));
75+
apiDocPageKeys.add(IndexedApiDocPage(i, page));
7676
apiDocPageValues.add(page.symbols!.join(' '));
7777
}
7878
}
@@ -376,7 +376,8 @@ class InMemoryPackageIndex {
376376
// are multiplied. We can use a package filter that is applied after each
377377
// word to reduce the scope of the later words based on the previous results.
378378
/// However, API docs search should be filtered on the original list.
379-
final packages = packageScores.toKeySet();
379+
final indexedPositiveList = packageScores.toIndexedPositiveList();
380+
380381
for (final word in words) {
381382
if (includeNameMatches && _documentsByName.containsKey(word)) {
382383
nameMatches ??= <String>{};
@@ -406,7 +407,7 @@ class InMemoryPackageIndex {
406407
if (value < 0.01) continue;
407408

408409
final doc = symbolPages.keys[i];
409-
if (!packages.contains(doc.package)) continue;
410+
if (!indexedPositiveList[doc.index]) continue;
410411

411412
// skip if the previously found pages are better than the current one
412413
final pages =
@@ -660,8 +661,7 @@ class IndexedPackageHit {
660661

661662
class IndexedApiDocPage {
662663
final int index;
663-
final String package;
664664
final ApiDocPage page;
665665

666-
IndexedApiDocPage(this.index, this.package, this.page);
666+
IndexedApiDocPage(this.index, this.page);
667667
}

app/lib/search/token_index.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,18 @@ class IndexedScore<K> {
250250
}
251251
}
252252

253-
Set<K> toKeySet() {
254-
final set = <K>{};
253+
/// Returns a list where each index describes whether the position in the
254+
/// current [IndexedScore] is positive. The current instance changes are
255+
/// not reflected in the returned list, it won't change after it was created.
256+
List<bool> toIndexedPositiveList() {
257+
final list = List.filled(_values.length, false);
255258
for (var i = 0; i < _values.length; i++) {
256259
final v = _values[i];
257260
if (v > 0.0) {
258-
set.add(_keys[i]);
261+
list[i] = true;
259262
}
260263
}
261-
return set;
264+
return list;
262265
}
263266

264267
Map<K, double> top(int count, {double? minValue}) {

0 commit comments

Comments
 (0)