Skip to content

fix(diagnostics): limit amount of diagnostics processed improving performance #1010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hinell
Copy link

@hinell hinell commented Apr 2, 2025

nvim_lsp = function()
local results = {}
local diagnostics = vim.diagnostic.get()
for _, d in pairs(diagnostics) do
if diagnostic_is_enabled(d) then
if not results[d.bufnr] then results[d.bufnr] = {} end
table.insert(results[d.bufnr], d)
end
end
return results
end,

The above chunk of code gets constantly evaluated in a loop with diagnostics.get(options) fetching multiple times the same array with evergrowing number of diagnostics that get added by LSP over time.
The table diagnostics is evaluated by for _, d in pairs(diagnostics) loop multiple times though diagnostics remain the same. This results in a degraded performance.

Replicate the issue

I've discovered accidentally a massive drop in performance while configuring lua-language-server (LLS) for nvim plugin project.

Checkout a small bash script down below that automatically sets up a project that replicates the issue. It opens commands.lua file which gets over 2500+ diagnostics (to check this out, you can debug the above chunk of code).

Make sure that LLS is installed and configured for your nvim, and
you have the latest bufferline.nvim and the following config setup:

	config = {}
	config.options.diagnostics  = "nvim_lsp"
	config.options.diagnostics_update_on_event = true
	bufferline.setup(config)
REPLICATE.sh
#!/usr/bin/env -S bash 

pushd $(mktemp -d -p /tmp project-XXXXXXXXX)
test -d ./lua/test/ && echo "exists: ${_}" || mkdir -vp $_
test -s ./lua/test/commands.lua || {
cat <<EOL > ./lua/test/commands.lua
vim.api.nvim_del_user_command("string")
vim.api.nvim_create_user_command() 
---@type vim.api.keyset.create_user_command.command_args
local someVar
EOL
}


test -s .luarc.json || {
cat <<EOL > .luarc.json
{
	"\$schema": "https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json",
	"runtime": {
		"version": "LuaJIT",
		"path": [
		]
	},
	"workspace": {
		"library": [
		"/usr/local/share/nvim/runtime/lua/vim/_meta.lua",
		"/usr/local/share/nvim/runtime/lua/vim/_meta/api.lua",
		"/usr/local/share/nvim/runtime/lua/vim/_meta/api_keysets.lua",
		"/usr/local/share/nvim/runtime/lua/vim/_meta/api_keysets_extra.lua",
		"/usr/local/share/nvim/runtime/lua/vim/_meta/base64.lua",
		"/usr/local/share/nvim/runtime/lua/vim/_meta/builtin.lua",
		"/usr/local/share/nvim/runtime/lua/vim/_meta/builtin_types.lua",
		"/usr/local/share/nvim/runtime/lua/vim/_meta/diff.lua",
		"/usr/local/share/nvim/runtime/lua/vim/_meta/lpeg.lua",
		"/usr/local/share/nvim/runtime/lua/vim/_meta/regex.lua",
		"/usr/local/share/nvim/runtime/lua/vim/_meta/vimfn.lua",
		"/usr/local/share/nvim/runtime/lua/vim/_meta/vvars.lua",
		"/usr/local/share/nvim/runtime/lua/vim/_meta/vvars_extra.lua",
		"/usr/local/share/nvim/runtime/lua/vim/_options.lua"
		],
		"checkThirdParty": false,
		"userThirdParty": []
	},
	"diagnostics": {
		"globals": [
		"vim"
		],
		"disable": [
		"unused-local",
		"unused-vararg"
		]
	},
	"format.enable": false
}
EOL

}

echo "${0}: running editor ${EDITOR}"
${EDITOR:-nvim} -p ./lua/test/commands.lua /usr/local/share/nvim/runtime/lua/vim/_meta/api_keysets.lua

Workaround

Just disable diagnostics in

	config.options.diagnostics = false
	bufferline.setup(config)

Related

@hinell hinell force-pushed the diagnostics-limit branch from bb5481c to 14bbfe2 Compare April 4, 2025 12:26
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