You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maka.apache.org by as...@apache.org on 2026/10/11 12:29:20 UTC

(maka) branch main updated: perf(runtime): advance fuzzy Edit line offsets incrementally (#6050)

This is an automated email from the ASF dual-hosted git repository.

Astro-Han pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/maka.git


The following commit(s) were added to refs/heads/main by this push:
     new 38b51cc76d perf(runtime): advance fuzzy Edit line offsets incrementally (#6050)
38b51cc76d is described below

commit 38b51cc76d557a0a9e5ae9913a8f22f348e634cb
Author: seekskyworld <14...@users.noreply.github.com>
AuthorDate: Sun Oct 11 20:29:14 2026 +0800

    perf(runtime): advance fuzzy Edit line offsets incrementally (#6050)
    
    Avoid rescanning every prior line for each line-trimmed candidate.
    Keep ambiguity, newline, size-limit and replacement semantics unchanged.
    Add offset coverage and a CPU-budget regression for repetitive inputs.
    
    Fixes #6049
    
    Generated-by: OpenAI Codex
    Signed-off-by: seekskyworld <dj...@gmail.com>
---
 .../runtime/src/__tests__/edit-replace.test.ts     | 35 ++++++++++++++++++++++
 packages/runtime/src/edit-replace.ts               | 10 +++++--
 2 files changed, 42 insertions(+), 3 deletions(-)

diff --git a/packages/runtime/src/__tests__/edit-replace.test.ts b/packages/runtime/src/__tests__/edit-replace.test.ts
index 7c78ca59a1..bd6ad27a3b 100644
--- a/packages/runtime/src/__tests__/edit-replace.test.ts
+++ b/packages/runtime/src/__tests__/edit-replace.test.ts
@@ -82,6 +82,26 @@ describe('computeEditedSource โ€” exact match', () => {
 });
 
 describe('computeEditedSource โ€” fuzzy cascade', () => {
+  test('line-trimmed: keeps offsets after unequal non-matching lines and at EOF', () => {
+    const prefix = 'short\r\n\r\nlonger prefix with emoji ๐Ÿ˜€\r\n';
+    for (const suffix of ['', '\n', '\nfollowing line']) {
+      assert.deepEqual(
+        computeEditedSource(
+          `${prefix}  first();\r\n\tsecond();${suffix}`,
+          'first();\nsecond();',
+          'replacement();',
+          'offsets.ts',
+        ),
+        {
+          content: `${prefix}replacement();${suffix}`,
+          matchedVia: 'line-trimmed',
+          startLine: 4,
+          endLine: 5,
+        },
+      );
+    }
+  });
+
   test('line-trimmed: tolerates indentation drift on a multi-line block', () => {
     const content = 'function f() {\n    return 1;\n}\n'; // 4-space body
     const oldString = 'function f() {\n  return 1;\n}'; // model used 2-space body
@@ -133,6 +153,21 @@ describe('computeEditedSource โ€” fuzzy cascade', () => {
 });
 
 describe('computeEditedSource โ€” anti-corruption guards', () => {
+  test('rejects repeated fuzzy candidates within a bounded CPU budget', () => {
+    // This fits both fuzzy-input caps. Recomputing each matching line's
+    // prefix performs over a billion additions per edit before rejecting it.
+    const content = '  repeated();\n'.repeat(49_000);
+    const started = process.cpuUsage();
+    for (let attempt = 0; attempt < 4; attempt++) {
+      assert.throws(
+        () => computeEditedSource(content, 'repeated();  ', 'next();', 'repeated.ts'),
+        /line-trimmed span that occurs more than once/,
+      );
+    }
+    const { user, system } = process.cpuUsage(started);
+    assert.ok(user + system < 1_000_000, `repeated fuzzy edits used ${user + system}ยตs CPU`);
+  });
+
   test('rejects multiple distinct fuzzy candidates instead of guessing', () => {
     const content = 'function a() {\n  x;\n}\nfunction a() {\n   x;\n}\n';
     const oldString = 'function a() {\n    x;\n}'; // matches both blocks by trimmed lines
diff --git a/packages/runtime/src/edit-replace.ts b/packages/runtime/src/edit-replace.ts
index 6af561932b..b3b98912c1 100644
--- a/packages/runtime/src/edit-replace.ts
+++ b/packages/runtime/src/edit-replace.ts
@@ -32,7 +32,8 @@
 //     License:   Apache-2.0
 //     Copyright: Copyright 2025 Cline Bot Inc.
 //     Modified:  returns every matching span instead of the first, and rejects
-//                a match at EOF when old_string ended with a newline.
+//                a match at EOF when old_string ended with a newline; tracks
+//                line offsets incrementally instead of rescanning prefixes.
 //
 //   `escapeNormalizedSpans` unescape โ€” the regular expression and the first
 //   eight branches (n, t, r, ', ", `, \, newline), in order, are from
@@ -226,7 +227,12 @@ export function computeEditedSource(
       searchLines.pop();
     }
     if (searchLines.length === 0) return out;
+    let nextLineOffset = 0;
     for (let i = 0; i <= originalLines.length - searchLines.length; i++) {
+      // Advance even on a mismatch: repeated candidates must not rescan the
+      // entire file prefix just to recover their character offsets.
+      const startIndex = nextLineOffset;
+      nextLineOffset += originalLines[i].length + 1;
       let matches = true;
       for (let j = 0; j < searchLines.length; j++) {
         if (originalLines[i + j].trim() !== searchLines[j].trim()) {
@@ -235,8 +241,6 @@ export function computeEditedSource(
         }
       }
       if (!matches) continue;
-      let startIndex = 0;
-      for (let k = 0; k < i; k++) startIndex += originalLines[k].length + 1;
       let endIndex = startIndex;
       for (let k = 0; k < searchLines.length; k++) {
         endIndex += originalLines[i + k].length;


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@maka.apache.org
For additional commands, e-mail: commits-help@maka.apache.org