Undo: hold large texts as storage spans; byte budget with pruning
- Undo texts of 4 KiB or more (Insert/Paste from the buffer, Delete from the spans the piece table remembers for its last large deletion) are held as TextSpan references into the piece table's append-only storage, after checking they hold exactly the recorded bytes. Undo and redo insert them with Buffer::insert_spans, which adds pieces over the stored bytes instead of copying them into storage again (the journal still gets the bytes, as one record). A replace-all session on a 26 MB file peaks at 181 MB instead of 332 MB; undo/redo cycles of a 6.6 MB kill no longer grow memory by 6.6 MB each. - UndoSystem keeps a byte budget (default max(64 MiB, twice the buffer size); SetByteBudget). Over it, history is pruned to 75% of the budget: branches off the current path, then the oldest edits (an undo group goes whole, and never the current state's group), then the redo line. When the saved state is dropped the buffer stays modified until saved. - replace_all_bytes clears undo history, which was recorded against (and may reference the storage of) the previous content. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015rcVQiiWyTzoNrsvLSaAfB
This commit is contained in:
@@ -972,10 +972,54 @@ Buffer::replace_all_bytes(const std::string_view bytes)
|
||||
if (!bytes.empty())
|
||||
content_.Append(bytes.data(), bytes.size());
|
||||
rows_cache_dirty_ = true;
|
||||
if (undo_sys_)
|
||||
undo_sys_->clear();
|
||||
MarkContentChanged();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Buffer::insert_spans(int row, int col, const std::vector<TextSpan> &spans)
|
||||
{
|
||||
if (row < 0)
|
||||
row = 0;
|
||||
if (col < 0)
|
||||
col = 0;
|
||||
const std::size_t off = content_.LineColToByteOffset(static_cast<std::size_t>(row),
|
||||
static_cast<std::size_t>(col));
|
||||
content_.InsertSpans(off, spans);
|
||||
rows_cache_dirty_ = true;
|
||||
edited_at_(row);
|
||||
if (swap_rec_) {
|
||||
// The journal needs the bytes as one record (see insert_row).
|
||||
std::string text;
|
||||
content_.VisitSpans(spans, [&](const char *d, std::size_t n) {
|
||||
text.append(d, n);
|
||||
});
|
||||
if (!text.empty())
|
||||
swap_rec_->OnInsert(row, col, text);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::vector<TextSpan>
|
||||
Buffer::SpansAt(int row, int col, std::size_t len) const
|
||||
{
|
||||
const std::size_t off = content_.LineColToByteOffset(static_cast<std::size_t>(std::max(row, 0)),
|
||||
static_cast<std::size_t>(std::max(col, 0)));
|
||||
return content_.SpansInRange(off, len);
|
||||
}
|
||||
|
||||
|
||||
std::vector<TextSpan>
|
||||
Buffer::TakeDeletedSpans(int row, int col, std::size_t len)
|
||||
{
|
||||
const std::size_t off = content_.LineColToByteOffset(static_cast<std::size_t>(std::max(row, 0)),
|
||||
static_cast<std::size_t>(std::max(col, 0)));
|
||||
return content_.TakeDeletedSpans(off, len);
|
||||
}
|
||||
|
||||
|
||||
// Undo system accessors
|
||||
UndoSystem *
|
||||
Buffer::Undo()
|
||||
|
||||
Reference in New Issue
Block a user