Skip to content
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

The functions pruneLinesFromTop and pruneLinesFromBottom are time-consuming when processing large files, which negatively impacts the code completion experience. #4947

Open
2 tasks done
DanielYzd opened this issue Apr 2, 2025 · 1 comment
Assignees
Labels
area:autocomplete Relates to the auto complete feature kind:enhancement Indicates a new feature request, imrovement, or extension

Comments

@DanielYzd
Copy link

Validations

  • I believe this is a way to improve. I'll try to join the Continue Discord for questions
  • I'm not able to find an open issue that requests the same enhancement

Problem

countTokens.ts
pruneLinesFromTop、pruneLinesFromBottom
Process a file with 2000 lines, requiring the removal of the first 1000 lines.

Original Version: Execute the shift() operation 1000 times, each time moving 2000 → 1999 → ... → 1001 Elements, resulting in a total of approximately (2000+1001)*1000/2 = 1,500,500 operations.

Optimized Version: Preprocess the tokens of 2000 lines (O(n)), loop 1000 times with index increment (O(n)), totaling approximately 3000 operations.

Solution

Optimize as follows
`function pruneLinesFromTop(
prompt: string,
maxTokens: number,
modelName: string,
): string {
const lines = prompt.split("\n");
//Preprocess tokens for all rows and cache them.
const lineTokens = lines.map(line => countTokens(line, modelName));
let totalTokens = lineTokens.reduce((sum, tokens) => sum + tokens, 0);
let start = 0;

// Using indexes instead of array modifications.
while (totalTokens > maxTokens && start < lines.length) {
totalTokens -= lineTokens[start];
start++;
}

return lines.slice(start).join("\n");
}

function pruneLinesFromBottom(
prompt: string,
maxTokens: number,
modelName: string,
): string {
const lines = prompt.split("\n");
const lineTokens = lines.map(line => countTokens(line, modelName));
let totalTokens = lineTokens.reduce((sum, tokens) => sum + tokens, 0);
let end = lines.length;

// Reverse traversal to avoid array modification
while (totalTokens > maxTokens && end > 0) {
end--;
totalTokens -= lineTokens[end];
}

return lines.slice(0, end).join("\n");
}`

@dosubot dosubot bot added area:autocomplete Relates to the auto complete feature kind:enhancement Indicates a new feature request, imrovement, or extension labels Apr 2, 2025
@Patrick-Erichsen
Copy link
Collaborator

Thanks for the analysis here! That code looks more/less correct to me, any chance you'd be willing to open up a PR with the changes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:autocomplete Relates to the auto complete feature kind:enhancement Indicates a new feature request, imrovement, or extension
Projects
None yet
Development

No branches or pull requests

2 participants