diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-21 17:31:25 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-21 19:27:50 -0300 |
| commit | 71a68eac9d4bce7deb19236a9cc8338b1a37caa1 (patch) | |
| tree | 9c667f806046f528cc295ef397a954ae67ced107 | |
| parent | 2b9da7be7d9520701b24d46d8db2ec56517b45b8 (diff) | |
| download | pardes-71a68eac9d4bce7deb19236a9cc8338b1a37caa1.tar.gz pardes-71a68eac9d4bce7deb19236a9cc8338b1a37caa1.zip | |
layers: with `Changes` off a client's key pays for items A and B no more: all of jj's session state is one field, `Pardes.changes: jj.Session` (jj's answers by pane serial, the `Changes` jj runs for, the one asked meanwhile, how many were asked), whose digest is nothing until a `Changes` is asked and then one update while nothing is asked of jj, where item A had three `Stream` fields (three updates in every digest of the session, and a client's key takes several digests) beside round 4's answers list (one more); `Stream` holds nothing of jj's again (its dispatch lines stay), the session's end lets the worker and the asks go in `Session.deinit`, and the batch's 8-byte `jj` field goes (a `Changes` answer is told by its generation's bit, `jj.gen_bit`, after a paint's, and the pane it is for is the ask's, which every replica holds); measured first, a variant at a time, client-key-down against tree3's pardes-perf in the gate's own layout (ReleaseFast gui, its farms and HOME; my first script's layout read about 0.05% lower): item B as it was small +0.111%, the batch's field gone and the stream's three fields one +0.104%, the session's state one field digested as nothing until asked small +0.047%, large +0.047%, medium +0.010%
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
| -rw-r--r-- | core/Core.zig | 10 | ||||
| -rw-r--r-- | core/File.zig | 2 | ||||
| -rw-r--r-- | core/Stream.zig | 15 | ||||
| -rw-r--r-- | core/jj.zig | 125 |
4 files changed, 83 insertions, 69 deletions
diff --git a/core/Core.zig b/core/Core.zig index 177a6e78..efa2ac3d 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -355,10 +355,11 @@ lsp_ask: Ask = .{}, pipe: Pipe = .{}, /// A query pane's Grep, and its Find's listing, on a worker (Stream.zig). stream: Stream = .{}, -/// What jj answered for each pane's `Changes` (jj.zig), by its serial: the -/// session's list, not a field of every pane (a digest and a rollback copy -/// walk every pane's fields at every step). -changes: std.ArrayList(@import("jj.zig").Changes) = .empty, +/// The panes' `Changes` (jj.zig): what was asked of jj, and what it +/// answered for each pane, by its serial: the session's, not a field of +/// every pane (a digest and a rollback copy walk every pane's fields at +/// every step), and nothing in a digest until a `Changes` is asked. +changes: @import("jj.zig").Session = .{}, fs: filesystem.Namespace = .{}, boot: Boot = .shell, @@ -868,7 +869,6 @@ pub fn deinit(p: *Pardes) void { for (p.spawn_dirs) |d| if (d) |dir| gpa.free(dir); p.pipe.deinit(gpa); p.stream.deinit(gpa); - for (p.changes.items) |*c| c.deinit(gpa); p.changes.deinit(gpa); p.handover.deinit(gpa); if (p.stream.ahead.len > 0) p.tree_sitter_gpa.free(p.stream.ahead); diff --git a/core/File.zig b/core/File.zig index ae052344..8749417f 100644 --- a/core/File.zig +++ b/core/File.zig @@ -757,7 +757,7 @@ fn edit(p: *Pardes, f: *State, off: usize, len: usize, bytes_in: []const u8, who }; reportEdit(p, f, off, len, bytes); // A pinned change's per-line values ride with the lines (jj.zig). - if (p.changes.items.len > 0) @import("jj.zig").edited(p, f, off, len, bytes); + if (p.changes.answers.items.len > 0) @import("jj.zig").edited(p, f, off, len, bytes); if (f.mini) |*mini| mini.deinit(p.gpa); f.mini = null; if (f.line_starts.len > 0) shiftIndex(p.gpa, f, off, len, bytes); diff --git a/core/Stream.zig b/core/Stream.zig index 80812840..1d061b52 100644 --- a/core/Stream.zig +++ b/core/Stream.zig @@ -74,11 +74,9 @@ pub const Batch = struct { /// A paint's answer (`paintAhead`): the colours of pane `serial`'s text /// at `revision` from byte `start`, one syntax.Syn a byte. painted: ?struct { serial: u32, revision: u32, start: usize } = null, + /// Or, with a `Changes` generation (jj.gen_bit), jj's annotate (jj's + /// log in `rows`, or its refusal and `end` failed). styles: []u8 = &.{}, - /// A `Changes` answer (jj.zig) for pane `jj`, by serial: jj's log in - /// `rows` and its annotate in `styles`, or its refusal in `rows` and - /// `end` failed. - jj: ?u32 = null, pub const End = enum { done, failed }; @@ -259,14 +257,7 @@ rows_asked_at: u8 = 0, /// Rows asked for their colours while a 9P write was served: asked again /// on the next step (Query.produceAll). rows_later: bool = false, -/// The `Changes` asked of jj and its worker, the one asked while it ran, -/// and how many were asked (jj.zig: one at a time, the last one pinned). -changes: ?jj.Task = null, -changes_next: ?*jj.Ask = null, -changes_count: u32 = 0, - pub fn deinit(s: *Stream, gpa: std.mem.Allocator) void { - jj.cancel(s); s.cancelTask(); s.cancelParse(); s.cancelPaint(); @@ -700,8 +691,8 @@ const Collect = struct { pub fn receive(p: *Pardes, b: *const Batch) void { const s = &p.stream; if (b.parsed) |parsed| return receiveParse(p, b, parsed.serial, parsed.revision); - if (b.jj) |serial| return jj.receive(p, b, serial); if (b.painted != null) return receivePaint(p, b); + if (b.gen & jj.gen_bit != 0) return jj.receive(p, b); if (b.gen != s.gen or (s.state != .searching and s.state != .capped)) { s.dropped += 1; return; diff --git a/core/jj.zig b/core/jj.zig index a381443c..0cdf94a1 100644 --- a/core/jj.zig +++ b/core/jj.zig @@ -241,15 +241,15 @@ pub const Changes = struct { /// What jj answered for pane `serial`'s `Changes` (`Pardes.changes`), if anything. pub fn answered(p: *Pardes, serial: u32) ?*Changes { - for (p.changes.items) |*c| if (c.serial == serial) return c; + for (p.changes.answers.items) |*c| if (c.serial == serial) return c; return null; } /// Pane `serial`'s answers let go (the pane's end). pub fn forget(p: *Pardes, serial: u32) void { - for (p.changes.items, 0..) |*c, i| if (c.serial == serial) { + for (p.changes.answers.items, 0..) |*c, i| if (c.serial == serial) { c.deinit(p.gpa); - _ = p.changes.swapRemove(i); + _ = p.changes.answers.swapRemove(i); return; }; } @@ -339,7 +339,7 @@ pub fn changesWord(c: pardes.builtins.Ctx) void { /// A `Changes` asked of jj: the pane (by serial), the file it shows and the /// revision whose lines are annotated, the revset. Its two runs go on a /// worker where the host has one (the `search` effect: Stream.start), their -/// raw answers back as one input (`Event.search`, its `jj` set) stamped +/// raw answers back as one input (`Event.search`) stamped /// with its generation, which the step takes (`receive`): a replica replays /// that input and never runs jj. With no worker (a test), on the loop, each /// run a site of the step's tape. @@ -367,9 +367,44 @@ pub const Ask = struct { } }; -/// The `Changes` asked and its worker (`Stream.changes`), one at a time: -/// one asked while it runs waits (`Stream.changes_next`), and only the last -/// asked is pinned. +/// The session's `Changes` (`Pardes.changes`): jj's answers, a pane's by +/// its serial; the `Changes` jj runs for, one at a time, the one asked +/// while it runs (only the last asked is pinned), and how many were asked +/// (the generation). In every digest of the session: nothing until a +/// `Changes` is asked, then one update while nothing is asked of jj. +pub const Session = struct { + answers: std.ArrayList(Changes) = .empty, + task: ?Task = null, + next: ?*Ask = null, + count: u32 = 0, + + pub fn digest(c: *const Session, h: *pardes.walk.Hasher) void { + if (c.count == 0) return; + const bits: u64 = c.count | @as(u64, @intFromBool(c.task != null)) << 32 | @as(u64, @intFromBool(c.next != null)) << 33; + h.update(std.mem.asBytes(&bits)); + pardes.walk.hash(std.ArrayList(Changes), &c.answers, h); + if (c.task) |*t| pardes.walk.hash(Task, t, h); + if (c.next) |n| pardes.walk.hash(Ask, n, h); + } + + /// The session's end: the worker canceled, the asks and the answers + /// let go. + pub fn deinit(c: *Session, gpa: std.mem.Allocator) void { + if (c.task) |t| { + c.task = null; + if (t.future) |f| pardes.Stream.join(f, t.io.?, .cancel); + t.ask.deinit(); + } + if (c.next) |a| { + c.next = null; + a.deinit(); + } + for (c.answers.items) |*a| a.deinit(gpa); + c.answers.deinit(gpa); + } +}; + +/// The `Changes` jj runs for, and its worker. pub const Task = struct { // The worker the host started for it, live: a replica has none. pub const replica = .{ .authority = .{ "future", "io" } }; @@ -382,27 +417,26 @@ pub const Task = struct { /// from 0; a paint's has the top one). pub const gen_bit: u32 = 1 << 29; -fn liveGen(s: *const pardes.Stream) u32 { - return gen_bit | (s.changes_count & (gen_bit - 1)); +fn liveGen(c: *const Session) u32 { + return gen_bit | (c.count & (gen_bit - 1)); } /// Ask jj of `revset` for pane `serial` (said at pane `id` while it runs): /// what the pane shows stays until the answer lands. fn ask(p: *Pardes, id: usize, serial: u32, path: []const u8, rev: []const u8, revset: []const u8) void { - const s = &p.stream; - s.changes_count +%= 1; - const a = Ask.make(p.gpa, liveGen(s), serial, path, rev, revset) catch return p.reportFailure(id, "Changes: out of memory"); + p.changes.count +%= 1; + const a = Ask.make(p.gpa, liveGen(&p.changes), serial, path, rev, revset) catch return p.reportFailure(id, "Changes: out of memory"); p.setStatus(.at(id), "Changes: jj\u{2026}"); - if (s.changes != null) { - if (s.changes_next) |old| old.deinit(); - s.changes_next = a; + if (p.changes.task != null) { + if (p.changes.next) |old| old.deinit(); + p.changes.next = a; return; } go(p, a); } fn go(p: *Pardes, a: *Ask) void { - p.stream.changes = .{ .ask = a }; + p.changes.task = .{ .ask = a }; if (p.tape.has(p, .search)) return p.emit(.{ .search = .{ .gen = a.gen } }); runHere(p, a.gen, false); } @@ -410,7 +444,7 @@ fn go(p: *Pardes, a: *Ask) void { /// The host's `search` effect for a `Changes` generation (Stream.start): /// its runs on a worker that sends to `sink`. pub fn start(io: std.Io, gpa: std.mem.Allocator, p: *Pardes, gen: u32, ctx: ?*anyopaque, sink: pardes.Stream.Sink) void { - const t = if (p.stream.changes) |*t| t else return; + const t = if (p.changes.task) |*t| t else return; if (t.ask.gen != gen or t.future != null) return; t.ask.out = gpa; // No worker to be had: here, its answer an input. @@ -427,11 +461,11 @@ fn work(a: *Ask, ctx: ?*anyopaque, sink: pardes.Stream.Sink) void { /// `in_call`: in the host's `search` entry, the answer handed in as an /// input, as a worker's is. pub fn runHere(p: *Pardes, gen: u32, comptime in_call: bool) void { - const t = p.stream.changes orelse return; + const t = p.changes.task orelse return; if (t.ask.gen != gen or t.future != null) return; const b = answer(p, p.gpa, t.ask); defer b.deinit(p.gpa); - if (in_call) p.update(.{ .search = b }) else receive(p, &b, t.ask.serial); + if (in_call) p.update(.{ .search = b }) else receive(p, &b); } /// jj's two answers for `a`, raw, in `gpa`: through `p`'s tape when given @@ -440,7 +474,7 @@ fn answer(p: ?*Pardes, gpa: std.mem.Allocator, a: *const Ask) pardes.Stream.Batc const failed = struct { fn f(g: std.mem.Allocator, at: *const Ask, why: []u8) pardes.Stream.Batch { _ = g; - return .{ .gen = at.gen, .jj = at.serial, .rows = why, .end = .failed }; + return .{ .gen = at.gen, .rows = why, .end = .failed }; } }.f; const set = fileset(gpa, a.path) catch return failed(gpa, a, refusal(gpa, "out of memory").refused); @@ -457,7 +491,7 @@ fn answer(p: ?*Pardes, gpa: std.mem.Allocator, a: *const Ask) pardes.Stream.Batc const any = while (it.next()) |l| { if (pinOf(l) != null) break true; } else false; - if (!any) return .{ .gen = a.gen, .jj = a.serial, .rows = logged, .end = .done }; + if (!any) return .{ .gen = a.gen, .rows = logged, .end = .done }; // Who wrote each line: a path, not a fileset (annotate takes one file // as it is named). const annotate_args = [_][]const u8{ "file", "annotate", "-r", a.rev, "-T", "commit.change_id().short(8) ++ \"\\t\" ++ content", "--", std.Io.Dir.path.basename(a.path) }; @@ -466,7 +500,7 @@ fn answer(p: ?*Pardes, gpa: std.mem.Allocator, a: *const Ask) pardes.Stream.Batc gpa.free(logged); return failed(gpa, a, why); }, - .bytes => |annotated| .{ .gen = a.gen, .jj = a.serial, .rows = logged, .styles = annotated, .end = .done }, + .bytes => |annotated| .{ .gen = a.gen, .rows = logged, .styles = annotated, .end = .done }, }; } @@ -474,25 +508,27 @@ fn runJj(p: ?*Pardes, gpa: std.mem.Allocator, dir: []const u8, args: []const []c return if (p) |q| q.tape.site(.jj, gpa, runHost, .{ gpa, dir, args }) else runHost(gpa, dir, args); } -/// A `Changes` answer (`Event.search`, its `jj` set), borrowed: the last -/// asked's is parsed and pinned on its pane; an earlier one's, or a gone -/// pane's, dropped. Then the one asked meanwhile goes. -pub fn receive(p: *Pardes, b: *const pardes.Stream.Batch, serial: u32) void { - const s = &p.stream; +/// A `Changes` answer (`Event.search` with a `Changes` generation), +/// borrowed: the last asked's is parsed and pinned on its pane (the ask +/// names it); an earlier one's, or a gone pane's, dropped. Then the one +/// asked meanwhile goes. +pub fn receive(p: *Pardes, b: *const pardes.Stream.Batch) void { var revset: [Changes.revset_max]u8 = undefined; var revset_len: usize = 0; - if (s.changes) |t| if (t.ask.gen == b.gen) { - s.changes = null; + var serial: u32 = 0; + if (p.changes.task) |t| if (t.ask.gen == b.gen) { + p.changes.task = null; if (t.future) |f| pardes.Stream.join(f, t.io.?, .wait); + serial = t.ask.serial; revset_len = t.ask.revset.len; @memcpy(revset[0..revset_len], t.ask.revset); t.ask.deinit(); }; - defer if (s.changes == null) if (s.changes_next) |next| { - s.changes_next = null; + defer if (p.changes.task == null) if (p.changes.next) |next| { + p.changes.next = null; go(p, next); }; - if (b.gen != liveGen(s) or revset_len == 0) return; + if (b.gen != liveGen(&p.changes) or revset_len == 0) return; const id = p.paneBySerial(serial) orelse return; pinAnswer(p, id, b, revset[0..revset_len]); } @@ -503,17 +539,17 @@ pub fn receive(p: *Pardes, b: *const pardes.Stream.Batch, serial: u32) void { /// lines, never asking jj again: the lines a splice takes out take their /// values with them, and a line it puts in is nobody's. pub fn edited(p: *Pardes, f: *const File.State, off: usize, len: usize, bytes: []const u8) void { - var i = p.changes.items.len; + var i = p.changes.answers.items.len; while (i > 0) { i -= 1; - const c = &p.changes.items[i]; + const c = &p.changes.answers.items[i]; const id = p.paneBySerial(c.serial) orelse continue; const shown = if (p.panes[id].?.file) |*shown| shown else continue; if (shown.content.ptr != f.content.ptr) continue; splice(p.gpa, c, f.content, off, len, bytes) catch { // No room for it: its values would lie, so it goes. c.deinit(p.gpa); - _ = p.changes.swapRemove(i); + _ = p.changes.answers.swapRemove(i); }; } } @@ -541,19 +577,6 @@ fn splice(gpa: std.mem.Allocator, c: *Changes, text: []const u8, off: usize, len c.lines = next; } -/// The session's end: the worker canceled, the asks let go. -pub fn cancel(s: *pardes.Stream) void { - if (s.changes) |t| { - s.changes = null; - if (t.future) |f| pardes.Stream.join(f, t.io.?, .cancel); - t.ask.deinit(); - } - if (s.changes_next) |a| { - s.changes_next = null; - a.deinit(); - } -} - /// jj's answer `b` for `revset` parsed and pinned on pane `id`, or its /// refusal said there. fn pinAnswer(p: *Pardes, id: usize, b: *const pardes.Stream.Batch, revset: []const u8) void { @@ -583,7 +606,7 @@ fn pinAnswer(p: *Pardes, id: usize, b: *const pardes.Stream.Batch, revset: []con if (answered(p, pane.serial)) |kept| { kept.deinit(p.gpa); kept.* = changes.*; - } else p.changes.append(p.gpa, changes.*) catch { + } else p.changes.answers.append(p.gpa, changes.*) catch { changes.deinit(p.gpa); return p.reportFailure(id, "Changes: out of memory"); }; @@ -1199,7 +1222,7 @@ const WorkerJj = struct { /// answer the session's input, as its loop hands it, and the client's. fn land(w: *WorkerJj) !void { var waited: u32 = 0; - while (w.a.core.stream.changes != null or w.a.core.stream.changes_next != null) : (waited += 1) { + while (w.a.core.changes.task != null or w.a.core.changes.next != null) : (waited += 1) { if (waited == 5000) return error.Timeout; var batch: Inbox.Batch = .{}; w.shell.q.takeInto(&batch); @@ -1254,7 +1277,7 @@ test "the pane closed before its Changes answer: nothing pinned, nothing kept" { try w.word("Del"); try testing.expect(w.a.core.panes[w.id] == null or w.a.core.panes[w.id].?.serial != w.serial); try w.land(); - try testing.expectEqual(@as(usize, 0), w.a.core.changes.items.len); + try testing.expectEqual(@as(usize, 0), w.a.core.changes.answers.items.len); } test "Changes draws a lane a pinned change after the line number, nested ones a level to the right on their rows only, and a line one added whole green across it; bare again, the gutter as it was" { |
