Optimize on_did_change()
#364
Merged
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.
Branched from #363
It turns out that we are rebuilding our
LineIndexat least twice on every singleon_did_change()call.LineIndexbuilt for thisDocumentair/crates/lsp/src/rust_analyzer/utils.rs
Line 35 in be96e71
air/crates/lsp/src/documents.rs
Lines 135 to 136 in be96e71
That's not super efficient! In the ~90% case where the user is just typing one character at a time, we should only be rebuilding the line index exactly 1 time.
I've folded
apply_document_changes()intoon_did_change()to keep all of the mutable-state-updating code close together.The actual implementation is basically still the same, I've just had to rework things a little to do this optimization.
I had added in some logging and can confirm that we now only rebuild the
LineIndex1 time on every update as the user is typing. We typically also rebuild it exactly 1 time during a find-and-replace as well, due to how we take advantage of VS Code sending us the updates from bottom to top (so we can apply them sequentially without invalidating our line index).I might try a PR upstream to rust-analyzer, because I'm fairly certain they have this too from looking at their code.