perf(chat): improve performance of the chat buffer#2941
Merged
Conversation
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.
Description
I've been meaning to tighten performance on the chat buffer for sometime. It's felt slow when chat history gets big. Every request at a context over 100,000 tokens seems to be delayed by a split second.
I asked Opus 4.6 to conduct a review on the chat class and identify performance improvements. It's findings were:
vim.deepcopy(self.messages)on submitcheck_context()O(n^2)make_safe()called 3x per submitsync_all_buffer_contentnested iterationChat.new()synchronous initialization_G.codecompanion_bufferslinear scanThe first 3 were all great catches, which I missed. Possibly because I'm too close to the chat class itself ("can't see the wood for the trees") and the fact I hadn't appreciated the impact of what
vim.deepcopydoes when it traverses a complex table structure.For those who are still unsure of what value AI brings to their codebase, I'd reference the commits in this PR and the overall scope which I gave Opus 4.6. I liken it to having an experienced pair of eyes look at my code for what it is with no pre-conceived ideas and biases.
AI Usage
Opus 4.6 (via Claude Code)
Related Issue(s)
Screenshots
Checklist
make allto ensure docs are generated, tests pass and StyLua has formatted the code