From 2d30f3781ab40c38710835806a1029359509d27b Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 18 Nov 2026 23:31:53 -0300 Subject: frontends: the terminal and the window draw and turn input into events from the frame and one record beside it (the reorganisation's runners, step 10a, on the line as it is now): surface.Facts, the core's (Pardes.facts) and pointed at by the frame it is filled for (Surface.facts), filled by draw.place in the walk over the panes it makes anyway and by a runner after an input it applied (draw.facts), carries what a frontend took from the core (the view's size, cell pixels and row metrics, each open pane's rectangle, tag rows, context rows, serial, whether a terminal whose program takes the wheel or a kind's, its mode, the jumps' back and forward, the settings it draws by); the window keeps a copy (Gui.facts) with the frame it shows (Gui.frame) and its hit tests, wheel, touchpad rows, timedemo, blink, overlays and dim read only those; Host.present returns whether the frame was shown, so present_skipped leaves the core and nothing writes it; the frame statistics are the runner's (Options.frames, Pardes.frames a pointer to them), which the frontends write and the core's draw reads; Replica.draw returns the shown and keeps needs_frame itself, so the two client loops put nothing back; OwnView and Replica.within are deleted (the window client's own pass reads its facts, its inputs fed after each event as before); rollback-bench compiles again; the tape's watched fields lose present_skipped and present answers a bool, so the semantics word moves (0900e560fd6395ef, pardes-seam-b's, -> 2f615168fe07b35f). The first version (psvoxrup, on pardes-tree) kept the record by value in every Surface, and every Surface a tag line builds for a moment copied its 1.3 KB more: +1,556 instructions an image-frame, +3,763 a render (perf record: memcpy); now perfops against tree1 97 counts within 0.1%, largest image-frame +0.029%. Brought onto pardes-seam-b: the client set-up keeps the Distrusted list beside the runner's frame statistics, Options' local fields both, and the rollback test's present answers a bool. Gate: frames 77/77 byte-identical in Debug, the alternated windows' pictures identical in all 10 ReleaseFast runs, corpus2x -0.039% (PASS); tui core-test 836/979 (143 skipped), shell-test 1014/1017, term-test 15/15, pane-test 183/184, kitty-test 45/45, behaviour-test 275/275 and --replay 275/275, process-test lspcomplete and lspcompletemoved (the base's) and grep (passed alone three times of three); gui unit-test 1612/1616 with the base's BaselineHarnessMismatch; replicas --clients 5 diverged 0, unequal 0, deltas wrong 0, quiet 470/0, 600/0, 764/0; daemon.py plain, --far, --games, --window-clients, --upgrade both ways (from pardes-tree's pfx-base); the macOS library for Linux and aarch64-macos and the FreeBSD tui configure; rollback-bench runs Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- dev/tui/tui.zig | 85 +++++++++++++++++++++++++++++++++------------------------ 1 file changed, 50 insertions(+), 35 deletions(-) (limited to 'dev/tui') diff --git a/dev/tui/tui.zig b/dev/tui/tui.zig index 71a87f29..5903e448 100644 --- a/dev/tui/tui.zig +++ b/dev/tui/tui.zig @@ -624,23 +624,16 @@ const Client = struct { link: replica.Link, /// The client's own view drawn on its terminal, outside any step: the - /// replica's digest stays the session's (views.drawOne). What the host - /// writes in the core as it presents (a frame skipped, one owed) is put - /// back, as the frame statistics are this process's. + /// replica's digest stays the session's (views.drawOne); the frame + /// statistics are this process's (`Shell.frames`). fn draw(c: *Client, s: *Shell) void { - const r = c.link.r; - const core = r.core(); - const skipped = core.present_skipped; - const needs = core.needs_frame; - core.frames.mark_start = pardes.FrameStats.nowNs(); - core.frames.mark_update = core.frames.mark_start; - r.draw(.{ .ctx = s, .f = Shell.present }); - core.frames.mark_render = core.frames.mark_update; - const drew = core.frames.bytes > 0; - c.link.owed = core.present_skipped; - core.frames.record(pardes.FrameStats.nowNs(), false, c.link.owed); - core.present_skipped = skipped; - core.needs_frame = needs; + const frames = s.frames; + frames.mark_start = pardes.FrameStats.nowNs(); + frames.mark_update = frames.mark_start; + c.link.owed = !c.link.r.draw(.{ .ctx = s, .f = Shell.present }); + frames.mark_render = frames.mark_update; + const drew = frames.bytes > 0; + frames.record(pardes.FrameStats.nowNs(), false, c.link.owed); c.link.drawn(drew); } }; @@ -696,6 +689,9 @@ pub fn runClient(init: std.process.Init, opts: pardes.Options, path: []const u8, o.image_allocator = allocs.image; var distrusted: pardes.Addon.Distrusted = .{}; o.distrusted = &distrusted; + // Its frame timings, this process's (Pardes.frames). + var frames: pardes.FrameStats = .{}; + o.frames = &frames; var joined: replica.Joined = undefined; var r = replica.join(gpa, &joined, path, session, o, have, &why, null) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)}); @@ -729,6 +725,7 @@ pub fn runClient(init: std.process.Init, opts: pardes.Options, path: []const u8, .gpa = gpa, .lsp_gpa = allocs.lsp, .core = r.core(), + .frames = &frames, .prompt_rcs = undefined, .loop = &loop, .frame = &frame_arena, @@ -768,11 +765,8 @@ pub fn runClient(init: std.process.Init, opts: pardes.Options, path: []const u8, // The tty's own motions (its glide, a jump's trail): the last // frame again, moved on, as the session's terminal draws them // (`ownFrame`), not rendered again. One the terminal's answer - // holds is skipped (the next tick's moves on): nothing of it - // is written in the core, as `Client.draw` puts it back. - const skipped = r.core().present_skipped; - Shell.present(&sh, &r.core().surface); - r.core().present_skipped = skipped; + // holds is skipped (the next tick's moves on). + _ = Shell.present(&sh, &r.core().surface); link.again += 1; } // Idle, its frame drawn: the next key needs no copy taken. @@ -982,6 +976,9 @@ fn localSession( options.image_allocator = allocs.image; options.tree_sitter_allocator = allocs.tree_sitter; options.frame_allocator = allocs.frame; + // Its frame timings, the terminal's to write as it draws (Pardes.frames). + var frames: pardes.FrameStats = .{}; + options.frames = &frames; host_io.startWorkers(allocs); defer host_io.stopWorkers(); @@ -1010,6 +1007,7 @@ fn localSession( .gpa = gpa, .lsp_gpa = allocs.lsp, .core = core, + .frames = &frames, .prompt_rcs = &prompt_rcs, .loop = loop, .frame = &frame_arena, @@ -1202,6 +1200,9 @@ const Shell = struct { gpa: std.mem.Allocator, lsp_gpa: std.mem.Allocator, core: *pardes.Pardes, + /// Its runner's frame timings (Pardes.frames): written as it draws and + /// takes input, never through the core. + frames: *pardes.FrameStats, prompt_rcs: *const host_io.Shell.PromptFiles, loop: *Loop, /// The terminal (none in tests), and the view it shows: a second @@ -1255,6 +1256,9 @@ const Shell = struct { took_input: bool = false, took_output: bool = false, held: ?Msg = null, + /// A frame of the tty's own motions was held (`ownFrame`): the core's + /// next is owed too (`presentFrame`). + own_skipped: bool = false, /// This process a replica client of a session elsewhere (`runClient`): /// its inputs go to the replica, never straight to the core. client: ?*Client = null, @@ -1290,7 +1294,7 @@ const Shell = struct { const isolated_vtable: host_io.Host.VTable = .{ .wait_input = waitInput, .now = clockNow, - .present = present, + .present = presentFrame, .post_present = postPresent, .poll_frame = ownFrame, }; @@ -1298,7 +1302,7 @@ const Shell = struct { const vtable: host_io.Host.VTable = .{ .wait_input = waitInput, .now = clockNow, - .present = present, + .present = presentFrame, .post_present = postPresent, .poll_frame = pollFrame, .watched = watched, @@ -1689,11 +1693,11 @@ const Shell = struct { // answers it (FrameStats): when the loop takes it in. switch (event) { .key_press => { - core.frames.input(pardes.FrameStats.nowNs()); + s.frames.input(pardes.FrameStats.nowNs()); s.input_seen = true; }, .mouse => |m| if (m.type == .press) { - core.frames.input(pardes.FrameStats.nowNs()); + s.frames.input(pardes.FrameStats.nowNs()); s.input_seen = true; }, .paste, .paste_start, .winsize => s.input_seen = true, @@ -1903,7 +1907,9 @@ const Shell = struct { /// again, moved on, when the core has none to draw. fn ownFrame(ctx: ?*anyopaque) void { const s = of(ctx); - if (s.moving() and !s.core.needs_frame and s.core.surface.cells.len != 0) present(ctx, &s.core.surface); + if (s.moving() and !s.core.needs_frame and s.core.surface.cells.len != 0) { + if (!present(ctx, &s.core.surface)) s.own_skipped = true; + } } fn pollFrame(ctx: ?*anyopaque) void { @@ -1929,16 +1935,22 @@ const Shell = struct { } } - fn present(ctx: ?*anyopaque, canonical: *const pardes.Surface) void { + /// The core's frame (Host.present): owed too when a frame of the + /// tty's own motions (`ownFrame`) was held since the last. + fn presentFrame(ctx: ?*anyopaque, canonical: *const pardes.Surface) bool { const s = of(ctx); - const sc = s.term() orelse return; + const shown = present(ctx, canonical); + defer s.own_skipped = false; + return shown and !s.own_skipped; + } + + fn present(ctx: ?*anyopaque, canonical: *const pardes.Surface) bool { + const s = of(ctx); + const sc = s.term() orelse return true; const vx = &sc.vx; // The last frame is not answered yet: this one stays owed, and the // answer's wake draws the state as it is then. - if (sc.feed.ack.holds(@intCast(std.Io.Clock.awake.now(s.io).nanoseconds), s.io)) { - s.core.present_skipped = true; - return; - } + if (sc.feed.ack.holds(@intCast(std.Io.Clock.awake.now(s.io).nanoseconds), s.io)) return false; _ = s.frame.reset(.retain_capacity); const tz_cells = tracy.zone(@src(), "surface->vaxis"); const win = vx.window(); @@ -1949,10 +1961,10 @@ const Shell = struct { const cell_h = terminalCellPixels(vx.screen.height_pix, vx.screen.height, 16); const arted = sc.art.frame(s.gpa, s.frame.allocator(), canonical, vx.caps.kitty_graphics, cell_h) catch canonical; sc.glide.frame(s.gpa, arted, now, null, null); - s.core.frames.scroll = @round(sc.glide.follow.offset); + s.frames.scroll = @round(sc.glide.follow.offset); const glided = glideRows(s.frame.allocator(), arted, &sc.glide) catch arted; const truecolor = vx.caps.rgb or colortermTruecolor(); - const trail_on = s.core.settings.jump_trail and truecolor; + const trail_on = canonical.facts.jump_trail and truecolor; const shown = trailCells(s.frame.allocator(), glided, &sc.trail, trail_on, now) catch glided; paintCells(win, shown.cells, shown.cols, shown.rows); if (s.client) |c| if (c.link.notice_len > 0) noticeLine(win, c.link.notice(), shown.cols, shown.rows); @@ -2038,10 +2050,11 @@ const Shell = struct { vx.render(sc.tty.writer()) catch {}; // The frame's cost to the terminal (FrameStats.Frame.bytes): what // vaxis's diff of the cells wrote. - s.core.frames.bytes +|= @intCast(@min(std.math.maxInt(u32), sc.tty.tty_writer.pos + sc.tty.writer().end -| before)); + s.frames.bytes +|= @intCast(@min(std.math.maxInt(u32), sc.tty.tty_writer.pos + sc.tty.writer().end -| before)); if (sc.tty.tty_writer.pos + sc.tty.writer().end != before) sc.feed.ack.ask(sc.tty.writer(), @intCast(std.Io.Clock.awake.now(s.io).nanoseconds)); s.drawn_ns = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); tz_render.end(); + return true; } fn postPresent(ctx: ?*anyopaque) void { @@ -3016,6 +3029,7 @@ test "TTY queued results reject old PTY generations and LSP request IDs" { .gpa = gpa, .lsp_gpa = gpa, .core = core, + .frames = core.frames, .prompt_rcs = undefined, .loop = undefined, .frame = undefined, @@ -3061,6 +3075,7 @@ test "TTY worker setup failures finish matching LSP and pipe requests" { .gpa = failing.allocator(), .lsp_gpa = failing.allocator(), .core = core, + .frames = core.frames, .prompt_rcs = undefined, .loop = undefined, .frame = undefined, -- cgit v1.3