-
Notifications
You must be signed in to change notification settings - Fork 2.7k
perf(llm): Optimize pruneLines functions in countTokens #5310
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
base: main
Are you sure you want to change the base?
Conversation
✅ Deploy Preview for continuedev canceled.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a fairly complex change that affects a lot of things, so if we are going to consider merging this I would ask that it come with very good testing. Are you able to add unit tests that cover edge cases and make sure that this hasn't regressed in any way?
@0x23d11 There are some tests in countTokens.test.ts that you could just flesh out a bit with more examples and then unskip! |
@RomneyDa ok I've seen the tests, I'll write some more examples for the tests. After that it should be good to go right? Considering all the new test additions are good enough |
@0x23d11 I'd love to merge this PR, please let me know if you have the chance to write some tests, or if you'd like any help! |
Description
Closes #4947
This PR addresses issue #4947 by optimizing the performance of the
pruneLinesFromTop
andpruneLinesFromBottom
functions incore/llm/countTokens.ts
Problem
The previous implementations used
Array.prototype.shift()
andArray.prototype.pop()
within awhile
loop to remove lines from the beginning or end of a prompt until it fit within the token limit. These array modification methods have a time complexity of O(n) because they require shifting subsequent elements. When dealing with very long prompts (e.g., thousands of lines), repeatedly callingshift()
orpop()
becomes computationally expensive, leading to significant performance degradation.Solution
The implemented solution refactors these functions to avoid costly array modifications within the loop:
lineTokens
).lineTokens
and adding the count for necessary newline characters (\n
).while
loop iterates as long as thetotalTokens
exceedsmaxTokens
.lines
array, an index pointer (start
orend
) is adjusted.totalTokens
.Array.prototype.slice()
(an O(n) operation performed only once) is used with the finalstart
orend
index to extract the desired lines.Benefits
This approach drastically reduces the computational complexity, especially for large prompts, as the expensive O(n) operations (
shift
/pop
) inside the loop are replaced by cheap O(1) index increments/decrements. The token calculation per line and the finalslice
operation are performed only once.Checklist
Screenshots
[ For visual changes, include screenshots. Screen recordings are particularly helpful, and appreciated! ]
Testing instructions
[ For new or modified features, provide step-by-step testing instructions to validate the intended behavior of the change, including any relevant tests to run. ]