yegappan / lsp

Language Server Protocol (LSP) plugin for Vim9
MIT License
492 stars 60 forks source link

clangd + semanticHighlight causes major lag and slow down #491

Open S4deghN opened 8 months ago

S4deghN commented 8 months ago

takes seconds to delete a line or undo it same for loading buffers. just disabling semanticHighlight make everything snappy. any idea what's the issue?

this is my config for reference:

var lspOpts = {
    semanticHighlight: true,
    aleSupport: false,
    autoComplete: true,
    autoHighlight: false,
    autoHighlightDiags: true,
    autoPopulateDiags: false,
    completionMatcher: 'icase',
    completionMatcherValue: 1,
    echoSignature: false,
    hideDisabledCodeActions: false,
    highlightDiagInline: false,
    hoverInPreview: false,
    ignoreMissingServer: true,
    keepFocusInDiags: false,
    keepFocusInReferences: true,
    completionTextEdit: true,
    diagVirtualTextAlign: 'above',
    diagVirtualTextWrap: 'default',
    noNewlineInCompletion: false,
    omniComplete: false,
    outlineOnRight: false,
    outlineWinSize: 20,
    showDiagInBalloon: true,
    showDiagInPopup: true,
    showDiagOnStatusLine: false,
    showDiagWithSign: true,
    showDiagWithVirtualText: false,
    showInlayHints: false,
    showSignature: true,
    snippetSupport: false,
    ultisnipsSupport: false,
    useBufferCompletion: false,
    usePopupInCodeAction: false,
    useQuickfixForLocations: true,
    vsnipSupport: false,
    bufferCompletionTimeout: 0,
    customCompletionKinds: false,
    completionKinds: {},
    filterCompletionDuplicates: false,
}

var lspServers = [
    {
        name: 'clangd',
        filetype: ['c', 'cpp'],
        path: 'clangd',
        args: [
            '--background-index',
            "--malloc-trim",
            "--enable-config",
            "--all-scopes-completion=true",
            "--completion-style=detailed",
            "--function-arg-placeholders",
            "--header-insertion=iwyu",
            "--header-insertion-decorators",
        ]
    },
]
S4deghN commented 7 months ago

I wanted to add that syntax highlighting on small projects doesn't introduce any discernible lag. And that I also tested the same project with a minimal neovim with native lsp config to see if it's a clangd issue but neovim didn't have any problem with syntax highlighting.

MisterSnuvi commented 3 weeks ago

I tried debugging this as I was experiencing that myself. It looks to me that on many changes, e.g. when "dd" is spammed, the synchronous server calls cannot keep up and block for a longer period. I fixed it on my side by making the call at lspserver.vim#L564 asynchronous and calling "UpdateTokens" in the callback. I am not sure if this is a good solution as I am not aware of the reason for a synchronous call in the first place (maybe just to prevent race conditions?).

eithrial commented 1 week ago

@MisterSnuvi could you provide they way you fixed this? I tried making it asynchronous, but results using my solution were mixed, so I still keep semantic highlighting off.

MisterSnuvi commented 1 week ago

@eithrial I changed the lines from lspserver.vim#564 to 568 to

564   │   lspserver.rpc_a(method, params, (_, reply) => {
565   │     if reply->empty()
566   │       return
567   │     endif
568   │     semantichighlight.UpdateTokens(lspserver, bnr, reply)
569   │   })

It's not perfectly stable with this but the lags are mostly gone.

S4deghN commented 1 week ago

@eithrial I changed the lines from lspserver.vim#564 to 568 to

564   │   lspserver.rpc_a(method, params, (_, reply) => {
565   │     if reply->empty()
566   │       return
567   │     endif
568   │     semantichighlight.UpdateTokens(lspserver, bnr, reply)
569   │   })

It's not perfectly stable with this but the lags are mostly gone.

I want to confirm that this diminishes the lag for me too, but it's not all gone.