Skip to content

Commit 1195830

Browse files
authored
Use ScorePool for dartdoc and SDK search results. (#8321)
1 parent 926d97d commit 1195830

File tree

3 files changed

+58
-40
lines changed

3 files changed

+58
-40
lines changed

app/lib/search/mem_index.dart

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -334,35 +334,36 @@ class InMemoryPackageIndex {
334334
List<List<MapEntry<String, double>>?>.filled(_documents.length, null);
335335
const maxApiPageCount = 2;
336336
if (!checkAborted()) {
337-
final symbolPages = _apiSymbolIndex.searchWords(words, weight: 0.70);
338-
339-
for (var i = 0; i < symbolPages.length; i++) {
340-
final value = symbolPages.getValue(i);
341-
if (value < 0.01) continue;
342-
343-
final doc = symbolPages.keys[i];
344-
if (!packages.contains(doc.package)) continue;
345-
346-
// skip if the previously found pages are better than the current one
347-
final pages = topApiPages[doc.index] ??= <MapEntry<String, double>>[];
348-
if (pages.length >= maxApiPageCount && pages.last.value > value) {
349-
continue;
337+
_apiSymbolIndex.withSearchWords(words, weight: 0.70, (symbolPages) {
338+
for (var i = 0; i < symbolPages.length; i++) {
339+
final value = symbolPages.getValue(i);
340+
if (value < 0.01) continue;
341+
342+
final doc = symbolPages.keys[i];
343+
if (!packages.contains(doc.package)) continue;
344+
345+
// skip if the previously found pages are better than the current one
346+
final pages =
347+
topApiPages[doc.index] ??= <MapEntry<String, double>>[];
348+
if (pages.length >= maxApiPageCount && pages.last.value > value) {
349+
continue;
350+
}
351+
352+
// update the top api packages score
353+
packageScores.setValueMaxOf(doc.index, value);
354+
355+
// add the page and re-sort the current results
356+
pages.add(MapEntry(doc.page.relativePath, value));
357+
if (pages.length > 1) {
358+
pages.sort((a, b) => -a.value.compareTo(b.value));
359+
}
360+
361+
// keep the results limited to the max count
362+
if (pages.length > maxApiPageCount) {
363+
pages.removeLast();
364+
}
350365
}
351-
352-
// update the top api packages score
353-
packageScores.setValueMaxOf(doc.index, value);
354-
355-
// add the page and re-sort the current results
356-
pages.add(MapEntry(doc.page.relativePath, value));
357-
if (pages.length > 1) {
358-
pages.sort((a, b) => -a.value.compareTo(b.value));
359-
}
360-
361-
// keep the results limited to the max count
362-
if (pages.length > maxApiPageCount) {
363-
pages.removeLast();
364-
}
365-
}
366+
});
366367
}
367368

368369
// filter results based on exact phrases

app/lib/search/sdk_mem_index.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ class SdkMemIndex {
137137
final isQualifiedQuery = query.contains(library.split(':').last);
138138

139139
final tokens = _tokensPerLibrary[library]!;
140-
final plainResults = tokens.searchWords(words).top(3, minValue: 0.05);
140+
final plainResults = tokens.withSearchWords(
141+
words, (score) => score.top(3, minValue: 0.05));
141142
if (plainResults.isEmpty) continue;
142143

143144
final libraryWeight = _libraryWeights[library] ?? 1.0;

app/lib/search/token_index.dart

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class TokenIndex<K> {
3434

3535
late final _length = _ids.length;
3636

37+
late final _scorePool = ScorePool(_ids);
38+
3739
TokenIndex(
3840
List<K> ids,
3941
List<String?> values, {
@@ -96,22 +98,27 @@ class TokenIndex<K> {
9698
return tokenMatch;
9799
}
98100

99-
/// Search the index for [words], with a (term-match / document coverage percent)
100-
/// scoring.
101-
IndexedScore<K> searchWords(List<String> words, {double weight = 1.0}) {
101+
/// Search the index for [words], providing the result [IndexedScore] values
102+
/// in the [fn] callback, reusing the score buffers between calls.
103+
R withSearchWords<R>(List<String> words, R Function(IndexedScore<K> score) fn,
104+
{double weight = 1.0}) {
102105
IndexedScore<K>? score;
103106

104107
weight = math.pow(weight, 1 / words.length).toDouble();
105108
for (final w in words) {
106-
final s = IndexedScore(_ids);
109+
final s = _scorePool._acquire(0.0);
107110
searchAndAccumulate(w, score: s, weight: weight);
108111
if (score == null) {
109112
score = s;
110113
} else {
111114
score.multiplyAllFrom(s);
115+
_scorePool._release(s);
112116
}
113117
}
114-
return score ?? IndexedScore(_ids);
118+
score ??= _scorePool._acquire(0.0);
119+
final r = fn(score);
120+
_scorePool._release(score);
121+
return r;
115122
}
116123

117124
/// Searches the index with [word] and stores the results in [score], using
@@ -139,7 +146,7 @@ extension StringTokenIndexExt on TokenIndex<String> {
139146
/// scoring.
140147
@visibleForTesting
141148
Map<String, double> search(String text) {
142-
return searchWords(splitForQuery(text)).toMap();
149+
return withSearchWords(splitForQuery(text), (score) => score.toMap());
143150
}
144151
}
145152

@@ -150,19 +157,28 @@ class ScorePool<K> {
150157

151158
ScorePool(this._keys);
152159

153-
R withScore<R>({
154-
required double value,
155-
required R Function(IndexedScore<K> score) fn,
156-
}) {
160+
IndexedScore<K> _acquire(double value) {
157161
late IndexedScore<K> score;
158162
if (_pool.isNotEmpty) {
159163
score = _pool.removeLast();
160164
score._values.setAll(0, Iterable.generate(score.length, (_) => value));
161165
} else {
162166
score = IndexedScore<K>(_keys, value);
163167
}
164-
final r = fn(score);
168+
return score;
169+
}
170+
171+
void _release(IndexedScore<K> score) {
165172
_pool.add(score);
173+
}
174+
175+
R withScore<R>({
176+
required double value,
177+
required R Function(IndexedScore<K> score) fn,
178+
}) {
179+
final score = _acquire(value);
180+
final r = fn(score);
181+
_release(score);
166182
return r;
167183
}
168184
}

0 commit comments

Comments
 (0)