Perf: replace-all, yank and repeat counts as single edits

Each piece-table edit costs time linear in the buffer (piece lookup,
line-index update), so operations issuing one edit per match, line or
repetition were quadratic:

- Plain and regex replace-all build the new text in one pass and apply
  it as one delete + one insert (one undo group, one chunked journal
  record). 12 MB with 229k matches: minutes -> 0.14 s.
- Yank inserts the whole kill in one edit (it split and re-trimmed line
  by line): 200k lines in 0.02 s.
- InsertText with a repeat count inserts the repeated text once; the
  total is capped at 256 MiB (also across visual-line broadcast).
- Incremental regex search skips lines over 20,000 bytes and renderer
  match highlighting over 10,000 (the previous change raised the render
  limit to 1 MB): std::regex can be quadratic in line length even for
  ordinary patterns such as '.*q', uninterruptibly, and these run on
  every keystroke or frame. Explicit regex replace still processes all.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015rcVQiiWyTzoNrsvLSaAfB
This commit is contained in:
Claude
2026-09-24 09:16:04 -07:00
committed by Kyle Isom
parent a0441e4c25
commit 91ceb9bb01
3 changed files with 179 additions and 93 deletions
+52
View File
@@ -1146,3 +1146,55 @@ TEST(Audit_VirtualBuffer_SavedAs_GetsJournal)
ASSERT_TRUE(std::filesystem::exists(swp));
std::filesystem::remove(swp);
}
// Replace-all is applied as one edit; results and undo are unchanged.
TEST(Audit_ReplaceAll_WholeBuffer_ResultAndUndo)
{
TestHarness h;
Buffer &b = h.Buf();
const std::string orig = "cat cat\ndog\ncatcat\n";
b.insert_text(0, 0, orig);
b.SetCursor(0, 0);
ASSERT_TRUE(h.Exec(CommandId::SearchReplace));
ASSERT_TRUE(h.Exec(CommandId::InsertText, "cat"));
ASSERT_TRUE(h.Exec(CommandId::Newline));
ASSERT_TRUE(h.Exec(CommandId::InsertText, "ox"));
ASSERT_TRUE(h.Exec(CommandId::Newline));
ASSERT_EQ(h.Text(), std::string("ox ox\ndog\noxox\n"));
ASSERT_TRUE(h.Undo());
ASSERT_EQ(h.Text(), orig);
ASSERT_TRUE(h.Redo());
ASSERT_EQ(h.Text(), std::string("ox ox\ndog\noxox\n"));
}
// Yanking a multi-line kill inserts it in one edit and leaves the cursor
// after it.
TEST(Audit_Yank_MultiLine_SingleInsert)
{
TestHarness h;
Editor &ed = h.EditorRef();
Buffer &b = h.Buf();
b.insert_text(0, 0, "AB");
b.SetCursor(1, 0);
ed.KillRingPush("x\ny\nz");
ASSERT_TRUE(h.Exec(CommandId::Yank));
ASSERT_EQ(h.Text(), std::string("Ax\ny\nzB"));
ASSERT_EQ(b.Cury(), (std::size_t) 2);
ASSERT_EQ(b.Curx(), (std::size_t) 1);
ASSERT_TRUE(h.Undo());
ASSERT_EQ(h.Text(), std::string("AB"));
}
// A repeat count inserts the repeated text once; undo removes all of it.
TEST(Audit_InsertText_RepeatCount)
{
TestHarness h;
ASSERT_TRUE(h.Exec(CommandId::InsertText, "ab", 3));
ASSERT_EQ(h.Text(), std::string("ababab"));
ASSERT_EQ(h.Buf().Curx(), (std::size_t) 6);
ASSERT_TRUE(h.Undo());
ASSERT_EQ(h.Text(), std::string(""));
}