Skip to content

Commit

Permalink
Reduce Map creation on SDK API search. (#8276)
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos authored Nov 11, 2024
1 parent cb64a1c commit 6b5e2d6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/lib/search/sdk_mem_index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class SdkMemIndex {

final tokens = _tokensPerLibrary[library]!;
final plainResults =
tokens.searchWords(words).toScore().top(3, minValue: 0.05);
Score(tokens.searchWords(words).top(3, minValue: 0.05));
if (plainResults.isEmpty) continue;

final libraryWeight = _libraryWeights[library] ?? 1.0;
Expand Down
30 changes: 21 additions & 9 deletions app/lib/search/token_index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,6 @@ extension type const Score._(Map<String, double> _values)
Score mapValues(double Function(String key, double value) f) =>
Score.fromEntries(
_values.entries.map((e) => MapEntry(e.key, f(e.key, e.value))));

/// Returns a new [Score] object with the top [count] entry.
Score top(int count, {double? minValue}) {
final entries = _values.entries
.where((e) => minValue == null || e.value >= minValue)
.toList();
entries.sort((a, b) => -a.value.compareTo(b.value));
return Score(Map.fromEntries(entries.take(count)));
}
}

/// The weighted tokens used for the final search.
Expand Down Expand Up @@ -261,6 +252,9 @@ class IndexedScore<K> {
factory IndexedScore(List<K> keys, [double value = 0.0]) =>
IndexedScore._(keys, List<double>.filled(keys.length, value));

factory IndexedScore.fromMap(Map<K, double> values) =>
IndexedScore._(values.keys.toList(), values.values.toList());

List<K> get keys => _keys;
late final length = _values.length;

Expand Down Expand Up @@ -321,6 +315,24 @@ class IndexedScore<K> {
}
return set;
}

Map<K, double> top(int count, {double? minValue}) {
final list = <int>[];
double? lastValue;
for (var i = 0; i < length; i++) {
final v = _values[i];
if (minValue != null && v < minValue) continue;
if (list.length == count) {
if (lastValue != null && lastValue >= v) continue;
list[count - 1] = i;
} else {
list.add(i);
}
list.sort((a, b) => -_values[a].compareTo(_values[b]));
lastValue = _values[list.last];
}
return Map.fromEntries(list.map((i) => MapEntry(_keys[i], _values[i])));
}
}

extension StringIndexedScoreExt on IndexedScore<String> {
Expand Down
4 changes: 4 additions & 0 deletions app/test/search/token_index_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ void main() {
'a': 100.0,
});
});
});

group('IndexedScore', () {
final score = IndexedScore.fromMap({'a': 100.0, 'b': 30.0, 'c': 55.0});

test('top', () {
expect(score.top(1), {'a': 100.0});
Expand Down

0 comments on commit 6b5e2d6

Please sign in to comment.