Skip to content

Commit 0e70bf0

Browse files
authored
Reduce Score creation in the search ranking step. (#8264)
1 parent 6b5e2d6 commit 0e70bf0

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

app/lib/search/mem_index.dart

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class InMemoryPackageIndex {
3030

3131
/// Adjusted score takes the overall score and transforms
3232
/// it linearly into the [0.4-1.0] range.
33-
final _adjustedOverallScores = <String, double>{};
33+
late final List<double> _adjustedOverallScores;
3434
late final List<IndexedPackageHit> _overallOrderedHits;
3535
late final List<IndexedPackageHit> _createdOrderedHits;
3636
late final List<IndexedPackageHit> _updatedOrderedHits;
@@ -197,13 +197,11 @@ class InMemoryPackageIndex {
197197
/// Adjusted score takes the overall score and transforms
198198
/// it linearly into the [0.4-1.0] range, to allow better
199199
/// multiplication outcomes.
200-
final overallScore = textResults.pkgScore
201-
.mapValues((key, value) => value * _adjustedOverallScores[key]!);
202-
packageHits = _rankWithValues(overallScore);
200+
packageScores.multiplyAllFromValues(_adjustedOverallScores);
201+
packageHits = _rankWithValues(packageScores);
203202
break;
204203
case SearchOrder.text:
205-
final score = textResults?.pkgScore ?? Score.empty;
206-
packageHits = _rankWithValues(score);
204+
packageHits = _rankWithValues(packageScores);
207205
break;
208206
case SearchOrder.created:
209207
packageHits = _createdOrderedHits.whereInScores(packageScores);
@@ -251,16 +249,16 @@ class InMemoryPackageIndex {
251249

252250
/// Update the overall score both on [PackageDocument] and in the [_adjustedOverallScores] map.
253251
void _updateOverallScores() {
254-
for (final doc in _documentsByName.values) {
252+
_adjustedOverallScores = _documents.map((doc) {
255253
final downloadScore = doc.popularityScore ?? 0.0;
256254
final likeScore = doc.likeScore ?? 0.0;
257255
final popularity = (downloadScore + likeScore) / 2;
258256
final points = doc.grantedPoints / math.max(1, doc.maxPoints);
259257
final overall = popularity * 0.5 + points * 0.5;
260258
doc.overallScore = overall;
261259
// adding a base score prevents later multiplication with zero
262-
_adjustedOverallScores[doc.package] = 0.4 + 0.6 * overall;
263-
}
260+
return 0.4 + 0.6 * overall;
261+
}).toList();
264262
}
265263

266264
_TextResults? _searchText(
@@ -272,6 +270,9 @@ class InMemoryPackageIndex {
272270
if (text != null && text.isNotEmpty) {
273271
final words = splitForQuery(text);
274272
if (words.isEmpty) {
273+
for (var i = 0; i < packageScores.length; i++) {
274+
packageScores.setValue(i, 0);
275+
}
275276
return _TextResults.empty();
276277
}
277278

@@ -361,26 +362,28 @@ class InMemoryPackageIndex {
361362
}
362363

363364
return _TextResults(
364-
packageScores.toScore(),
365365
topApiPages,
366366
nameMatches: nameMatches?.toList(),
367367
);
368368
}
369369
return null;
370370
}
371371

372-
List<PackageHit> _rankWithValues(Map<String, double> values) {
373-
final list = values.entries
374-
.map((e) => PackageHit(package: e.key, score: e.value))
375-
.toList();
372+
List<PackageHit> _rankWithValues(IndexedScore<String> score) {
373+
final list = <IndexedPackageHit>[];
374+
for (var i = 0; i < score.length; i++) {
375+
final value = score.getValue(i);
376+
if (value <= 0.0) continue;
377+
list.add(IndexedPackageHit(
378+
i, PackageHit(package: score.keys[i], score: value)));
379+
}
376380
list.sort((a, b) {
377-
final int scoreCompare = -a.score!.compareTo(b.score!);
381+
final scoreCompare = -a.hit.score!.compareTo(b.hit.score!);
378382
if (scoreCompare != 0) return scoreCompare;
379383
// if two packages got the same score, order by last updated
380-
return _compareUpdated(
381-
_documentsByName[a.package]!, _documentsByName[b.package]!);
384+
return _compareUpdated(_documents[a.index], _documents[b.index]);
382385
});
383-
return list;
386+
return list.map((h) => h.hit).toList();
384387
}
385388

386389
List<IndexedPackageHit> _rankWithComparator(
@@ -438,18 +441,15 @@ class InMemoryPackageIndex {
438441
}
439442

440443
class _TextResults {
441-
final Score pkgScore;
442444
final Map<String, List<MapEntry<String, double>>> topApiPages;
443445
final List<String>? nameMatches;
444446

445447
factory _TextResults.empty() => _TextResults(
446-
Score.empty,
447448
{},
448449
nameMatches: null,
449450
);
450451

451452
_TextResults(
452-
this.pkgScore,
453453
this.topApiPages, {
454454
required this.nameMatches,
455455
});

app/lib/search/token_index.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,14 @@ class IndexedScore<K> {
297297
}
298298

299299
void multiplyAllFrom(IndexedScore other) {
300-
assert(other._values.length == _values.length);
300+
multiplyAllFromValues(other._values);
301+
}
302+
303+
void multiplyAllFromValues(List<double> values) {
304+
assert(_values.length == values.length);
301305
for (var i = 0; i < _values.length; i++) {
302306
if (_values[i] == 0.0) continue;
303-
final v = other._values[i];
307+
final v = values[i];
304308
_values[i] = v == 0.0 ? 0.0 : _values[i] * v;
305309
}
306310
}

0 commit comments

Comments
 (0)