The functions pruneLinesFromTop
and pruneLinesFromBottom
are time-consuming when processing large files, which negatively impacts the code completion experience.
#4947
Labels
area:autocomplete
Relates to the auto complete feature
kind:enhancement
Indicates a new feature request, imrovement, or extension
Validations
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");
}`
The text was updated successfully, but these errors were encountered: