提交 07d13825 编写于 作者: B Benjamin Pasero

add cache for scorer to speed up things

上级 3fe08d6a
......@@ -31,13 +31,18 @@
* Start of word/path bonus: 7
* Start of string bonus: 8
*/
export function score(target: string, query: string): number {
export function score(target: string, query: string, cache?: {[id: string]: number}): number {
let score = 0;
if (!target || !query) {
return score; // return early if target or query are undefined
}
const cached = cache && cache[target + query];
if (typeof cached === 'number') {
return cached;
}
const queryLen = query.length;
const targetLower = target.toLowerCase();
const queryLower = query.toLowerCase();
......@@ -77,6 +82,10 @@ export function score(target: string, query: string): number {
index++;
}
if (cache) {
cache[target + query] = score;
}
return score;
}
......
......@@ -32,4 +32,12 @@ suite('Scorer', () => {
let sortedScores = scores.sort();
assert.deepEqual(scores.reverse(), sortedScores);
});
test("cache", function() {
const cache = Object.create(null);
scorer.score('target', 'query', cache);
assert.ok(Object.getOwnPropertyNames(cache).length > 0);
});
});
\ No newline at end of file
......@@ -45,6 +45,7 @@ export class OpenAnythingHandler extends QuickOpenHandler {
private delayer: ThrottledDelayer<QuickOpenModel>;
private pendingSearch: TPromise<QuickOpenModel>;
private isClosed: boolean;
private scorerCache: {[key: string]: number};
private fuzzyMatchingEnabled: boolean;
private configurationListenerUnbind: ListenerUnbind;
......@@ -64,6 +65,7 @@ export class OpenAnythingHandler extends QuickOpenHandler {
this.openFileHandler.setStandalone(false);
this.resultsToSearchCache = Object.create(null);
this.scorerCache = Object.create(null);
this.delayer = new ThrottledDelayer<QuickOpenModel>(OpenAnythingHandler.SEARCH_DELAY);
this.updateFuzzyMatching(contextService.getOptions().globalSettings.settings);
......@@ -293,15 +295,15 @@ export class OpenAnythingHandler extends QuickOpenHandler {
// Fuzzy scoring is special
if (enableFuzzyScoring) {
const labelAScore = scorer.score(elementA.getLabel(), lookFor);
const labelBScore = scorer.score(elementB.getLabel(), lookFor);
const labelAScore = scorer.score(elementA.getLabel(), lookFor, this.scorerCache);
const labelBScore = scorer.score(elementB.getLabel(), lookFor, this.scorerCache);
if (labelAScore !== labelBScore) {
return labelAScore > labelBScore ? -1 : 1;
}
const descriptionAScore = scorer.score(elementA.getDescription(), lookFor);
const descriptionBScore = scorer.score(elementB.getDescription(), lookFor);
const descriptionAScore = scorer.score(elementA.getDescription(), lookFor, this.scorerCache);
const descriptionBScore = scorer.score(elementB.getDescription(), lookFor, this.scorerCache);
if (descriptionAScore !== descriptionBScore) {
return descriptionAScore > descriptionBScore ? -1 : 1;
......@@ -329,6 +331,7 @@ export class OpenAnythingHandler extends QuickOpenHandler {
// Clear Cache
this.resultsToSearchCache = Object.create(null);
this.scorerCache = Object.create(null);
// Propagate
this.openSymbolHandler.onClose(canceled);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册