From bce88593014a4078dc9259855944f9ddbc19fe54 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 23:14:10 -0300 Subject: 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 --- docs/next-daemon.md | 44 ++++++++++++++++--- src/Registers.zig | 13 ++++-- src/locations.zig | 9 ++-- src/rollback.zig | 123 +++++++++++++++++++++++++++++++++++++++++++++++++--- src/views.zig | 16 +++++++ 5 files changed, 184 insertions(+), 21 deletions(-) diff --git a/docs/next-daemon.md b/docs/next-daemon.md index 00f66cf8..55921027 100644 --- a/docs/next-daemon.md +++ b/docs/next-daemon.md @@ -1743,7 +1743,7 @@ cores (ReleaseFast). the copy's p99 is 7.8 ms and a rollback's 17 ms: a register holding a terminal's whole grid (`%` then `y` in a shell's text, 2 MB after the flood) is copied whole by every speculation. Registers could be pinned - as file texts are; left as found. + as file texts are. (Done since: the follow-ups below.) - **No local regression.** `pardes-perf-live --live`, A and the new tty interleaved A B A ... A (4 runs of the new one, 5 of A) on the perf cores; the median of the runs' p50s, new/A, present and core: @@ -1782,12 +1782,8 @@ Not done, and why: - Text reads outside `buildRows` (a pointer selection's copy in tty mode, 9P's screen reads) do not ask for the rows; 9P's are the authority's anyway. -- **A hazard for part A, not fixed here.** The rollback copy follows a - view's stale slot (above). If a stale pointer lands on a live allocation - that a speculation then writes in place, the rollback puts the old bytes - back over that allocation's new owner. Emptying a slot's pointers when - its view goes live (views.zig `unpark`) would end it; the copy skipping - the live slot instead diverged, so part of it is still read. +- **A hazard for part A** (fixed since, below): the rollback copy followed + a view's stale slot. - Echo prediction (phase 6): a key into a terminal waits a round trip. - A replica steps every output step of the authority's (bytes-less), about 2000 a second under a flood, each a core step. @@ -1796,6 +1792,40 @@ Not done, and why: - The other agent wires it in: the tty and GUI hosts, `Screens` per attached peer, and a replica frontend drawing after `screen`. +Follow-ups (2026-10-29): +- **A live view's slot keeps no pointer.** When a view goes live (`to`, + `close`, `only`, and `drawOthers` putting the live one back), its parked + share is emptied (views.zig `emptied`): it held the pointers it handed + the live fields (a column tag's text, a chord's argument, the + presentation's) until it parked again, and the copy, walking every + slot, saved and put back whatever memory had since taken their place. + A pane's share holds no pointer but its selections, which `unpark` + already hands over. A test frees a tag's text under the stale slot, + hands its memory to another allocation, writes it under a held copy and + puts the copy back: it fails without the fix. +- **Registers and location rows are pinned, not copied.** A register's + values and joined text, and a file's location rows (with the ones an + edit dropped and a query's saved), are replaced whole and never written, + so the copy pins them as it does a file's text, freed raw. Rows pin + whole with what each owns (its path, its colours: `pinnedDeep`). In the + harness at 30 ms the big copies were not the register but a file's + location rows, 5 to 6 MB after the flood. ReleaseFast, 20 seeds, three + clients, p99: copy 5.1 ms to 0.22 ms at 30 ms (0.19 to 0.20 ms at 80 and + 207); rollback 14.9 to 9.5 ms at 30 ms (1.3 to 1.4 ms at 80 and 207). + What is left of a rollback's tail is the replay's digests: half the + harness's CPU is Wyhash over the whole core at every replayed unit, the + pinned blobs included (part A's digest). Hashing a pinned blob once, + while it lives, would take most of it. +- **The live perf harness ends what it starts.** Every process a live run + starts inherits its private runtime directory: after each cell, and in + the cleanup every path takes, the harness sends TERM, then KILL, to each + whose environment names it (`liveSweep`). With a wrapper that leaves a + daemon-kept detached session (as the remote agent's client-live.sh + does), the old harness left three sessions and their daemon a run; this + one leaves none. The gate's baselines are recorded again from base (main + and the new perf.zig), and .scratch/daemon/base/pardes-perf-live rebuilt + from it. + ## Phase 6: upgrade notes Built 2026-10-29 (`src/upgrade.zig`, the tty and GUI hosts, `daemon.zig`, diff --git a/src/Registers.zig b/src/Registers.zig index 5e23da5b..760aff9a 100644 --- a/src/Registers.zig +++ b/src/Registers.zig @@ -5,12 +5,17 @@ //! computed from the editor rather than stored (`_`, `#`, `.`, `%`) and the //! clipboard's paste side are read in edit.zig, which has the pane at hand. const std = @import("std"); +const freeRaw = @import("memory.zig").freeRaw; const Registers = @This(); list: std.ArrayList(Reg) = .empty, last_search: u21 = '/', +/// A value and the joined text are replaced whole, never written in place, +/// so a replica's rollback copy pins them as it does a file's text +/// (src/rollback.zig `pinned`), and they are freed raw: a safe build's +/// free would write over one the copy still holds. pub const Reg = struct { name: u21, /// gpa-owned, one per range, in document order @@ -30,9 +35,9 @@ pub fn deinit(r: *Registers, gpa: std.mem.Allocator) void { } fn freeReg(gpa: std.mem.Allocator, reg: *Reg) void { - for (reg.values) |v| gpa.free(v); + for (reg.values) |v| freeRaw(gpa, v); gpa.free(reg.values); - if (reg.joined) |j| gpa.free(j); + if (reg.joined) |j| freeRaw(gpa, j); } pub fn get(r: *Registers, name: u21) ?*Reg { @@ -69,9 +74,9 @@ pub fn put(r: *Registers, gpa: std.mem.Allocator, name: u21, bytes: []const u8, freeReg(gpa, reg); reg.* = .{ .name = name, .values = values }; } - gpa.free(reg.values[i]); + freeRaw(gpa, reg.values[i]); reg.values[i] = copy; - if (reg.joined) |j| gpa.free(j); + if (reg.joined) |j| freeRaw(gpa, j); reg.joined = null; } diff --git a/src/locations.zig b/src/locations.zig index cdb228a6..40f22f85 100644 --- a/src/locations.zig +++ b/src/locations.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const freeRaw = @import("memory.zig").freeRaw; const pardes = @import("pardes.zig"); const look = @import("look.zig"); const syntax = @import("syntax.zig"); @@ -25,12 +26,14 @@ pub const Row = struct { colors: []u8 = &.{}, }; +/// Raw (no safe build's overwrite): a replica's rollback copy pins a row +/// blob and what its rows own (src/rollback.zig `pinnedDeep`). pub fn freeRows(gpa: std.mem.Allocator, rows: []Row) void { for (rows) |row| { - gpa.free(row.path); - gpa.free(row.colors); + freeRaw(gpa, row.path); + freeRaw(gpa, row.colors); } - gpa.free(rows); + freeRaw(gpa, rows); } /// A list of rows being built, given up: each row's own bytes, then the list. 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()); +} diff --git a/src/views.zig b/src/views.zig index bf9b9147..b9760143 100644 --- a/src/views.zig +++ b/src/views.zig @@ -215,6 +215,7 @@ pub fn undumped(p: *Pardes, others: []const Dumped) !void { /// for one client (a Restore, `-l`) is one view. pub fn only(p: *Pardes) void { for (0..max) |k| if (k != p.view) close(p, @intCast(k)); + emptied(p, p.view); const k = p.view; if (k == 0) return; std.mem.swap(?*Parked, &p.parked[0], &p.parked[k]); @@ -390,6 +391,19 @@ pub fn to(p: *Pardes, k: u8) void { if (k == p.view) return; park(p); unpark(p, k); + emptied(p, k); +} + +/// View `k` is live: its slot, stale until it parks again (written whole +/// then), keeps no pointer to what it handed the live fields. A rollback +/// copy walks every slot (src/rollback.zig): one left there, freed by the +/// live view and the memory another's since, would be saved and its old +/// bytes put back over the other's. (A pane's share holds no pointer but +/// its selections, which `unpark` hands over.) +fn emptied(p: *Pardes, k: u8) void { + const slot = p.parked[k] orelse return; + slot.view = .{ .screen_w = p.screen_w, .screen_h = p.screen_h }; + slot.view.col_tags = @splat(.{ .what = .column, .gpa = p.gpa }); } /// A new view, as the live one is laid out and focused, with its panes @@ -431,6 +445,7 @@ pub fn close(p: *Pardes, k: u8) void { } else unreachable; park(p); unpark(p, other); + emptied(p, other); } const v = &p.parked[k].?.view; for (&v.col_tags) |*t| t.deinit(p.gpa); @@ -478,6 +493,7 @@ pub fn drawOthers(p: *Pardes, h: pardes.Host) !void { park(p); defer { copy(&fields, p, &p.parked[live].?.view); + emptied(p, live); p.view = live; for (p.panes) |s| if (s) |pane| { const pv = &pane.pv[live]; -- cgit v1.3