Skip to content

Editor lags badly when Vim extension is enabled #1800

Closed

Description

@Maistho

Hi, I notice severe lag/freezing when typing in dart files in my flutter project. The lag is not there if I disable the Dart vscode extension.

Sometimes inputs are lost, most often when pressing escape (I use the vim plugin) and then quickly do some vim commands. I assume it is because the CPU is busy, since the extension host seems pinned at 25% (on a quadcore system).

I use an overclocked i5-6600k and 32GB of RAM, so my system specs should be more than enough.

I did some typing in a file and recorded the CPU profile which is attached below.

Any ideas what causes this?
CPU-20190615T122227.237Z.cpuprofile.txt
CPU-20190615T121759.944Z.cpuprofile.txt

Activity

  1. DanTup commented on Jun 15, 2019

    @DanTup
    Member

    The CPU profiles don't look too bad, but it's not clear if these are just normal typing, or if you saw the issue during the capture of these.

    A few other questions:

    Can you also confirm what version of VS Code, the Dart extension and Dart/Flutter you're using?

    Is it always Vim keystrokes that get dropped, or typing in the editor too? (in theory the extension host shouldn't be able to prevent typing in the editor, but a busy extension host could prevent - or at least delay - Vim handling commands).

    If you can reliably repro it, could you try turning off autoImportCompletions and see if it makes any difference (my guess is that it will, as I suspect the increased CPU is from the bigger completion list).

  2. added
    awaiting infoRequires more information from the customer to progress
    on Jun 15, 2019
  3. kika commented on Jun 15, 2019

    @kika

    I started to have the same problem recently. I can't say who to blame because I auto update the extension and frequently update flutter. I'm on the dev channel and always at the latest DartCode release. The issue becomes annoying, I frequently type blind for 10-15 characters while the editor lags behind and this behavior interferes with autocomplete. Like I'm typing if(something.something == null), the editor lags behind while trying to figure out null from nu and then I end up with if(something.something == nu)ll, lol. I also use vim plugin and I'm on a MacPro with 3.6GHz 8 cores (16 threads) with 64GB RAM. If this is not enough for the text editor, I don't know what is :-)
    For me it's both vim commands and the regular typing in insert mode, but dropping altogether happens rather rarely, it's the huge lag that frustrates me the most. My project is 10K Dart LOC in 77 files.

    P.S. Disabling autoImportCompletions did help!.

  4. DanTup commented on Jun 16, 2019

    @DanTup
    Member

    the editor lags behind

    Do you mean your keystrokes aren't even appearing as characters on the screen immediately as you type? It was my understanding that extensions couldn't affect this at all (since they run in another extension and everything is async), so that's quite concerning.

    Or do you mean that other things (like vim commands, code completion, red squiggles) are delayed?

    A CPU profile capture while this lagging is going on would be really helpful (maybe of both Vim and Dart extensions - I'm not sure if one profile includes both).

    Also, if you can think it's easy to repro for a non-Vim user by installing the extension and following some steps, let me know and I can have a go.

    Also - please confirm your VS Code + Extension versions. There was a serious memory leak in the previous VS Code, but that should be fixed in v1.35.

  5. dat7e commented on Jun 16, 2019

    @dat7e

    I also have this problem. For me, CPU spikes happen every time the Dart plugin tries to display autocomplete suggestion. So for example when I type "Con" for "Container", the editor is lagged at "C" (my keystrokes won't be shown on screen until the suggestion box appears).

    I'm on the latest version of VSCode, Dart plugin, Flutter, and Vim plugin. Computer is a maxed-out Macbook Pro 15 2015

    Additional notes:

    • The problem persists even when vim is disabled with: "vim.disableExtension": true
    • Turning off autoImportCompletions also doesn't seem to have any effect.
  6. Maistho commented on Jun 16, 2019

    @Maistho
    Author

    @DanTup

    The CPU profiles don't look too bad, but it's not clear if these are just normal typing, or if you saw the issue during the capture of these.

    I saw the issue during the capture of these, while typing.

    Can you also confirm what version of VS Code, the Dart extension and Dart/Flutter you're using?

    VSCode 1.35.1
    Dart-Code 3.1.0
    Flutter 1.7.3
    Dart 2.3.2-dev.0.1.flutter-6e0d978505

    Is it always Vim keystrokes that get dropped, or typing in the editor too?

    Hm, I think that only Vim keystrokes get dropped now that you say it. I can't recall it happening with regular typing. Regular typing still lags behind a lot, but I don't think any inputs are dropped.

    could you try turning off autoImportCompletions and see if it makes any difference

    Doesn't make a difference for me

    Edit: after restarting vscode it seems to make a difference, typing seems much more responsive wih it turned off. I added a screencast showing this example as well.

    Do you mean your keystrokes aren't even appearing as characters on the screen immediately as you type?

    Exactly. I'll attach a screencast showing the issue.

    With the extension active: https://youtu.be/oMb3U2sFBGs

    With the extension active and autoImportCompletions turned off: https://youtu.be/vBYSwlvjgQU

    Without the flutter/dart extensions active: https://youtu.be/DuB5oXc5TRQ

    I'm typing via a macro on my keyboard, which waits 100ms between each keystroke. They should be evenly spaced as in the example where the extensions are disabled.

  7. kika commented on Jun 16, 2019

    @kika

    the editor lags behind
    Do you mean your keystrokes aren't even appearing as characters on the screen immediately

    Correct. I also thought that VS Code should pass the characters through immediately, but this is not the case.

    Or do you mean that other things (like vim commands, code completion, red squiggles) are delayed?

    This too. This is obviously somehow related to completion.

    A CPU profile capture while this lagging is going on would be really helpful (maybe of both Vim and Dart extensions - I'm not sure if one profile includes both).

    I'll try to make it.

    Also, if you can think it's easy to repro for a non-Vim user by installing the extension and following some steps, let me know and I can have a go.

    I can't repro it on a small project with or without vim. Thus I thought it might be related to some caches, cruft, artifacts, etc, tried to clean as much as possible, but to no avail.

    Disabling autoImportCompletions makes my life much better, but doesn't completely alleviate the problem. I still type faster than VS Code can process my keystrokes and I'm not a fast typist.

    Also - please confirm your VS Code + Extension versions. There was a serious memory leak in the previous VS Code, but that should be fixed in v1.35.

    Everything latest. VS Code 1.35.1, DartCode 3.1.0

  8. DanTup commented on Jun 17, 2019

    @DanTup
    Member

    Thanks for all the info. I can repro this with the Vim extension installed, and it seems like it's something that Vim is doing. For example, you can repro this by just creating a dummy VS Code extension that blocks for 1sec when asking 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> {
    		var start = Date.now();
    		let i = 0;
    		while (Date.now() < start + 1000) {
    			// Block for a second...
    			i++;
    		}
    		return [new vscode.CompletionItem(`aaaItem (${i} iterations)`)];
    	},
    }));

    Without the Vim extension, this does not affect typing at all - the completion request runs in the extension host, and the UI is completely responsive. However if you enable the Vim extension, this causes the editor to lag. I don't exactly what's going on, but I keep seeing running save participants in the status bar, so I wonder if it's triggering something that requires VS Code to block the editor, but because the extension host is busy, it's blocked for that whole duration.

    Blocking the extension for long periods is not ideal, however since there's only one extension host and it's single threaded, this does happen when generating huge completion lists (not just because of our code, but also the JSON serialisation VS Code does).

    I'll see if I can figure out exactly what's happening to trigger this, to see if it can be avoided.

    In the meantime, it's worth trying out this new build:

    https://github.com/Dart-Code/Dart-Code/releases/tag/v3.2.0-alpha.1

    It was some performance tweaks relating to completion, so it might make the problem a little less severe (as will turning off auto-import-completions).

  9. added
    other extensionSomething caused by another extension
    and removed
    awaiting infoRequires more information from the customer to progress
    on Jun 17, 2019
  10. changed the title [-]Extension using up a lot of CPU time when typing[/-] [+]Editor lags badly when Vim extension is enabled[/+] on Jun 17, 2019
  11. DanTup commented on Jun 17, 2019

    @DanTup
    Member

    Ok, I think I tracked this down. VS Code Vim is overriding the VS Code command that handles typing:

    https://github.com/VSCodeVim/Vim/blob/1ef304e5dd857c93894c117990e2b58cb0a6abbc/extension.ts#L293

    Based in this comment that's an ok thing to do, it was specifically added to support Vim. However, it means that every key press has to go via the extension host, and that means if any extension blocks for any period of time, it will lag the typing. In the case where you press a character that triggers completion, that means the character will lag until the completion request has been satisfied.

    I don't think there's really anything we can do to fix this (besides some of the tweaks in the Alpha version above, which will yield with await periodically during processing). We can (and do) try to make completion as fast as possible, but that typing is being handled in the extension host feels like a really bad design and I think the real fix would be that extensions that need to handle typing should get their own thread (extension host), since extensions often need to do intensive processing (like building and serialising tens of thousands of objects).

    I've opened an issue with VS Code to see if they're aware of these sorts of issues and have had any thoughts:

    microsoft/vscode#75627

    I'll keep this issue open for a little while in case they have simple ideas that might improve this.

  12. kika commented on Jun 19, 2019

    @kika

    I've installed the alpha and reenabled autoImportCompletions. The result is almost perfect. It's still a little sluggish, but bearable. Thank you!

  13. DanTup commented on Jun 19, 2019

    @DanTup
    Member

    @kika glad to hear! Most likely the improvements come from adding some awaits in the loop processing the completions - this was mainly done so we can check if the request is cancelled and save some work, but it likely also has the effect that we don't block the thread for such long periods and Vim is able to process the keys more frequently.

    We might be able to tweak this further (add more awaits if there is particular code that blocks the CPU for a while), so grabbing CPU profiles (ideally that just cover an example of typing where this was observed) for your specific case(s) is useful.

    • Run the Show Running Extensions command
    • Right click Dart and choose Start Extension Host Profile
    • Reproduce the issue
    • Back in extension list, Right click Dart and choose Stop Extension Host Profile
    • Right click Dart and click Save Extension Host Profile
  14. 32 remaining items

  15. DanTup commented on Jun 8, 2020

    @DanTup
    Member

    I compared the profiles and there wasn't a huge difference, but there are some additional (and longer) Garbage Collector pauses when there's low memory (which I guess makes sense):

    Screenshot 2020-06-08 at 11 08 30

    There were some additional events (like updating codelens) that appeared in the high memory one (also adding additional time) that weren't always in the low memory one (though that could just be a timing or something).

    Soon we'll be moving to LSP, which it just occurred to me will move some of this processing out-of-process (and therefore consume less time in the extension host thread). Could you try enabling dart.previewLsp and see what difference this makes? I also have some potential upcoming changes that may significantly improve that too (removing 90% of the number of completion items) - that will also be LSP-only.

  16. minvs1 commented on Jun 11, 2020

    @minvs1

    I tried on two separate machines (but synced settings) which had the same issue. Turning LSP seems to fix this on both of them (still has some minor lag but completely doable). One machine, which profiles I am attaching below, had plenty of free resources, but the lag was still present without LSP so, maybe low on the memory (or other resources) wasn't the issue?

    Without LSP - CPU-20200611T123908.696Z.cpuprofile.txt

    With LSP - CPU-20200611T124136.775Z.cpuprofile.txt

    Thanks for the help!

  17. DanTup commented on Jun 17, 2020

    @DanTup
    Member

    Turning LSP seems to fix this on both of them

    Glad to hear! The goal is to move over to LSP by default in an upcoming release, so it sounds like that will help some. I've also recently landed a change in LSP (which hasn't made it to Flutter yet, but I hope will be in the master branch soon) that should reduce the number of completions by filtering based on the prefix (so when you press p we'll only transmit completions that start with p rather than every completion - we did this previously because the spec is vague about how backspacing is handled). On average I think this should result in 1/26th of the number of completions (which hopefully means the stutter is 1/26th as long).

    I'll ping here once that change is in Flutter's master branch so you can try it out.

  18. willhaslett commented on Jun 22, 2020

    @willhaslett

    fwiw, I too enabled dart.previewLsp, restarted, and things are much better. I was in barely-usable territory. It's still a bit laggy, but way better.

    Update: sort of. It seems to particularly struggle when typing arguments.

  19. DanTup commented on Jun 23, 2020

    @DanTup
    Member

    @willhaslett which Flutter branch are you on? A few of my changes made it to Flutter's master branch yesterday, and I've landed another change in the SDK today (which might take a few days/weeks to reach Flutter master).

    If you're still seeing stutters on master, can you give a specific code example where you see this so I can take a look? Thanks!

  20. willhaslett commented on Jun 23, 2020

    @willhaslett

    @DanTup I'm on master. Just upgraded. Will report back.

  21. willhaslett commented on Jun 24, 2020

    @willhaslett

    @DanTup way better after upgrade! Fixed for me.

  22. added this to the v3.13.0 milestone on Jun 29, 2020
  23. modified the milestones: v3.13.0, v3.14.0 on Jul 28, 2020
  24. DanTup commented on Aug 24, 2020

    @DanTup
    Member

    I'm going to consider the fix for this to be using LSP mode. It's still behind a preview flag for now, but I hope it can be enabled by default in the coming months (likely after the next Flutter stable release, so the fixes mentioned above are available to stable users) and the non-LSP code path won't be developed after that (and ultimately will be removed).

    If anyone still sees editor/typing lag when using LSP and a recent build (dev/master for Flutter), please do let me know!

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions