Repository navigation
Conversation
Opening a document whose URI embeds a long payload (e.g. an AI diff virtual document) made files.normalize scan the whole string from every start position, because the '..' folding pattern requires a path separator that such a URI does not contain. The default 'Lua.diagnostics.enableScheme' check did not help since it runs after ws.isIgnored(). Only fold when the path can contain '..' and is short enough to be a real path, and skip path handling entirely for non-file URIs in ws.isIgnored().
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Avoid quadratic path normalization on long virtual URIs
Closes #3501.
Problem
A
textDocument/didOpenwhose URI embeds a long payload (virtual diff documents do this: the URI carries the whole file as URL-encoded base64) blocks the single LSP thread for minutes at 100% of one core. In my session,didOpenitself took 376 s and every queueddocumentColor/inlayHint/codeAction/codeLens/semanticTokens/documentSymbol/foldingRange/$/cancelRequestrequest returned afterwards, all of them taking 216 s.The
didOpenhandler callsfiles.open(), which dispatches theopenwatchers synchronously; the diagnostics watcher reachesworkspace.isIgnored()→getRelativePath()→files.normalize(). Because the URI does not start withfile:, the whole URI is treated as a filesystem path and normalized. The..-folding loop then runson a ~100 KB string that contains no
/,\or.(URL-encoded base64 escapes both+and/), so the pattern engine scans to the end of the string and backtracks from every start position: O(n²), ~5.4×10⁹ steps for 104 k characters.Lua.diagnostics.enableScheme(default["file"]) does not help, because it is checked afterisIgnored().Changes
script/files.lua: run the..-folding loop only when the string contains..and is at most 4096 characters long. Thegsubcan never match without a literal.., so skip it with a plain (linear)find. The length limit also protects other callers from the same pattern on absurdly long inputs; real filesystem paths are far below it.script/workspace/workspace.lua: inisIgnored, returnfalsefor non-file:schemes before any path handling. Ignore patterns are filesystem paths, so matching a virtual document URI against them is meaningless — and currently very expensive.test/basic/uri-normalize.lua: regression test. It asserts thatfiles.normalizeon a 100 k-character separator-free URI returns unchanged within 2 s, thatworkspace.isIgnoredreturnsfalsequickly for such a URI, and that ordinary paths still fold..segments.changelog.md: oneFIXentry underUnreleased.Measurements
didOpen+ follow-up requestfiles.normalize/every 64 charsBehavior
No behavior change for real paths: when the string has no
.., the folding loop could not have matched anyway; when it has..and is shorter than 4096 characters, the code path is exactly the old one. Non-file:URIs were never meaningfully matched byfiles.exclude/.gitignore, which are filesystem pattern sets.