Skip to content

Command palette: update the TF-IDF index instead of rebuilding it - #350404

Draft
Federico Brancasi (federicobrancasi) wants to merge 1 commit into
mainfrom
fbrancasi/command-palette-tfidf-reuse
Draft

Federico Brancasi (federicobrancasi) wants to merge 1 commit into
mainfrom
fbrancasi/command-palette-tfidf-reuse

Conversation

@federicobrancasi

Copy link
Copy Markdown
Member

The command palette ranks "similar commands" with TF-IDF once the filter has three or more characters and a command doesn't match by its label. _getPicks built a new TfIdfCalculator over all commands for each query, so every keystroke split and counted the words of every command again.

This PR keeps one calculator in the provider. Each query reconciles it with the current commands: commands that are new or whose text changed are indexed again, and removed ones are deleted. TF-IDF still runs only when a query needs it.

Results stay those of a freshly built calculator:

  • Ties at the cut to five results. A kept calculator lists commands in the order they were first indexed. When more than five commands score, the scores are first put in the order of the current commands, so equal scores are cut as before.
  • Duplicate command IDs. The last command with an ID wins, as before. One difference: the old code also counted the earlier command in the word statistics (updateDocuments deletes all keys first, then adds every document), so its words counted twice. Each command now counts once. This can change scores only when two commands share an ID and the filter has several words.

The word counts of all commands now stay in memory while the window is open, instead of being rebuilt for each query.

No issue is associated; this came from profiling.

Measured on my machine

Code OSS dev build, scripted scenarios that run commands by typing their names in the command palette; window CPU profile, the same scenarios before and after:

TfIdfCalculator.updateDocuments Before After
Git scenario 111 ms 4 ms
Large file scenario 109 ms 0 ms

How to test

The two new tests check that similar commands follow added, removed and changed commands, and that ties are cut in command order. They also pass with the old code, which builds a fresh calculator each time; each fails when removed commands aren't deleted or when the order before the cut isn't restored.

Command Result
./scripts/test.sh --run src/vs/platform/quickinput/test/browser/commandsQuickAccess.test.ts --run src/vs/base/test/common/tfIdf.test.ts passed, 13 passing
node_modules/.bin/tsc --project ./src/tsconfig.json --noEmit --skipLibCheck no errors in the changed files (9 errors in src/vs/platform/agentHost/, from my local Copilot SDK package being older than the one on main)
node_modules/.bin/oxlint <changed files> passed

Every query of three or more characters built a new TF-IDF calculator
over all commands, splitting and counting the words of every command
again on each keystroke. Keep one calculator and only index commands
that are new or changed, and delete removed ones. Scores are ordered
like the commands before the five-result cut, so ties end up as with a
fresh calculator.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant