microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
162.22k stars 28.55k forks source link

Extensions using the "type" command (for ex. Vim) have poor performance due to being single-threaded with other extensions #75627

Open DanTup opened 5 years ago

DanTup commented 5 years ago

I don't know if this is a known/accepted issue, but I've had a number of users complain of poor performance in the editor when using my extension along with the Vim extension.

This appears to be because the Vim extension uses the type command to handle keypresses (in the extension host). This means if pressing a key triggers a command that blocks in another extension (for example the first character press can trigger code completion, which if the list is 20,000 items can block the thread for a little while while they're build + serialised) the typing in the editor is really sluggish.

You can easily reproduce this by making an extension that blocks for 1s when asked for completions:

context.subscriptions.push(vscode.languages.registerCompletionItemProvider({ scheme: "file" }, {
    provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken, context: vscode.CompletionContext): vscode.ProviderResult<vscode.CompletionItem[] | vscode.CompletionList> {
        console.log('Completion request');
        var start = Date.now();
        let i = 0;
        while (Date.now() < start + 10000) {
            // Block for a second...
            i++;
        }
        return [new vscode.CompletionItem(`aaaItem (${i} iterations)`)];
    },
}));

If you run this and enabled the Vim plugin, when you start typing on a newline (which triggers completion), the characters you type won't appear for a while.

Of course, extensions should try to avoid blocking the extension host as much as possible, but sometimes it's unavoidable (for ex. assembling and serialising a huge number of completion items). It's not clear where users should raise bugs, since in isolation neither extension is really doing anything wrong.

I don't know what the fix is (separate extension host for type-handling extensions might work, but that might also be a huge task), but I couldn't find any issues discussing this and figured it was worth some discussion (even if only to have the information described in one place we can point people to that hit these issue).

CleyFaye commented 12 months ago

I stumbled upon this issue while searching for laggy typing and missed input. After running the profiler I found two extension that had >150ms runtime where other had <1ms: vim and Intellicode.

Using the affinity setting I moved vim out of the way (affinity=1), but the lagginess remained. I also moved intellicode (and the intellicode completion extension) in yet another process (affinity=2 for both of them), and the issue disappeared.

Thanks for that. I'm not sure how desirable it could be to auto-adjust this setting when some extension misbehave, but even as a clutch it works well enough.

victorallume commented 11 months ago

I'm having this issue re-appear after seemingly being fixed a few months ago with the affinity setting. It seems to be an interaction of two or more extensions. If I disable either pylance or vscodevim, it seems to work happily. But with both of them installed, as soon as I start typing something (in insert mode), I get crazy lag and skipped and repeated keystrokes (and sometimes it treats inline blame annotations (via Gitlens) as actual text in the editor). While the madness is happening (and indeed, every single time I start typing something, VSCode starts analyzing some number of files). Restarting VSCode doesn't fix the issue.