-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Description
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");
}`