summaryrefslogtreecommitdiff
path: root/src/rollback.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-29 23:14:10 -0300
committerGabriel Schneider <[email protected]>2026-10-29 23:59:54 -0300
commitbce88593014a4078dc9259855944f9ddbc19fe54 (patch)
tree9be0950d36a63cec15552dccec3cd29550b2f26c /src/rollback.zig
parentc02ab55482c055c57146b9ea9a5a78c20bcdf122 (diff)
downloadpardes-bce88593014a4078dc9259855944f9ddbc19fe54.tar.gz
pardes-bce88593014a4078dc9259855944f9ddbc19fe54.zip
views and the rollback copy: a live view's slot keeps no pointer into what the live core owns; a register's texts and a file's location rows are pinned, not copied
views.zig: when a view goes live (to, close, drawOthers putting the live one back, only), its parked share is emptied (`emptied`): it was left holding copies of the pointers it handed the live fields (a column tag's text, a chord's argument, the presentation's), stale until it parked again. The rollback copy walks every slot: a stale pointer whose memory the live view had freed and another taken saved that other's bytes and put them back. A pane's share holds no pointer but its selections, which `unpark` already hands over. The test fails without it: a tag's text freed under view 0's stale slot, its memory taken by an allocation the copy has no business with, written under a held copy, and the copy put back. Registers.zig, locations.zig, rollback.zig: a register's values and joined text, and a file's location rows (the dropped ones, a query's saved), are replaced whole and never written, so the copy pins them as it does a file's text, freed raw (a safe build's free would write over one a copy still holds); rows pin whole with what each owns (`pinnedDeep`). In the harness at 30 ms the big copies were a file's location rows, 5-6 MB after the flood, not the register. ReleaseFast, 20 seeds, three clients, p99: copy 5.1 -> 0.22 ms at 30 ms (0.19-0.20 at 80 and 207); rollback 14.9 -> 9.5 ms at 30 ms (1.3-1.4 at 80 and 207), what is left being the replay's digests (half the harness's CPU is Wyhash over the whole core per replayed unit). Tests: a 2 MB register and 500 location rows pinned, not saved, and whole after a speculation drops them and the copy is put back. docs/next-daemon.md: the follow-ups. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src/rollback.zig')
-rw-r--r--src/rollback.zig123
1 files changed, 116 insertions, 7 deletions
diff --git a/src/rollback.zig b/src/rollback.zig
index 79b8202e..a7027da6 100644
--- a/src/rollback.zig
+++ b/src/rollback.zig
@@ -11,10 +11,11 @@
//! - `Copy`, the walker's walk of the core's state (src/walk.zig: what a
//! digest covers, so views, buffers, registers, counters and terminal
//! metadata), saving the bytes of every allocation it reaches, the core
-//! struct's own included. A file's text, its line index and its undo
-//! snapshots are pinned instead: an edit puts a new buffer in their
-//! place (File.setContentSpan) and never writes one, so the copy keeps
-//! a reference and the put-off free keeps it alive.
+//! struct's own included. A file's text, its line index, its undo
+//! snapshots and a register's texts are pinned instead: an edit puts a
+//! new buffer in their place (File.setContentSpan, Registers.put) and
+//! never writes one, so the copy keeps a reference and the put-off free
+//! keeps it alive.
//!
//! Putting the copy back writes each saved allocation's bytes over it, so
//! every pointer in them points where it did, at what was there; then the
@@ -197,6 +198,8 @@ pub const Copy = struct {
/// of the blobs: a file's text or an undo snapshot's.
where: []const u8 = "",
pinnable: bool = false,
+ /// Whether it is a blob of rows that own allocations of their own.
+ deep: bool = false,
const Region = struct { addr: usize, off: usize, len: usize };
const Pin = struct { addr: usize, len: usize, hash: u64, where: []const u8 };
@@ -342,6 +345,7 @@ pub const Copy = struct {
if (comptime opaqueTo(F)) continue;
c.where = @typeName(T) ++ "." ++ name;
c.pinnable = comptime pinned(T, name);
+ c.deep = comptime pinnedDeep(T, name);
if (comptime walk.lengthOf(T, name) != null and @typeInfo(F) == .array) {
const len = comptime walk.lengthOf(T, name).?;
if (comptime heapIn(@typeInfo(F).array.child, 0)) for (@field(v, name)[0..walk.lengthIn(v, len)]) |*e| try c.visit(@typeInfo(F).array.child, e);
@@ -394,11 +398,14 @@ pub const Copy = struct {
if (s.len == 0) return;
const addr = @intFromPtr(s.ptr);
if (try c.reached([]const E, addr)) return;
- if (comptime !heapIn(E, 0)) if (c.pinnable and s.len * @sizeOf(E) >= blob_min and c.epoch.?.owns(addr) != null) {
+ const flat = comptime !heapIn(E, 0);
+ if ((if (flat) c.pinnable else c.deep) and s.len * @sizeOf(E) >= blob_min and c.epoch.?.owns(addr) != null) {
+ // A blob of rows with allocations of their own pinned whole:
+ // they are as immutable as it, and kept alive with it.
const bytes = std.mem.sliceAsBytes(s);
try c.pins.append(c.meta, .{ .addr = addr, .len = bytes.len, .hash = if (checked) std.hash.Wyhash.hash(0, bytes) else 0, .where = c.where });
return;
- };
+ }
_ = try c.allocation(addr);
if (comptime heapIn(E, 0)) for (s) |*e| try c.visit(E, e);
}
@@ -409,7 +416,21 @@ pub const Copy = struct {
fn pinned(comptime T: type, comptime name: []const u8) bool {
const File = pardes.panes.File;
return (T == File.State and (std.mem.eql(u8, name, "content") or std.mem.eql(u8, name, "line_starts"))) or
- (T == File.Snapshot and std.mem.eql(u8, name, "content"));
+ (T == File.Snapshot and std.mem.eql(u8, name, "content")) or
+ // A register's values and joined text: a yank of a terminal's whole
+ // grid is megabytes, which every speculation would otherwise copy.
+ (T == pardes.Registers.Reg and (std.mem.eql(u8, name, "values") or std.mem.eql(u8, name, "joined")));
+}
+
+/// Blobs of rows that own allocations of their own (a location's path, its
+/// colours), the array and they replaced whole and never written, freed raw
+/// (locations.freeRows): a file's location rows (5 MB after a flood of
+/// search results), the ones an edit dropped, a query's saved.
+fn pinnedDeep(comptime T: type, comptime name: []const u8) bool {
+ const File = pardes.panes.File;
+ return (T == File.State and std.mem.eql(u8, name, "location_rows")) or
+ (T == File.DroppedMarks and std.mem.eql(u8, name, "rows")) or
+ (T == pardes.panes.Query.Saved and std.mem.eql(u8, name, "rows"));
}
fn annotatedStruct(comptime T: type) bool {
@@ -497,3 +518,91 @@ test "a copy put back is the core as it was; one let go keeps what was done" {
p.deinit();
try std.testing.expectEqual(@as(usize, 0), e.live.count());
}
+
+test "a view's slot while it is live keeps no pointer a rollback could put old bytes back through" {
+ // The C heap: a freed chunk comes back to the next allocation its size.
+ var e: Epoch = .{};
+ defer e.deinit();
+ const gpa = e.allocator();
+ const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml", .image_allocator = gpa, .pdf_allocator = gpa, .tree_sitter_allocator = gpa, .frame_allocator = gpa });
+ defer p.deinit();
+ p.pump(.{}) catch {};
+ _ = p.openView() orelse return error.NoView;
+ // View 0's column tag has a text of its own; out to view 1 and back.
+ const text = "the column tag of view 0, its own";
+ p.col_tags[0].own = try gpa.dupe(u8, text);
+ p.toView(1);
+ p.toView(0);
+ // The live view drops it, and its memory is another's: something the
+ // copy has no business saving.
+ const was = p.col_tags[0].own.?;
+ gpa.free(was);
+ p.col_tags[0].own = null;
+ const other = try gpa.alloc(u8, text.len);
+ defer gpa.free(other);
+ if (other.ptr != was.ptr) return error.SkipZigTest; // the heap handed out another
+ @memset(other, 'a');
+ var c: Copy = .{};
+ defer c.deinit();
+ try c.take(&e, p);
+ // Written in place while the copy is held, then the copy put back.
+ @memset(other, 'b');
+ c.restore();
+ try std.testing.expect(std.mem.allEqual(u8, other, 'b'));
+}
+
+test "a register's texts are pinned, not copied, and come back whole" {
+ var e: Epoch = .{ .child = std.testing.allocator };
+ defer e.deinit();
+ const gpa = e.allocator();
+ const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml", .image_allocator = gpa, .pdf_allocator = gpa, .tree_sitter_allocator = gpa, .frame_allocator = gpa });
+ p.pump(.{}) catch {};
+ // A terminal's whole grid yanked: megabytes, and joined once read.
+ const grid = try std.testing.allocator.alloc(u8, 2 << 20);
+ defer std.testing.allocator.free(grid);
+ @memset(grid, 'y');
+ const reg = pardes.Registers.default;
+ p.registers.put(gpa, reg, grid, 0, 1, true);
+ _ = p.registers.text(gpa, reg) orelse return error.NoText;
+ var c: Copy = .{};
+ defer c.deinit();
+ try c.take(&e, p);
+ try std.testing.expect(c.savedBytes() < 1 << 20);
+ try std.testing.expect(c.pinnedBytes() >= 4 << 20);
+ // A speculation yanks over it, and is undone.
+ p.registers.put(gpa, reg, "a word", 0, 1, true);
+ c.restore();
+ const back = p.registers.value(reg, 0) orelse return error.Lost;
+ try std.testing.expectEqual(grid.len, back.len);
+ try std.testing.expect(std.mem.allEqual(u8, back, 'y'));
+ p.deinit();
+ try std.testing.expectEqual(@as(usize, 0), e.live.count());
+}
+
+test "a file's location rows are pinned whole, with what they own, and come back" {
+ const locations = @import("locations.zig");
+ var e: Epoch = .{ .child = std.testing.allocator };
+ defer e.deinit();
+ const gpa = e.allocator();
+ const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml", .image_allocator = gpa, .pdf_allocator = gpa, .tree_sitter_allocator = gpa, .frame_allocator = gpa });
+ p.pump(.{}) catch {};
+ const f = for (p.panes) |slot| {
+ if (slot) |pane| if (pane.file) |*file| break file;
+ } else return error.NoFile;
+ // A search's rows: each its own path.
+ const rows = try gpa.alloc(locations.Row, 500);
+ for (rows) |*row| row.* = .{ .kind = .match, .path = try gpa.dupe(u8, "src/rollback.zig"), .at = .{}, .location_end = 0, .code_start = 0 };
+ f.location_rows = rows;
+ var c: Copy = .{};
+ defer c.deinit();
+ try c.take(&e, p);
+ try std.testing.expect(c.pinnedBytes() >= rows.len * @sizeOf(locations.Row));
+ // A speculation's new search drops them, and is undone.
+ locations.freeRows(gpa, f.location_rows);
+ f.location_rows = &.{};
+ c.restore();
+ try std.testing.expectEqual(@as(usize, 500), f.location_rows.len);
+ try std.testing.expectEqualStrings("src/rollback.zig", f.location_rows[499].path);
+ p.deinit();
+ try std.testing.expectEqual(@as(usize, 0), e.live.count());
+}