diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-31 06:28:48 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-31 06:28:48 -0300 |
| commit | a479792a1ab2b593642d7f648194bfa23ce01dba (patch) | |
| tree | 96ddee7d879a9084daa07db43b0b7910b072a785 /src/Stream.zig | |
| parent | e179cc44fe5060c76a5b00c41e53f11209f5acd9 (diff) | |
| parent | 4c761893e5b3fdae422622cfa02104ab36633045 (diff) | |
| download | pardes-a479792a1ab2b593642d7f648194bfa23ce01dba.tar.gz pardes-a479792a1ab2b593642d7f648194bfa23ce01dba.zip | |
Merge the upstream daemon and replicated client/server refactor
Diffstat (limited to 'src/Stream.zig')
| -rw-r--r-- | src/Stream.zig | 182 |
1 files changed, 157 insertions, 25 deletions
diff --git a/src/Stream.zig b/src/Stream.zig index da7d12eb..69dc311f 100644 --- a/src/Stream.zig +++ b/src/Stream.zig @@ -104,7 +104,13 @@ pub const ParseJob = struct { } }; -const ParseTask = struct { job: *ParseJob, future: ?std.Io.Future(void) = null, io: ?std.Io = null }; +const ParseTask = struct { + // The worker the host started for it, live: a replica has none. + pub const replica = .{ .authority = .{ "future", "io" } }; + job: *ParseJob, + future: ?std.Io.Future(void) = null, + io: ?std.Io = null, +}; /// The colours of the text a scroll is heading into, worked out on a /// worker before it gets there, as a game streams in the level ahead of @@ -134,12 +140,21 @@ pub const PaintJob = struct { } }; -const PaintTask = struct { job: *PaintJob, future: ?std.Io.Future(void) = null, io: ?std.Io = null }; +const PaintTask = struct { + // The worker the host started for it, live: a replica has none. + pub const replica = .{ .authority = .{ "future", "io" } }; + job: *PaintJob, + future: ?std.Io.Future(void) = null, + io: ?std.Io = null, +}; /// A paint's generation has the top bit, so it can never be a search's or /// a parse's (those count from 0 in `gen`, and it would take two billion /// searches to reach it). const paint_bit: u32 = 1 << 31; +/// A query rows' paint's has the next bit too: counted apart from the +/// paints a view's frames ask for (`rows_count`), which are its own. +const rows_bit: u32 = 1 << 30; const max_paint_bytes = 512 << 10; /// Where a worker hands a batch: false when it was turned away (the inbox @@ -172,6 +187,19 @@ pub const Job = struct { const Task = struct { job: *Job, future: std.Io.Future(void), io: std.Io }; +/// What the replay digest leaves out (src/walk.zig): the workers' timings, +/// and the colours painted ahead of a view for its frames, which is the +/// render's own prefetch (a replica that draws nothing paints none). +pub const replica = .{ + // The worker a search runs on, which the host starts. + .authority = .{"task"}, + .local = .{ + "started_ns", "first_ns", "finished_ns", "worker_ns", "parse_ns", + "paint", "paint_retired", "paint_count", "ahead", "ahead_for", + "painted_ahead", "painted_on_loop", "ahead_seen", "ahead_ask", + }, +}; + gen: u32 = 0, state: State = .idle, kind: Kind = .grep, @@ -210,6 +238,10 @@ paint: ?PaintTask = null, /// loop for half a second). No new paint starts while it is out. paint_retired: ?PaintTask = null, paint_count: u32 = 0, +rows_count: u32 = 0, +/// The paint a frame asked for, its worker started once the frame is +/// presented (`askAhead`): starting one is a thread, not a frame's to wait. +ahead_ask: ?u32 = null, ahead: []u8 = &.{}, ahead_for: struct { serial: u32 = 0, revision: u32 = 0, start: usize = 0 } = .{}, /// Stretches a view took painted ahead, and painted on the loop itself @@ -321,7 +353,17 @@ pub fn paintAhead(p: *Pardes, serial: u32, revision: u32, path: []const u8, cont const gen = paint_bit | (s.paint_count & ~paint_bit); job.* = .{ .gpa = p.gpa, .out = p.gpa, .gen = gen, .serial = serial, .revision = revision, .start = from, .path = path_copy, .source = source }; s.paint = .{ .job = job }; - p.emit(.{ .search = .{ .gen = gen } }); + // Asked of the host once the frame is presented, not queued as an + // effect of the session's: a replica that draws nothing asks for none. + s.ahead_ask = gen; +} + +/// The worker for the paint the last frame asked for: the host starts it +/// after the present (Pardes.pump), off the keystroke's way to the screen. +pub fn askAhead(p: *Pardes) void { + const gen = p.stream.ahead_ask orelse return; + p.stream.ahead_ask = null; + if (p.host.vtable.search) |f| f(p.host.ctx, gen); } /// A file's colours for the query rows that show its lines, the whole @@ -363,8 +405,8 @@ pub fn paintRows(p: *Pardes, path: []const u8) void { }; s.rows_asked[s.rows_asked_at] = key; s.rows_asked_at +%= 1; - s.paint_count +%= 1; - const gen = paint_bit | (s.paint_count & ~paint_bit); + s.rows_count +%= 1; + const gen = paint_bit | rows_bit | (s.rows_count & ~(paint_bit | rows_bit)); job.* = .{ .gpa = p.gpa, .out = p.gpa, .gen = gen, .serial = 0, .revision = revision, .start = 0, .path = path_copy, .source = source }; s.rows_paint = .{ .job = job }; p.emit(.{ .search = .{ .gen = gen } }); @@ -470,7 +512,7 @@ pub fn parseFor(p: *Pardes, id: usize) void { // Here and now: a small text, a host with no workers. var got: Collect = .{ .gpa = p.gpa }; defer got.batches.deinit(p.gpa); - parseWork(std.Io.Threaded.global_single_threaded.io(), job, &got, Collect.take); + parseWork(if (comptime pardes.hosted) std.Io.Threaded.global_single_threaded.io() else std.Io.failing, job, &got, Collect.take); for (got.batches.items) |*b| { receive(p, b); b.deinit(p.gpa); @@ -666,19 +708,28 @@ pub fn runHere(p: *Pardes, gen: u32) void { }; defer job.deinit(); job.waits = false; - var got: Collect = .{ .gpa = p.gpa }; - defer got.batches.deinit(p.gpa); - { - pardes.turn.yield(); - defer pardes.turn.back(); - work(std.Io.Threaded.global_single_threaded.io(), job, &got, Collect.take); - } - for (got.batches.items) |*b| { + // Its reads a site of the tape's (a 9P write's Grep or Find runs here): + // a replica takes the batches the session got, as it takes a worker's + // (Event.search), and reads no file. + const batches = pardes.tape.site(.search, p.gpa, searchHere, .{ p.gpa, job }); + defer p.gpa.free(batches); + for (batches) |*b| { receive(p, b); b.deinit(p.gpa); } } +/// The whole search, its batches gpa's. +fn searchHere(gpa: std.mem.Allocator, job: *Job) []Batch { + var got: Collect = .{ .gpa = gpa }; + work(std.Io.Threaded.global_single_threaded.io(), job, &got, Collect.take); + return got.batches.toOwnedSlice(gpa) catch { + for (got.batches.items) |b| b.deinit(gpa); + got.batches.deinit(gpa); + return &.{}; + }; +} + const Collect = struct { gpa: std.mem.Allocator, batches: std.ArrayList(Batch) = .empty, @@ -1145,22 +1196,30 @@ const testing = std.testing; /// worker, hand what it posts to the core. const TestShell = struct { q: host_io.Inbox, + p: ?*Pardes = null, fn init() TestShell { return .{ .q = .{ .gpa = testing.allocator, .lsp_allocator = testing.allocator, .io = testing.io } }; } + /// A shell's search: a worker started for the generation, as the tty's. const vtable: pardes.Host.VTable = .{ .search = struct { - fn f(_: ?*anyopaque, _: u32) void {} + fn f(ctx: ?*anyopaque, gen: u32) void { + const t: *TestShell = @ptrCast(@alignCast(ctx.?)); + start(testing.io, testing.allocator, t.p.?, gen, &t.q, host_io.Inbox.pushSearch); + } }.f }; - fn attach(p: *Pardes) void { - p.host = .{ .vtable = &vtable }; + fn attach(t: *TestShell, p: *Pardes) void { + t.p = p; + p.host = .{ .ctx = t, .vtable = &vtable }; } fn pump(t: *TestShell, p: *Pardes) void { + // What the last frame asked for, as Pardes.pump asks after a present. + askAhead(p); while (p.nextEffect()) |e| switch (e) { - .search => |s| start(testing.io, testing.allocator, p, s.gen, &t.q, host_io.Inbox.pushSearch), + .search => p.perform(e), else => {}, }; var batch: host_io.Inbox.Batch = .{}; @@ -1226,7 +1285,7 @@ test "Stream: a stale generation's rows never land in the query pane, however tw pane.file.?.path = try std.fs.path.join(p.gpa, &.{ root, "main.txt" }); p.advance(1); var shell: TestShell = .init(); - TestShell.attach(p); + shell.attach(p); defer { p.stream.cancelTask(); shell.q.deinit(); @@ -1328,7 +1387,7 @@ test "Stream: a big text's declarations are parsed on a worker, never the loop, pane.file.?.path = try p.gpa.dupe(u8, "/tmp/big.zig"); p.advance(1); var shell: TestShell = .init(); - TestShell.attach(p); + shell.attach(p); defer { p.stream.cancelTask(); p.stream.cancelParse(); @@ -1367,7 +1426,7 @@ test "Stream: a scroll's colours are painted ahead on a worker, so PageDown afte pane.file.?.path = try p.gpa.dupe(u8, "/tmp/big.zig"); p.advance(1); var shell: TestShell = .init(); - TestShell.attach(p); + shell.attach(p); defer { p.stream.cancelTask(); p.stream.cancelParse(); @@ -1410,7 +1469,7 @@ test "zz stream timings on the stress file with a worker (QUERY_TIMES=<path>)" { pane.file.?.path = try p.gpa.dupe(u8, path); p.advance(1); var shell: TestShell = .init(); - TestShell.attach(p); + shell.attach(p); defer { p.stream.cancelTask(); p.stream.cancelParse(); @@ -1464,7 +1523,7 @@ test "Stream: a Grep stops at its 2000 lines and waits; More in the query pane's pane.file.?.path = try std.fs.path.join(p.gpa, &.{ root, "main.txt" }); p.advance(1); var shell: TestShell = .init(); - TestShell.attach(p); + shell.attach(p); defer { p.stream.cancelTask(); shell.q.deinit(); @@ -1508,7 +1567,7 @@ test "zz grep rows timings: a Grep of about 2000 rows on the stress file, a work pane.file.?.path = try p.gpa.dupe(u8, path); p.advance(1); var shell: TestShell = .init(); - TestShell.attach(p); + shell.attach(p); defer { p.stream.cancelTask(); p.stream.cancelParse(); @@ -1588,6 +1647,79 @@ test "zz grep rows timings: a Grep of about 2000 rows on the stress file, a work Stats.print("other frames", idle.items); } +test "Stream: a Grep's rows recoloured are made again, never written, and a dump of them dropped by an edit loads to its digest" { + if (comptime !filesystem.platform_has_fs) return; + if (!syntax.enabled or !syntax.readyApart("c.zig")) return; + var tmp = testing.tmpDir(.{}); + defer tmp.cleanup(); + // Rows enough that a digest keeps a word for them (walk's blob_min). + var text: std.ArrayList(u8) = .empty; + defer text.deinit(testing.allocator); + for (0..80) |i| try text.print(testing.allocator, "pub const value_{d}: u32 = {d};\n", .{ i, i }); + try tmp.dir.writeFile(testing.io, .{ .sub_path = "c.zig", .data = text.items }); + var root_buf: [4096]u8 = undefined; + const root = root_buf[0..try tmp.dir.realPath(testing.io, &root_buf)]; + const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 40 }); + defer p.deinit(); + const pane = try p.setTestFile(text.items); + p.gpa.free(pane.file.?.path); + pane.file.?.path = try std.fs.path.join(p.gpa, &.{ root, "c.zig" }); + var shell: TestShell = .init(); + shell.attach(p); + defer { + p.stream.cancelTask(); + p.stream.cancelPaint(); + shell.q.deinit(); + } + panes.Query.open(p, 0, .grep, "value", false); + const qid = for (p.panes, 0..) |slot, i| { + if (slot) |qp| if (qp.query != null) break i; + } else return error.NoQueryPane; + const f = &p.panes[qid].?.file.?; + const coloured = struct { + fn count(rows: []const @import("locations.zig").Row) usize { + var n: usize = 0; + for (rows) |row| n += @intFromBool(row.kind == .match and row.location_end > 0 and std.mem.indexOfNone(u8, row.colors, &.{0}) != null); + return n; + } + }.count; + var arena = std.heap.ArenaAllocator.init(testing.allocator); + defer arena.deinit(); + const until = now() + 10 * std.time.ns_per_s; + // The rows, plain, digested (their word kept) before their colours land. + while (now() < until and f.location_rows.len < 80) { + p.advance(now()); + shell.pump(p); + p.update(.tick); + testing.io.sleep(.fromMilliseconds(2), .awake) catch {}; + } + try testing.expect(f.location_rows.len >= 80 and coloured(f.location_rows) == 0); + _ = pardes.walk.digest(Pardes, p); + const plain = f.location_rows.ptr; + while (now() < until and (coloured(f.location_rows) < 80 or p.stream.rows_paint != null)) { + p.advance(now()); + shell.pump(p); + p.update(.tick); + _ = arena.reset(.retain_capacity); + _ = try p.render(arena.allocator()); + testing.io.sleep(.fromMilliseconds(2), .awake) catch {}; + } + try testing.expectEqual(@as(usize, 80), coloured(f.location_rows)); + // Made again: a Debug build's digest finds no blob written in place + // (the plain rows' kept word is theirs), and the rows are new ones. + _ = pardes.walk.digest(Pardes, p); + try testing.expect(f.location_rows.ptr != plain); + // An edit drops them with the text they were for (dropped_marks): a + // join's load of the session is it, digest for digest. + panes.File.setContent(p, f, try p.gpa.dupe(u8, "value\n")); + try testing.expect(f.dropped_marks != null); + const bytes = try pardes.dump.dumpBytes(p); + defer p.gpa.free(bytes); + const q = try pardes.dump.load(testing.allocator, p.opts, bytes, null); + defer q.deinit(); + try testing.expectEqual(pardes.walk.digest(panes.File.State, f), pardes.walk.digest(panes.File.State, &q.panes[qid].?.file.?)); +} + test "Stream: a Grep's rows are coloured as their file's language colours them, the file painted on a worker, the match tint on top" { if (comptime !filesystem.platform_has_fs) return; if (!syntax.enabled or !syntax.readyApart("c.zig")) return; @@ -1604,7 +1736,7 @@ test "Stream: a Grep's rows are coloured as their file's language colours them, p.gpa.free(pane.file.?.path); pane.file.?.path = try std.fs.path.join(p.gpa, &.{ root, "c.zig" }); var shell: TestShell = .init(); - TestShell.attach(p); + shell.attach(p); defer { p.stream.cancelTask(); p.stream.cancelPaint(); |
