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:
+28
@@ -1,6 +1,9 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "TextSpan.h"
|
||||
|
||||
|
||||
enum class UndoType : std::uint8_t {
|
||||
@@ -24,7 +27,32 @@ struct UndoNode {
|
||||
int col{};
|
||||
std::uint64_t group_id = 0; // 0 means ungrouped; non-zero means undo/redo as an atomic group
|
||||
std::string text;
|
||||
// A large Insert/Paste/Delete text is held as references to the buffer's
|
||||
// storage instead (text is then empty and span_len is its length): undo
|
||||
// of a replace-all over a 100 MB file otherwise kept 200 MB of copies.
|
||||
std::vector<TextSpan> spans;
|
||||
std::size_t span_len = 0;
|
||||
UndoNode *parent = nullptr; // previous state; null means pre-first-edit
|
||||
UndoNode *child = nullptr; // next in current timeline
|
||||
UndoNode *next = nullptr; // redo branch
|
||||
|
||||
|
||||
[[nodiscard]] bool HasSpans() const
|
||||
{
|
||||
return !spans.empty();
|
||||
}
|
||||
|
||||
|
||||
// Length of the node's text, however it is held.
|
||||
[[nodiscard]] std::size_t TextSize() const
|
||||
{
|
||||
return HasSpans() ? span_len : text.size();
|
||||
}
|
||||
|
||||
|
||||
// Approximate heap and object bytes held by this node.
|
||||
[[nodiscard]] std::size_t CostBytes() const
|
||||
{
|
||||
return sizeof(UndoNode) + text.capacity() + spans.capacity() * sizeof(TextSpan);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user