From e808161b9e17b9ef734d1063878f03f7ce253c23 Mon Sep 17 00:00:00 2001 From: Johannes Rieken Date: Tue, 2 Oct 2018 17:24:55 +0200 Subject: [PATCH] perf - pre-compute and cache lower case variants for label, sortText, and filterText --- src/vs/editor/common/modes.ts | 9 +++++ .../editor/contrib/suggest/completionModel.ts | 7 ++-- src/vs/editor/contrib/suggest/suggest.ts | 35 +++++++++++++------ src/vs/monaco.d.ts | 2 -- .../api/node/extHostLanguageFeatures.ts | 6 +++- 5 files changed, 42 insertions(+), 17 deletions(-) diff --git a/src/vs/editor/common/modes.ts b/src/vs/editor/common/modes.ts index 75e3a69a0fb..dc894483930 100644 --- a/src/vs/editor/common/modes.ts +++ b/src/vs/editor/common/modes.ts @@ -437,8 +437,17 @@ export interface CompletionItem { * A command that should be run upon acceptance of this item. */ command?: Command; + /**@internal*/ noWhitespaceAdjust?: boolean; + /**@internal*/ noAutoAccept?: boolean; + + /**@internal*/ + _labelLow?: string; + /**@internal*/ + _sortTextLow?: string; + /**@internal*/ + _filterTextLow?: string; } export interface CompletionList { diff --git a/src/vs/editor/contrib/suggest/completionModel.ts b/src/vs/editor/contrib/suggest/completionModel.ts index bd30bfcff7c..c49ddfd58a6 100644 --- a/src/vs/editor/contrib/suggest/completionModel.ts +++ b/src/vs/editor/contrib/suggest/completionModel.ts @@ -21,7 +21,6 @@ export interface ICompletionItem extends ISuggestionItem { word?: string; } - /* __GDPR__FRAGMENT__ "ICompletionStats" : { "suggestionCount" : { "classification": "SystemMetaData", "purpose": "FeatureInsight", "isMeasurement": true }, @@ -221,16 +220,16 @@ export class CompletionModel { // if it matches we check with the label to compute highlights // and if that doesn't yield a result we have no highlights, // despite having the match - let match = scoreFn(word, wordLow, wordPos, suggestion.filterText, suggestion.filterText.toLowerCase(), 0, false); + let match = scoreFn(word, wordLow, wordPos, suggestion.filterText, suggestion._filterTextLow, 0, false); if (!match) { continue; } item.score = match[0]; - item.matches = (fuzzyScore(word, wordLow, 0, suggestion.label, suggestion.label.toLowerCase(), 0, true) || anyScore(word, suggestion.label))[1]; + item.matches = (fuzzyScore(word, wordLow, 0, suggestion.label, suggestion._labelLow, 0, true) || anyScore(word, suggestion.label))[1]; } else { // by default match `word` against the `label` - let match = scoreFn(word, wordLow, wordPos, suggestion.label, suggestion.label.toLowerCase(), 0, false); + let match = scoreFn(word, wordLow, wordPos, suggestion.label, suggestion._labelLow, 0, false); if (match) { item.score = match[0]; item.matches = match[1]; diff --git a/src/vs/editor/contrib/suggest/suggest.ts b/src/vs/editor/contrib/suggest/suggest.ts index ae5ba1f5739..bc1a7feedd1 100644 --- a/src/vs/editor/contrib/suggest/suggest.ts +++ b/src/vs/editor/contrib/suggest/suggest.ts @@ -5,7 +5,6 @@ import { first } from 'vs/base/common/async'; import { isFalsyOrEmpty } from 'vs/base/common/arrays'; -import { compareIgnoreCase } from 'vs/base/common/strings'; import { assign } from 'vs/base/common/objects'; import { onUnexpectedExternalError, canceled } from 'vs/base/common/errors'; import { IEditorContribution } from 'vs/editor/common/editorCommon'; @@ -94,10 +93,22 @@ export function provideSuggestionItems( for (let suggestion of container.suggestions) { if (acceptSuggestion(suggestion)) { + // fill in default range when missing if (!suggestion.range) { suggestion.range = defaultRange; } + // fill in lower-case text + if (!suggestion._labelLow) { + suggestion._labelLow = suggestion.label.toLowerCase(); + } + if (suggestion.sortText && !suggestion._sortTextLow) { + suggestion._sortTextLow = suggestion.sortText.toLowerCase(); + } + if (suggestion.filterText && !suggestion._filterTextLow) { + suggestion._filterTextLow = suggestion.filterText.toLowerCase(); + } + allSuggestions.push({ position, container, @@ -156,28 +167,32 @@ function createSuggesionFilter(snippetConfig: SnippetConfig): (candidate: Comple } function defaultComparator(a: ISuggestionItem, b: ISuggestionItem): number { - let ret = 0; - // check with 'sortText' if (typeof a.suggestion.sortText === 'string' && typeof b.suggestion.sortText === 'string') { - ret = compareIgnoreCase(a.suggestion.sortText, b.suggestion.sortText); + if (a.suggestion._sortTextLow < b.suggestion._sortTextLow) { + return -1; + } else if (a.suggestion._sortTextLow > b.suggestion._sortTextLow) { + return 1; + } } // check with 'label' - if (ret === 0) { - ret = compareIgnoreCase(a.suggestion.label, b.suggestion.label); + if (a.suggestion.label < b.suggestion.label) { + return -1; + } else if (a.suggestion.label > b.suggestion.label) { + return 1; } // check with 'type' and lower snippets - if (ret === 0 && a.suggestion.kind !== b.suggestion.kind) { + if (a.suggestion.kind !== b.suggestion.kind) { if (a.suggestion.kind === CompletionItemKind.Snippet) { - ret = 1; + return 1; } else if (b.suggestion.kind === CompletionItemKind.Snippet) { - ret = -1; + return -1; } } - return ret; + return 0; } function snippetUpComparator(a: ISuggestionItem, b: ISuggestionItem): number { diff --git a/src/vs/monaco.d.ts b/src/vs/monaco.d.ts index 8395cb4d2d4..b01654c95dd 100644 --- a/src/vs/monaco.d.ts +++ b/src/vs/monaco.d.ts @@ -4757,8 +4757,6 @@ declare namespace monaco.languages { * A command that should be run upon acceptance of this item. */ command?: Command; - noWhitespaceAdjust?: boolean; - noAutoAccept?: boolean; } export interface CompletionList { diff --git a/src/vs/workbench/api/node/extHostLanguageFeatures.ts b/src/vs/workbench/api/node/extHostLanguageFeatures.ts index 9ea2c1ff079..9fba336e312 100644 --- a/src/vs/workbench/api/node/extHostLanguageFeatures.ts +++ b/src/vs/workbench/api/node/extHostLanguageFeatures.ts @@ -675,7 +675,11 @@ class SuggestAdapter { insertText: undefined, additionalTextEdits: item.additionalTextEdits && item.additionalTextEdits.map(typeConvert.TextEdit.from), command: this._commands.toInternal(item.command), - commitCharacters: item.commitCharacters + commitCharacters: item.commitCharacters, + // help with perf + _labelLow: item.label.toLowerCase(), + _filterTextLow: item.filterText && item.filterText.toLowerCase(), + _sortTextLow: item.sortText && item.sortText.toLowerCase() }; // 'insertText'-logic -- GitLab