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:
+121
-90
@@ -614,6 +614,47 @@ delete_region(Buffer &buf, std::size_t sx, std::size_t sy, std::size_t ex, std::
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// The buffer's full text (rows joined by '\n', which reproduces the bytes).
|
||||||
|
static std::string
|
||||||
|
buffer_text(const Buffer &buf)
|
||||||
|
{
|
||||||
|
std::string out;
|
||||||
|
const std::size_t nrows = buf.Nrows();
|
||||||
|
for (std::size_t y = 0; y < nrows; ++y) {
|
||||||
|
if (y > 0)
|
||||||
|
out.push_back('\n');
|
||||||
|
out += buf.GetLineString(y);
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Replace the whole buffer's text as one undo group (one delete, one insert).
|
||||||
|
static void
|
||||||
|
replace_buffer_text(Buffer &buf, const std::string &old_text, const std::string &new_text, UndoSystem *u)
|
||||||
|
{
|
||||||
|
UndoGroupGuard group(u);
|
||||||
|
if (!old_text.empty()) {
|
||||||
|
buf.delete_text(0, 0, old_text.size());
|
||||||
|
if (u) {
|
||||||
|
buf.SetCursor(0, 0);
|
||||||
|
u->Begin(UndoType::Delete);
|
||||||
|
u->Append(std::string_view(old_text));
|
||||||
|
u->commit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!new_text.empty()) {
|
||||||
|
buf.insert_text(0, 0, new_text);
|
||||||
|
if (u) {
|
||||||
|
buf.SetCursor(0, 0);
|
||||||
|
u->Begin(UndoType::Insert);
|
||||||
|
u->Append(std::string_view(new_text));
|
||||||
|
u->commit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Replace the text of rows [y, y + old_text lines) in place with new_text,
|
// Replace the text of rows [y, y + old_text lines) in place with new_text,
|
||||||
// where old_text is those rows joined by '\n' (no trailing newline). Unlike
|
// where old_text is those rows joined by '\n' (no trailing newline). Unlike
|
||||||
// delete_row/insert_row this never adds or removes the newline after the
|
// delete_row/insert_row this never adds or removes the newline after the
|
||||||
@@ -662,40 +703,18 @@ insert_text_at_cursor(Buffer &buf, const std::string &text)
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::size_t cur_y = y;
|
std::size_t cur_y = y;
|
||||||
std::size_t cur_x = x;
|
std::size_t cur_x = std::min(x, buf.GetLineString(cur_y).size());
|
||||||
|
|
||||||
std::string remain = text;
|
// One insert: the piece table takes newlines directly. Inserting and
|
||||||
while (true) {
|
// splitting line by line (re-trimming the remaining text each time) was
|
||||||
auto pos = remain.find('\n');
|
// quadratic in the number of lines yanked.
|
||||||
if (pos == std::string::npos) {
|
buf.insert_text(static_cast<int>(cur_y), static_cast<int>(cur_x), text);
|
||||||
// insert remaining into current line
|
const std::size_t last_nl = text.rfind('\n');
|
||||||
nrows = buf.Nrows();
|
if (last_nl == std::string::npos) {
|
||||||
if (cur_y >= nrows) {
|
cur_x += text.size();
|
||||||
buf.insert_row(static_cast<int>(nrows), "");
|
} else {
|
||||||
}
|
cur_y += static_cast<std::size_t>(std::count(text.begin(), text.end(), '\n'));
|
||||||
const auto &rows = rows_of(buf);
|
cur_x = text.size() - last_nl - 1;
|
||||||
if (cur_x > rows[cur_y].size())
|
|
||||||
cur_x = rows[cur_y].size();
|
|
||||||
buf.insert_text(static_cast<int>(cur_y), static_cast<int>(cur_x), remain);
|
|
||||||
cur_x += remain.size();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
// insert segment before newline
|
|
||||||
std::string seg = remain.substr(0, pos);
|
|
||||||
{
|
|
||||||
const auto &rows = rows_of(buf);
|
|
||||||
if (cur_x > rows[cur_y].size())
|
|
||||||
cur_x = rows[cur_y].size();
|
|
||||||
}
|
|
||||||
buf.insert_text(static_cast<int>(cur_y), static_cast<int>(cur_x), seg);
|
|
||||||
// split line at cur_x + seg.size()
|
|
||||||
cur_x += seg.size();
|
|
||||||
buf.split_line(static_cast<int>(cur_y), static_cast<int>(cur_x));
|
|
||||||
// move to start of next line
|
|
||||||
cur_y += 1;
|
|
||||||
cur_x = 0;
|
|
||||||
// advance remain after newline
|
|
||||||
remain.erase(0, pos + 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
buf.SetCursor(cur_x, cur_y);
|
buf.SetCursor(cur_x, cur_y);
|
||||||
@@ -841,6 +860,13 @@ search_compute_matches_regex(const Buffer &buf, const std::string &pattern, std:
|
|||||||
// std::regex recurses per matched character; see RegexGuard.h.
|
// std::regex recurses per matched character; see RegexGuard.h.
|
||||||
kte::RunWithLargeStack([&] {
|
kte::RunWithLargeStack([&] {
|
||||||
for (std::size_t y = 0; y < rows.size(); ++y) {
|
for (std::size_t y = 0; y < rows.size(); ++y) {
|
||||||
|
// This runs on every prompt keystroke, and std::regex can be
|
||||||
|
// quadratic in line length for ordinary patterns (".*q" on a
|
||||||
|
// 40 KB minified line took over a minute, uninterruptibly), so
|
||||||
|
// incremental search skips very long lines. Regex replace, an
|
||||||
|
// explicit one-shot command, still processes every line.
|
||||||
|
if (rows[y].size() > kte::kRegexIncrementalLineLimit)
|
||||||
|
continue;
|
||||||
std::string line = static_cast<std::string>(rows[y]);
|
std::string line = static_cast<std::string>(rows[y]);
|
||||||
for (auto it = std::sregex_iterator(line.begin(), line.end(), rx);
|
for (auto it = std::sregex_iterator(line.begin(), line.end(), rx);
|
||||||
it != std::sregex_iterator(); ++it) {
|
it != std::sregex_iterator(); ++it) {
|
||||||
@@ -2461,6 +2487,14 @@ cmd_insert_text(CommandContext &ctx)
|
|||||||
int repeat = ctx.count > 0 ? ctx.count : 1;
|
int repeat = ctx.count > 0 ? ctx.count : 1;
|
||||||
std::size_t cx = x;
|
std::size_t cx = x;
|
||||||
std::size_t cy = y;
|
std::size_t cy = y;
|
||||||
|
{
|
||||||
|
constexpr std::size_t kMaxInsertBytes = std::size_t{256} << 20;
|
||||||
|
const std::size_t lines = buf->VisualLineActive() ? (buf->VisualLineEndY() - buf->VisualLineStartY() + 1) : 1;
|
||||||
|
if (ctx.arg.size() * static_cast<std::size_t>(repeat) * lines > kMaxInsertBytes) {
|
||||||
|
ctx.editor.SetStatus("Insert too large");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Visual-line mode: broadcast inserts to each selected line at the same column.
|
// Visual-line mode: broadcast inserts to each selected line at the same column.
|
||||||
if (buf->VisualLineActive()) {
|
if (buf->VisualLineActive()) {
|
||||||
@@ -2520,12 +2554,20 @@ cmd_insert_text(CommandContext &ctx)
|
|||||||
}
|
}
|
||||||
// Apply edits to the underlying PieceTable through Buffer::insert_text,
|
// Apply edits to the underlying PieceTable through Buffer::insert_text,
|
||||||
// not directly to the legacy rows_ cache. This ensures Save() persists text.
|
// not directly to the legacy rows_ cache. This ensures Save() persists text.
|
||||||
for (int i = 0; i < repeat; ++i) {
|
// A repeat count inserts the repeated text in one edit (one edit per
|
||||||
buf->insert_text(static_cast<int>(y), static_cast<int>(x), std::string_view(ctx.arg));
|
// repetition made C-u 1000000 x take minutes on a large file).
|
||||||
if (u)
|
std::string ins;
|
||||||
u->Append(std::string_view(ctx.arg));
|
if (repeat == 1) {
|
||||||
x += ctx.arg.size();
|
ins = ctx.arg;
|
||||||
|
} else {
|
||||||
|
ins.reserve(ctx.arg.size() * static_cast<std::size_t>(repeat));
|
||||||
|
for (int i = 0; i < repeat; ++i)
|
||||||
|
ins += ctx.arg;
|
||||||
}
|
}
|
||||||
|
buf->insert_text(static_cast<int>(y), static_cast<int>(x), std::string_view(ins));
|
||||||
|
if (u)
|
||||||
|
u->Append(std::string_view(ins));
|
||||||
|
x += ins.size();
|
||||||
buf->SetDirty(true);
|
buf->SetDirty(true);
|
||||||
buf->SetCursor(x, y);
|
buf->SetCursor(x, y);
|
||||||
ensure_cursor_visible(ctx.editor, *buf);
|
ensure_cursor_visible(ctx.editor, *buf);
|
||||||
@@ -2862,48 +2904,30 @@ cmd_newline(CommandContext &ctx)
|
|||||||
UndoSystem *u = buf->Undo();
|
UndoSystem *u = buf->Undo();
|
||||||
if (u)
|
if (u)
|
||||||
u->commit(); // end any pending batch
|
u->commit(); // end any pending batch
|
||||||
{
|
if (!find.empty()) {
|
||||||
// One undo step for the whole replace-all; EndGroup also commits the
|
// Build the replaced text in one pass and apply it as a single
|
||||||
// final node so later typing cannot coalesce into it.
|
// edit: one piece-table edit per match cost O(file) each, so
|
||||||
UndoGroupGuard group(u);
|
// replace-all was quadratic (298 s for 249k matches in 12 MB).
|
||||||
// The prompt input is single-line, so the row count is stable. Keep a
|
const std::string before = buffer_text(*buf);
|
||||||
// local copy of each line in step with the edits instead of
|
std::string after;
|
||||||
// re-fetching (and rebuilding) Rows() after every replacement.
|
after.reserve(before.size());
|
||||||
const std::size_t nrows = buf->Nrows();
|
std::size_t pos = 0;
|
||||||
for (std::size_t y = 0; y < nrows && !find.empty(); ++y) {
|
while (true) {
|
||||||
std::string line = buf->GetLineString(y);
|
// Matches never span lines: the prompt text has no newline.
|
||||||
std::size_t pos = 0;
|
const std::size_t p = before.find(find, pos);
|
||||||
while (true) {
|
if (p == std::string::npos)
|
||||||
std::size_t p = line.find(find, pos);
|
break;
|
||||||
if (p == std::string::npos)
|
after.append(before, pos, p - pos);
|
||||||
break;
|
after += with;
|
||||||
// Delete matched segment from the piece table
|
pos = p + find.size();
|
||||||
buf->delete_text(static_cast<int>(y), static_cast<int>(p), find.size());
|
++total;
|
||||||
if (u) {
|
|
||||||
buf->SetCursor(p, y);
|
|
||||||
u->Begin(UndoType::Delete);
|
|
||||||
u->Append(std::string_view(find));
|
|
||||||
}
|
|
||||||
// Insert replacement if provided
|
|
||||||
if (!with.empty()) {
|
|
||||||
buf->insert_text(static_cast<int>(y), static_cast<int>(p),
|
|
||||||
std::string_view(with));
|
|
||||||
if (u) {
|
|
||||||
buf->SetCursor(p, y);
|
|
||||||
u->Begin(UndoType::Insert);
|
|
||||||
u->Append(std::string_view(with));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
line.replace(p, find.size(), with);
|
|
||||||
// Resume just past the replacement. For an empty replacement that
|
|
||||||
// is `p` itself (not p+1), which catches adjacent matches, e.g.
|
|
||||||
// "aaaa" -> "" replacing "aa".
|
|
||||||
pos = p + with.size();
|
|
||||||
++total;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
after.append(before, pos, std::string::npos);
|
||||||
|
if (total > 0)
|
||||||
|
replace_buffer_text(*buf, before, after, u);
|
||||||
}
|
}
|
||||||
buf->SetDirty(true);
|
if (total > 0)
|
||||||
|
buf->SetDirty(true);
|
||||||
// Restore original cursor, clamped: replacements may have shortened the line.
|
// Restore original cursor, clamped: replacements may have shortened the line.
|
||||||
if (orig_y < buf->Nrows())
|
if (orig_y < buf->Nrows())
|
||||||
buf->SetCursor(orig_x, orig_y);
|
buf->SetCursor(orig_x, orig_y);
|
||||||
@@ -3335,22 +3359,29 @@ cmd_newline(CommandContext &ctx)
|
|||||||
std::size_t nrows = buf->Nrows();
|
std::size_t nrows = buf->Nrows();
|
||||||
if (nrows > 1 && buf->GetLineString(nrows - 1).empty())
|
if (nrows > 1 && buf->GetLineString(nrows - 1).empty())
|
||||||
--nrows;
|
--nrows;
|
||||||
// std::regex recurses per matched character; see RegexGuard.h. The
|
// Build the new text line by line and apply it as one edit (one
|
||||||
// caller waits for the worker, so the buffer is never shared.
|
// edit per changed line was quadratic on big files). std::regex
|
||||||
|
// recurses per matched character, so match on a large stack
|
||||||
|
// (RegexGuard.h).
|
||||||
|
const std::size_t all_rows = buf->Nrows();
|
||||||
|
std::string after;
|
||||||
kte::RunWithLargeStack([&] {
|
kte::RunWithLargeStack([&] {
|
||||||
for (std::size_t y = 0; y < nrows; ++y) {
|
for (std::size_t y = 0; y < all_rows; ++y) {
|
||||||
const std::string before = buf->GetLineString(y);
|
if (y > 0)
|
||||||
const std::string after = std::regex_replace(before, rx, repl);
|
after.push_back('\n');
|
||||||
if (after != before) {
|
const std::string before_line = buf->GetLineString(y);
|
||||||
replace_rows_text(*buf, y, before, after, ru);
|
if (y >= nrows) {
|
||||||
// A replacement containing newlines adds rows; skip past them.
|
after += before_line; // the empty row after a final '\n'
|
||||||
const auto added = static_cast<std::size_t>(std::count(after.begin(), after.end(), '\n'));
|
continue;
|
||||||
y += added;
|
|
||||||
nrows += added;
|
|
||||||
++changed;
|
|
||||||
}
|
}
|
||||||
|
const std::string replaced = std::regex_replace(before_line, rx, repl);
|
||||||
|
if (replaced != before_line)
|
||||||
|
++changed;
|
||||||
|
after += replaced;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
if (changed > 0)
|
||||||
|
replace_buffer_text(*buf, buffer_text(*buf), after, ru);
|
||||||
clamp_cursor_to_buffer(*buf);
|
clamp_cursor_to_buffer(*buf);
|
||||||
buf->SetDirty(true);
|
buf->SetDirty(true);
|
||||||
ctx.editor.SetStatus("Regex replaced in " + std::to_string(changed) + " line(s)");
|
ctx.editor.SetStatus("Regex replaced in " + std::to_string(changed) + " line(s)");
|
||||||
|
|||||||
+6
-3
@@ -12,9 +12,12 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace kte {
|
namespace kte {
|
||||||
// Renderers highlight regex matches only on lines up to this many bytes (to
|
// Renderers highlight regex matches only on lines up to this many bytes, and
|
||||||
// bound per-frame work); longer lines are drawn without match highlighting.
|
// incremental regex search skips longer lines: std::regex can take time
|
||||||
constexpr std::size_t kRegexRenderLineLimit = 1000000;
|
// quadratic in line length even for ordinary patterns, with no way to
|
||||||
|
// interrupt it, and these run on every frame or keystroke.
|
||||||
|
constexpr std::size_t kRegexRenderLineLimit = 10000;
|
||||||
|
constexpr std::size_t kRegexIncrementalLineLimit = 20000;
|
||||||
|
|
||||||
// Run fn to completion on a thread with a large stack (address space is
|
// Run fn to completion on a thread with a large stack (address space is
|
||||||
// reserved, pages are committed only as used), so regex work over long lines
|
// reserved, pages are committed only as used), so regex work over long lines
|
||||||
|
|||||||
@@ -1146,3 +1146,55 @@ TEST(Audit_VirtualBuffer_SavedAs_GetsJournal)
|
|||||||
ASSERT_TRUE(std::filesystem::exists(swp));
|
ASSERT_TRUE(std::filesystem::exists(swp));
|
||||||
std::filesystem::remove(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(""));
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user