summaryrefslogtreecommitdiff
path: root/core/tape.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-19 11:52:31 -0300
committerGabriel Schneider <[email protected]>2026-11-19 12:41:12 -0300
commitea833164aa83d02119301a204b91ff9a8eeff27a (patch)
tree267ff49280d88c8807926c1c09d615ba4d0c5782 /core/tape.zig
parent71d8744b41dec56d835e30f531b96fede5d610a1 (diff)
downloadpardes-ea833164aa83d02119301a204b91ff9a8eeff27a.tar.gz
pardes-ea833164aa83d02119301a204b91ff9a8eeff27a.zip
tape: the core's entries are written once (P22, pardes-entries part 2, its first half; the owner's mechanism C and names): each of 36 entries is its public name made by `pardes.tape.taped` from the private body directly under it (`pub const setCwd = pardes.tape.taped(setCwdInner);` then `fn setCwdInner(p: *Pardes, id: usize, cwd: []const u8) void { body }`, the doc comment on the public line), so the entry no longer opens with `tape.enter` and a deferred `tape.leave` and has no replay arm of its own: `taped` makes a function of the inner's own parameters (one per arity, 1 to 4, `tape.Taped((function 'setCwdInner')).f3` in a profile) that records the variant of `tape.Input` whose line names the entry (`set_cwd: Args(Pardes.setCwd)`, a type of that entry's own, found by the function itself in the wrapper's body: at the declaration it would be a dependency loop between the union and the entries), defers the leave and calls the inner with `@call(.always_inline)`, so an entry compiles to what one written with its own pair did; the replay's `apply` keeps its hand-written arms (init, init_from_dump, restore, deinit, pump, render, to_view) and calls every other entry from its variant in ONE `inline else`; the six kept by hand say why in one line each (init and initFromDump: no core yet; deinit: what it records is no argument and it leaves on no core; pump: the host is set before the enter and is no input; render: the arena is no input; toView: the live view asked again records nothing, and a frontend asks before every input), and restore is taped with an arm of its own (it swaps the replay's core); an entry in 36 places went from 4 lines in 3 places (variant, enter, leave, arm) to 2 in 2 (the public line beside its body, the variant's line), and an argument left out or passed in another order can no longer be written: a variant naming the wrong entry or an entry with no variant does not compile, the error naming the inner ("tape: two variants of Input name the entry tape.Taped((function 'setStatusInner')): set_message and set_status", "tape: no variant of Input names the entry tape.Taped((function 'rejoinViewInner'))"); the payloads are now the entry's arguments in order (`args[0]`), read so in the tape's three readers (a screen-state output's bytes stripped, the input's name for a divergence, `describe`), `Input.Font` gone; the bytes a tape holds are the same (walk writes no field names) but its shape is not, so the semantics word moves, 2f615168fe07b35f -> 313a99190b93a8c5; `update`, `presented` and `dumpFailed` read `p.view()` before their enter and now after it (the enter writes only the tape's own state and the host it wraps, nothing a view holds); no call site changed, no line outside core/; core/ 203 lines added, 187 deleted (tape.zig 204 of them: the helper 58 code lines, 36 arms gone). Gate: tui core-test 838/981 (143 skipped), shell-test 1016/1019, term-test 15/15, pane-test 183/184, kitty-test 45/45, behaviour-test 275/275 and --replay 20 seeds of 70 steps, 0 failures, process-test lspcomplete and lspcompletemoved (the base's), replicas --clients 5 diverged 0, unequal 0, deltas wrong 0 (1705/0, 1722/0, 1714/0), quiet 526/0, 652/0, 811/0, stops not at may_wait 0; a session recorded and replayed by itself (test/cross_replay.py: 28 digests agree in each of the three replicas); gui unit-test 1614/1618 (the perf check's BaselineHarnessMismatch, the base's); daemon.py plain, --far, --games, --window-clients pass; the macOS library for Linux and aarch64-macos and the FreeBSD cross-build configure and build; frames (gpu/entries-1 against tree1) 77/77 byte-identical in Debug, gui unit-test 1614/1618 there too, perfab corpus2x +0.100% (session noise 0.031%, threshold 0.4%), pictures identical in all 10 runs, perfops 97 counts within 0.1% (largest image-frame on medium +0.0246%); every entry is one function in the ReleaseFast pardes-perf (36 symbols `tape.Taped((function '<inner>')).f<n>`); update's count, key-down on medium per operation, 10,040,394 at pardes-handle-1's step 2 -> 10,040,439 (+0.0004%; 10,040,102 at tree1), render's, render on medium per operation, 11,456,076 -> 11,456,085 (+0.0001%; 11,455,763 at tree1)
Co-Authored-By: Claude Opus <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/tape.zig')
-rw-r--r--core/tape.zig204
1 files changed, 125 insertions, 79 deletions
diff --git a/core/tape.zig b/core/tape.zig
index dc80887c..bb55ce5a 100644
--- a/core/tape.zig
+++ b/core/tape.zig
@@ -35,55 +35,56 @@ pub var on: ?*Tape = null;
pub var none: Tape = .{ .gpa = std.heap.page_allocator, .replay = false, .through = true, .scratch = .init(std.heap.page_allocator), .arena = .init(std.heap.page_allocator) };
/// Every call into the core a host makes (a 9P connection task's too).
+/// An entry written once (`taped`) has the variant `Args(<the entry>)`,
+/// its arguments after the core in order; the six written by hand say
+/// what they carry, and each has its arm in `apply`.
pub const Input = union(enum) {
init: pardes.Options,
init_from_dump: struct { opts: pardes.Options, bytes: []const u8 },
- restore: struct { bytes: []const u8, from: []const u8 },
+ restore: Args(pardes.dump.restore),
/// Whether it was the session's own core, or one a Restore replaced.
deinit: bool,
- update: pardes.Event,
- serve_fs: pardes.ctlfs.Req,
+ update: Args(Pardes.update),
+ serve_fs: Args(Pardes.serveFs),
pump,
- step,
- perform: pardes.Effect,
- next_effect,
- advance: u64,
+ step: Args(Pardes.step),
+ perform: Args(Pardes.perform),
+ next_effect: Args(Pardes.nextEffect),
+ advance: Args(Pardes.advance),
render,
- take_restore,
- set_cwd: struct { id: usize, cwd: []const u8 },
- acknowledge_shell: struct { id: usize, executable: []const u8, prompt_marks: bool },
+ take_restore: Args(Pardes.takeRestore),
+ set_cwd: Args(Pardes.setCwd),
+ acknowledge_shell: Args(Pardes.acknowledgeShell),
/// A dead session's shell taken in (Adopt), and a terminal's reader
/// that failed: what a host writes into the core, made inputs.
- adopt_shell,
- reader_failed: struct { id: u8, err: anyerror },
- shell_failed: struct { id: u8, err: anyerror },
- restart_failed: struct { id: u8, err: anyerror },
- save_failed: struct { id: u8, path: []const u8, err: anyerror },
- dump_failed: struct { path: []const u8, err: anyerror },
- set_last_dump: []const u8,
- set_message: struct { id: usize, text: []const u8 },
- set_status: struct { id: usize, text: []const u8 },
- report_error: struct { id: usize, operation: []const u8, err: anyerror },
- reload_watched_file: struct { id: u8, bytes: []const u8 },
- note_late_failure: usize,
- invalidate,
- presented,
- run_closed_lines,
- file_deleted: u8,
- open_view,
- close_view: u8,
- leave_view: u8,
- away_view: u8,
- rejoin_view: u8,
+ adopt_shell: Args(Pardes.adoptShell),
+ reader_failed: Args(Pardes.readerFailed),
+ shell_failed: Args(Pardes.shellFailed),
+ restart_failed: Args(Pardes.restartFailed),
+ save_failed: Args(Pardes.saveFailed),
+ dump_failed: Args(Pardes.dumpFailed),
+ set_last_dump: Args(Pardes.setLastDump),
+ set_message: Args(Pardes.setMessage),
+ set_status: Args(Pardes.setStatus),
+ report_error: Args(Pardes.reportError),
+ reload_watched_file: Args(Pardes.reloadWatchedFile),
+ note_late_failure: Args(Pardes.noteLateFailure),
+ invalidate: Args(Pardes.invalidate),
+ presented: Args(Pardes.presented),
+ run_closed_lines: Args(pardes.ctlfs.runClosedLines),
+ file_deleted: Args(pardes.panes.File.deleted),
+ open_view: Args(Pardes.openView),
+ close_view: Args(Pardes.closeView),
+ leave_view: Args(Pardes.leaveView),
+ away_view: Args(Pardes.awayView),
+ rejoin_view: Args(Pardes.rejoinView),
to_view: u8,
/// What a window says of its font and its opacity (gui.zig).
- take_font_request,
- observe_font: Font,
- acknowledge_font: Font,
- reject_font,
- window_opacity_done: struct { applied: u8, failure: ?[]const u8 },
-
- pub const Font = struct { name: []const u8, size: u16, unit: pardes.config.Runtime.FontSizeUnit };
+ take_font_request: Args(Pardes.takeFontRequest),
+ observe_font: Args(Pardes.observeFont),
+ acknowledge_font: Args(Pardes.acknowledgeFont),
+ reject_font: Args(Pardes.rejectFont),
+ window_opacity_done: Args(Pardes.windowOpacityDone),
};
/// The core's own syscalls (each `turn.yield()`s around itself), numbered
@@ -419,9 +420,9 @@ pub const Tape = struct {
t.wrap(core);
}
// A terminal's output crosses as screen state, not as its bytes.
- if (t.screen_state and input.* == .update and input.update == .output) {
+ if (t.screen_state and input.* == .update and input.update.args[0] == .output) {
var bare = input.*;
- bare.update.output.bytes = &.{};
+ bare.update.args[0].output.bytes = &.{};
t.record(.input, Input, &bare);
} else t.record(.input, Input, input);
return .{ .outer = true };
@@ -659,7 +660,7 @@ pub const Tape = struct {
const input = walk.decode(Input, r, if (kept) keep else t.arena.allocator()) catch return t.diverge("a short input", .{});
const outer = t.applying;
t.applying = switch (input) {
- .update => |ev| @tagName(ev),
+ .update => |a| @tagName(a.args[0]),
else => @tagName(input),
};
defer t.applying = outer;
@@ -690,8 +691,8 @@ pub const Tape = struct {
const p = t.core orelse return t.diverge("an input before the core", .{});
switch (input) {
.init, .init_from_dump => unreachable,
- .restore => |rs| {
- const nc = pardes.dump.restore(p, rs.bytes, rs.from) catch |err| return t.diverge("restore failed: {t}", .{err});
+ .restore => |a| {
+ const nc = pardes.dump.restore(p, a.args[0], a.args[1]) catch |err| return t.diverge("restore failed: {t}", .{err});
t.core = nc;
nc.tape = t;
nc.host = t.hostOf(t.mask);
@@ -705,47 +706,14 @@ pub const Tape = struct {
}
return t.skip();
},
- .update => |ev| p.update(ev),
- .serve_fs => |req| _ = p.serveFs(req),
.pump => p.pump(t.hostOf(t.mask)) catch {},
- .step => p.step(),
- .perform => |e| p.perform(e),
- .next_effect => _ = p.nextEffect(),
- .advance => |now| p.advance(now),
.render => if (t.skips_render) return t.skip() else {
_ = p.frame_arena.reset(.retain_capacity);
_ = p.render(p.frame_arena.allocator()) catch {};
},
- .take_restore => _ = p.takeRestore(),
- .set_cwd => |a| p.setCwd(a.id, a.cwd),
- .acknowledge_shell => |a| p.acknowledgeShell(a.id, a.executable, a.prompt_marks),
- .adopt_shell => _ = p.adoptShell(),
- .reader_failed => |a| p.readerFailed(a.id, a.err),
- .shell_failed => |a| p.shellFailed(a.id, a.err),
- .restart_failed => |a| p.restartFailed(a.id, a.err),
- .save_failed => |a| p.saveFailed(a.id, a.path, a.err),
- .dump_failed => |a| p.dumpFailed(a.path, a.err),
- .set_last_dump => |path| p.setLastDump(path),
- .set_message => |a| p.setMessage(a.id, a.text),
- .set_status => |a| p.setStatus(a.id, a.text),
- .report_error => |a| p.reportError(a.id, a.operation, a.err),
- .reload_watched_file => |a| _ = p.reloadWatchedFile(a.id, a.bytes),
- .note_late_failure => |id| p.noteLateFailure(id),
- .invalidate => p.invalidate(),
- .presented => p.presented(),
- .run_closed_lines => pardes.ctlfs.runClosedLines(p),
- .file_deleted => |id| pardes.panes.File.deleted(p, id),
- .open_view => _ = p.openView(),
- .close_view => |k| p.closeView(k),
- .leave_view => |k| p.leaveView(k),
- .away_view => |k| p.awayView(k),
- .rejoin_view => |k| _ = p.rejoinView(k),
.to_view => |k| p.toView(k),
- .take_font_request => _ = p.takeFontRequest(),
- .observe_font => |f| _ = p.observeFont(f.name, f.size, f.unit),
- .acknowledge_font => |f| _ = p.acknowledgeFont(f.name, f.size, f.unit),
- .reject_font => p.rejectFont(),
- .window_opacity_done => |w| p.windowOpacityDone(w.applied, w.failure),
+ // Every entry written once: the one its variant names.
+ inline else => |a| _ = @call(.auto, @TypeOf(a).entry, .{p} ++ a.args),
}
t.finishInput();
}
@@ -1065,6 +1033,84 @@ pub fn forget(p: *Pardes) void {
(on orelse &none).forget(p);
}
+/// An entry written once (P22): its public name made from the private
+/// body directly under it, `pub const setCwd = pardes.tape.taped(setCwdInner);`.
+/// The same parameters; the call recorded as the variant of `Input` whose
+/// line names the entry (`set_cwd: Args(Pardes.setCwd)`), which the entry
+/// finds by itself; the body inlined, so the entry compiles to what one
+/// opening with its own `enter` and deferred `leave` did.
+pub fn taped(comptime inner: anytype) @TypeOf(inner) {
+ const T = Taped(inner);
+ return switch (@typeInfo(@TypeOf(inner)).@"fn".param_types.len) {
+ 1 => T.f1,
+ 2 => T.f2,
+ 3 => T.f3,
+ 4 => T.f4,
+ else => @compileError("tape: an entry of more arguments"),
+ };
+}
+
+/// One function for each number of parameters, of which only the one
+/// `taped` picks is analysed; a profile names it `tape.Taped((function
+/// '<inner>')).f<n>`. The variant is found in the body, not in the
+/// declaration: there it would need `Input`, whose line needs the entry
+/// (a dependency loop).
+fn Taped(comptime inner: anytype) type {
+ const F = @typeInfo(@TypeOf(inner)).@"fn";
+ const P = F.param_types;
+ const R = F.return_type.?;
+ return struct {
+ fn f1(p: P[0].?) R {
+ const entered = enter(p, @unionInit(Input, tagOf(f1, @This()), .{ .args = .{} }));
+ defer leave(p, entered);
+ return @call(.always_inline, inner, .{p});
+ }
+ fn f2(p: P[0].?, a: P[1].?) R {
+ const entered = enter(p, @unionInit(Input, tagOf(f2, @This()), .{ .args = .{a} }));
+ defer leave(p, entered);
+ return @call(.always_inline, inner, .{ p, a });
+ }
+ fn f3(p: P[0].?, a: P[1].?, b: P[2].?) R {
+ const entered = enter(p, @unionInit(Input, tagOf(f3, @This()), .{ .args = .{ a, b } }));
+ defer leave(p, entered);
+ return @call(.always_inline, inner, .{ p, a, b });
+ }
+ fn f4(p: P[0].?, a: P[1].?, b: P[2].?, c: P[3].?) R {
+ const entered = enter(p, @unionInit(Input, tagOf(f4, @This()), .{ .args = .{ a, b, c } }));
+ defer leave(p, entered);
+ return @call(.always_inline, inner, .{ p, a, b, c });
+ }
+ };
+}
+
+/// What an entry's variant carries: its arguments after the core, and
+/// the entry itself, which makes the type the entry's own (two entries of
+/// the same parameters are two types): the entry finds its variant by it
+/// (`tagOf`) and the replay calls the entry from it (`apply`).
+pub fn Args(comptime f: anytype) type {
+ const params = @typeInfo(@TypeOf(f)).@"fn".param_types;
+ var types: [params.len - 1]type = undefined;
+ for (params[1..], &types) |P, *T| T.* = P.?;
+ const Tuple = @Tuple(&types);
+ return struct {
+ args: Tuple,
+ pub const entry = f;
+ };
+}
+
+/// The variant whose line names `entry`, which `Made` (`Taped(inner)`) made.
+fn tagOf(comptime entry: anytype, comptime Made: type) [:0]const u8 {
+ const u = @typeInfo(Input).@"union";
+ var found: ?[:0]const u8 = null;
+ for (u.field_names, u.field_types) |name, T| {
+ if (@typeInfo(T) != .@"struct" or !@hasDecl(T, "entry")) continue;
+ if (@TypeOf(T.entry) != @TypeOf(entry) or T.entry != entry) continue;
+ if (found) |other| @compileError("tape: two variants of Input name the entry " ++ @typeName(Made) ++ ": " ++ other ++ " and " ++ name);
+ found = name;
+ }
+ return found orelse @compileError("tape: no variant of Input names the entry " ++ @typeName(Made) ++ " (a line `<tag>: Args(<the entry>)` in tape.Input)");
+}
+
/// The Host function `i`, through the tape.
fn Wrap(comptime i: usize) type {
const Fn = @typeInfo(@typeInfo(@FieldType(VTable, host_fns[i])).optional.child).pointer.child;
@@ -1501,7 +1547,7 @@ pub fn describe(bytes: []const u8, w: *std.Io.Writer) void {
var room: [4096]u8 = undefined;
var fixed: std.heap.FixedBufferAllocator = .init(&room);
var in: walk.Reader = .{ .bytes = payload };
- if (tag == .update) if (walk.decode(Input, &in, fixed.allocator())) |input| switch (input.update) {
+ if (tag == .update) if (walk.decode(Input, &in, fixed.allocator())) |input| switch (input.update.args[0]) {
.key => |k| w.print(" {any}", .{k}) catch {},
.mouse => |m| w.print(" {t} {t} {d},{d} body {any} tag {any}", .{ m.button, m.kind, m.col, m.row, m.body_hit != null, m.tag_hit != null }) catch {},
else => {},