Skip to content

Conversation

@DavisVaughan
Copy link
Collaborator

@DavisVaughan DavisVaughan commented Jun 13, 2025

Branched from #363

It turns out that we are rebuilding our LineIndex at least twice on every single on_did_change() call.

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() into on_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 LineIndex 1 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.

Comment on lines -187 to -190
index_valid = range.start.line;
if let Ok(range) = from_proto::text_range(range, &line_index, encoding) {
text.replace_range(Range::<usize>::from(range), &change.text);
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We used to just...skip (??) the file update if we were unable to convert from Position to TextRange.

This is a critical failure IMO and should be treated as a panic, which we now do

}
text
// Rebuild the `line_index` after applying the final edit, and sync other fields
self.line_index = LineIndex::new(&self.contents);
Copy link
Collaborator Author

@DavisVaughan DavisVaughan Jun 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the cases of:

  • A single full document change
  • 1 or more incremental changes that don't invalidate each other

this is now the only place we recreate the line index

@DavisVaughan DavisVaughan requested a review from lionel- June 13, 2025 16:49
Base automatically changed from feature/proto-refactor to main June 16, 2025 13:07
@DavisVaughan DavisVaughan force-pushed the feature/did-change-optimization branch from ddb5fdb to 8f9da47 Compare June 16, 2025 13:08
@DavisVaughan DavisVaughan merged commit 40beb05 into main Jun 16, 2025
4 checks passed
@DavisVaughan DavisVaughan deleted the feature/did-change-optimization branch June 16, 2025 13:09
Hinkee

This comment was marked as spam.

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.

4 participants