mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-05 21:10:21 +00:00
perf: harden chunking against quadratic scans
This commit is contained in:
@@ -73,7 +73,27 @@ export function parseFenceSpans(buffer: string): FenceSpan[] {
|
||||
}
|
||||
|
||||
export function findFenceSpanAt(spans: FenceSpan[], index: number): FenceSpan | undefined {
|
||||
return spans.find((span) => index > span.start && index < span.end);
|
||||
let low = 0;
|
||||
let high = spans.length - 1;
|
||||
|
||||
while (low <= high) {
|
||||
const mid = Math.floor((low + high) / 2);
|
||||
const span = spans[mid];
|
||||
if (!span) {
|
||||
break;
|
||||
}
|
||||
if (index <= span.start) {
|
||||
high = mid - 1;
|
||||
continue;
|
||||
}
|
||||
if (index >= span.end) {
|
||||
low = mid + 1;
|
||||
continue;
|
||||
}
|
||||
return span;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function isSafeFenceBreak(spans: FenceSpan[], index: number): boolean {
|
||||
|
||||
Reference in New Issue
Block a user