diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-04 22:51:36 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-04 22:51:36 -0300 |
| commit | 0f46b51232650f2962cd57cded49a4ca8493bc65 (patch) | |
| tree | d6122232b2ec31f8feb792d1507b1a8e848d7c80 | |
| parent | 4dda8c8e7793e50cd719a8cfc1ff8689ca5ec475 (diff) | |
| download | pardes-0f46b51232650f2962cd57cded49a4ca8493bc65.tar.gz pardes-0f46b51232650f2962cd57cded49a4ca8493bc65.zip | |
Stream: one TextJob for a parse and a paint and one join for every worker: ParseJob and PaintJob were one type twice, each made by hand at three sites, and seven places joined a worker in five lines each
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
| -rw-r--r-- | src/Stream.zig | 206 |
1 files changed, 75 insertions, 131 deletions
diff --git a/src/Stream.zig b/src/Stream.zig index 69dc311f..d065e055 100644 --- a/src/Stream.zig +++ b/src/Stream.zig @@ -82,72 +82,67 @@ pub const Batch = struct { } }; -/// A parse of a pane's text for its declarations, on a worker: the rows of -/// a `/` group under them, and Symbols lists them, once it is done. The -/// loop never parses a big text (a 1.2 MB file is seconds in Debug). -pub const ParseJob = struct { +/// A text a worker works on, copied from the core: a pane's text parsed +/// for its declarations (`parseFor`: the rows of a `/` group under them, +/// and Symbols lists them, once it is done; the loop never parses a big +/// text, a 1.2 MB file is seconds in Debug), or its colours painted +/// (`paintAhead`, `paintRows`). +pub const TextJob = struct { gpa: std.mem.Allocator, /// What its answer is made with: the shell's, which frees a batch. out: std.mem.Allocator, - gen: u32, - serial: u32, - revision: u32, - path: []u8, - content: []u8, - /// Set when it is no longer wanted: the parse ends early. - stop: std.atomic.Value(bool) = .init(false), - - pub fn deinit(job: *ParseJob) void { - job.gpa.free(job.path); - job.gpa.free(job.content); - job.gpa.destroy(job); - } -}; - -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 -/// the player: refreshHighlights paints only the rows a view shows (and a -/// view's worth either side), so a scroll past them used to parse the next -/// stretch on the frame (6-58 ms in Debug on a 1.6 MB file, a hitch every -/// few pages). Now it finds them painted already, and only a jump past -/// what was foreseen (ge, a Look) paints on the frame. -/// ponytail: one paint at a time, for the pane last scrolled, in the way it -/// went; a pane per worker if two scroll at once. -pub const PaintJob = struct { - gpa: std.mem.Allocator, - out: std.mem.Allocator, - gen: u32, + gen: u32 = 0, serial: u32, revision: u32, /// Where `source` starts in the text. start: usize, path: []u8, source: []u8, + /// Set when it is no longer wanted: the work ends early. stop: std.atomic.Value(bool) = .init(false), - pub fn deinit(job: *PaintJob) void { + /// A job holding its own copies of `path` and `source`, its `gen` the + /// caller's to set; null when they cannot be had. + fn make(gpa: std.mem.Allocator, serial: u32, revision: u32, from: usize, path: []const u8, source: []const u8) ?*TextJob { + const job = gpa.create(TextJob) catch return null; + const path_copy = gpa.dupe(u8, path) catch { + gpa.destroy(job); + return null; + }; + const source_copy = gpa.dupe(u8, source) catch { + gpa.free(path_copy); + gpa.destroy(job); + return null; + }; + job.* = .{ .gpa = gpa, .out = gpa, .serial = serial, .revision = revision, .start = from, .path = path_copy, .source = source_copy }; + return job; + } + + pub fn deinit(job: *TextJob) void { job.gpa.free(job.path); job.gpa.free(job.source); job.gpa.destroy(job); } }; -const PaintTask = struct { +const TextTask = struct { // The worker the host started for it, live: a replica has none. pub const replica = .{ .authority = .{ "future", "io" } }; - job: *PaintJob, + job: *TextJob, future: ?std.Io.Future(void) = null, io: ?std.Io = null, }; +/// A worker joined, the turn out for it as for any blocking call (it may +/// be reading through a mount of this very session): canceled, or waited +/// for once it has sent its last. +fn join(future: std.Io.Future(void), io: std.Io, how: enum { cancel, wait }) void { + pardes.turn.yield(); + var f = future; + if (how == .cancel) f.cancel(io) else f.await(io); + pardes.turn.back(); +} + /// 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). @@ -225,18 +220,18 @@ finished_ns: u64 = 0, worker_ns: u64 = 0, /// The parse running or about to (one at a time), its generation, and the /// one asked for while it ran (pane serial and revision), started after it. -parse: ?ParseTask = null, +parse: ?TextTask = null, parse_gen: u32 = 0, parse_next: ?struct { serial: u32, revision: u32 } = null, /// What the last parse cost the worker. parse_ns: u64 = 0, /// The paint running (one at a time), its generation's count, and the /// painted stretch it gave, kept until the view gets there (tree_sitter_gpa). -paint: ?PaintTask = null, +paint: ?TextTask = null, /// One told to stop and not waited for: joined when its answer comes (a /// paint of a long line runs on in its query; waiting for it here held the /// loop for half a second). No new paint starts while it is out. -paint_retired: ?PaintTask = null, +paint_retired: ?TextTask = null, paint_count: u32 = 0, rows_count: u32 = 0, /// The paint a frame asked for, its worker started once the frame is @@ -254,7 +249,7 @@ ahead_seen: struct { serial: u32 = 0, scroll: usize = 0, dir: i2 = 1 } = .{}, /// time, its own task beside the scroll's. ponytail: one file at a time, /// the next asked when it lands (Query.recolor); a worker per file if a /// Grep of hundreds of files colours too slowly. -rows_paint: ?PaintTask = null, +rows_paint: ?TextTask = null, /// The files (a hash of path and revision) asked since the last Grep /// began: each asked once, so a file whose colours never fit its rows /// (changed on disk since) is not painted again and again. ponytail: a @@ -279,27 +274,17 @@ pub fn cancelParse(s: *Stream) void { const t = s.parse orelse return; s.parse = null; t.job.stop.store(true, .release); - if (t.future) |f| { - pardes.turn.yield(); - var future = f; - future.cancel(t.io.?); - pardes.turn.back(); - } + if (t.future) |f| join(f, t.io.?, .cancel); t.job.deinit(); } /// The paints' workers told to stop and joined (the session's end). pub fn cancelPaint(s: *Stream) void { - for ([_]*?PaintTask{ &s.paint, &s.paint_retired, &s.rows_paint }) |slot| { + for ([_]*?TextTask{ &s.paint, &s.paint_retired, &s.rows_paint }) |slot| { const t = slot.* orelse continue; slot.* = null; t.job.stop.store(true, .release); - if (t.future) |f| { - pardes.turn.yield(); - var future = f; - future.cancel(t.io.?); - pardes.turn.back(); - } + if (t.future) |f| join(f, t.io.?, .cancel); t.job.deinit(); } } @@ -316,6 +301,16 @@ pub fn takeAhead(p: *Pardes, serial: u32, revision: u32, need_start: usize, need return .{ .styles = s.ahead, .start = a.start }; } +/// 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 +/// the player: refreshHighlights paints only the rows a view shows (and a +/// view's worth either side), so a scroll past them used to parse the next +/// stretch on the frame (6-58 ms in Debug on a 1.6 MB file, a hitch every +/// few pages). Now it finds them painted already, and only a jump past +/// what was foreseen (ge, a Look) paints on the frame. +/// ponytail: one paint at a time, for the pane last scrolled, in the way it +/// went; a pane per worker if two scroll at once. +/// /// Asks a worker to paint bytes `start..end` of pane `serial`'s text ahead /// of its view, unless that is painted or being painted already. Only with /// workers, and only for a grammar a worker can paint (syntax.readyApart). @@ -339,23 +334,13 @@ pub fn paintAhead(p: *Pardes, serial: u32, revision: u32, path: []const u8, cont s.paint = null; } if (!syntax.readyApart(path)) return; - const job = p.gpa.create(PaintJob) catch return; - const path_copy = p.gpa.dupe(u8, path) catch { - p.gpa.destroy(job); - return; - }; - const source = p.gpa.dupe(u8, content[from..to]) catch { - p.gpa.free(path_copy); - p.gpa.destroy(job); - return; - }; + const job = TextJob.make(p.gpa, serial, revision, from, path, content[from..to]) orelse return; s.paint_count +%= 1; - 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 }; + job.gen = paint_bit | (s.paint_count & ~paint_bit); s.paint = .{ .job = job }; // 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; + s.ahead_ask = job.gen; } /// The worker for the paint the last frame asked for: the host starts it @@ -393,29 +378,19 @@ pub fn paintRows(p: *Pardes, path: []const u8) void { if (content) |c| if (c.len > max_rows_paint_bytes) return; const key = std.hash.Wyhash.hash(revision, path) | 1; if (std.mem.indexOfScalar(u64, &s.rows_asked, key) != null) return; - const job = p.gpa.create(PaintJob) catch return; - const path_copy = p.gpa.dupe(u8, path) catch { - p.gpa.destroy(job); - return; - }; - const source = p.gpa.dupe(u8, content orelse "") catch { - p.gpa.free(path_copy); - p.gpa.destroy(job); - return; - }; + const job = TextJob.make(p.gpa, 0, revision, 0, path, content orelse "") orelse return; s.rows_asked[s.rows_asked_at] = key; s.rows_asked_at +%= 1; 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 }; + job.gen = paint_bit | rows_bit | (s.rows_count & ~(paint_bit | rows_bit)); s.rows_paint = .{ .job = job }; - p.emit(.{ .search = .{ .gen = gen } }); + p.emit(.{ .search = .{ .gen = job.gen } }); } /// The biggest file whose colours query rows are painted from. const max_rows_paint_bytes = 4 << 20; -fn paintWork(io: std.Io, job: *PaintJob, ctx: ?*anyopaque, sink: Sink) void { +fn paintWork(io: std.Io, job: *TextJob, ctx: ?*anyopaque, sink: Sink) void { // A file's colours for query rows (paintRows, serial 0) with no text // given: read here, off the loop, and sent back with the colours, the // text they are of. @@ -440,12 +415,7 @@ fn receivePaint(p: *Pardes, b: *const Batch) void { // the rows coloured from them (which asks for the next file). if (s.rows_paint) |t| if (t.job.gen == b.gen) { s.rows_paint = null; - if (t.future) |f| { - pardes.turn.yield(); - var future = f; - future.await(t.io.?); - pardes.turn.back(); - } + if (t.future) |f| join(f, t.io.?, .wait); defer t.job.deinit(); p.locations_cache.put(p.tree_sitter_gpa, t.job.path, b.rows, b.styles) catch {}; panes.Query.recolor(p); @@ -453,14 +423,9 @@ fn receivePaint(p: *Pardes, b: *const Batch) void { return; }; // Its task joined: it has sent its last, so the join is a return away. - for ([_]*?PaintTask{ &s.paint, &s.paint_retired }) |slot| if (slot.*) |t| if (t.job.gen == b.gen) { + for ([_]*?TextTask{ &s.paint, &s.paint_retired }) |slot| if (slot.*) |t| if (t.job.gen == b.gen) { slot.* = null; - if (t.future) |f| { - pardes.turn.yield(); - var future = f; - future.await(t.io.?); - pardes.turn.back(); - } + if (t.future) |f| join(f, t.io.?, .wait); t.job.deinit(); // A retired one's answer is for a stretch nobody wants now. if (slot == &s.paint_retired) return; @@ -494,19 +459,10 @@ pub fn parseFor(p: *Pardes, id: usize) void { } const workers = p.host.vtable.search != null and !p.fs.serving; if (!workers and f.content.len > 64 << 10) return; - const job = p.gpa.create(ParseJob) catch return; - const path = p.gpa.dupe(u8, f.path) catch { - p.gpa.destroy(job); - return; - }; - const content = p.gpa.dupe(u8, f.content) catch { - p.gpa.free(path); - p.gpa.destroy(job); - return; - }; + const job = TextJob.make(p.gpa, pane.serial, f.revision, 0, f.path, f.content) orelse return; s.gen +%= 1; s.parse_gen = s.gen; - job.* = .{ .gpa = p.gpa, .out = p.gpa, .gen = s.parse_gen, .serial = pane.serial, .revision = f.revision, .path = path, .content = content }; + job.gen = s.parse_gen; s.parse = .{ .job = job }; if (workers) return p.emit(.{ .search = .{ .gen = s.parse_gen } }); // Here and now: a small text, a host with no workers. @@ -519,9 +475,9 @@ pub fn parseFor(p: *Pardes, id: usize) void { } } -fn parseWork(io: std.Io, job: *ParseJob, ctx: ?*anyopaque, sink: Sink) void { +fn parseWork(io: std.Io, job: *TextJob, ctx: ?*anyopaque, sink: Sink) void { const began = std.Io.Clock.awake.now(io); - const decls: []syntax.ContextDeclaration = syntax.declarationsApart(job.out, job.path, job.content, &job.stop) catch @constCast(&[_]syntax.ContextDeclaration{}); + const decls: []syntax.ContextDeclaration = syntax.declarationsApart(job.out, job.path, job.source, &job.stop) catch @constCast(&[_]syntax.ContextDeclaration{}); const took: u64 = @intCast(@max(0, began.durationTo(std.Io.Clock.awake.now(io)).nanoseconds)); _ = sink(ctx, .{ .gen = job.gen, .decls = decls, .parsed = .{ .serial = job.serial, .revision = job.revision }, .end = .done, .took_ns = took }); } @@ -533,15 +489,11 @@ fn now() u64 { } /// Joins the worker, if one runs: a cancel interrupts its waits and reads, -/// so this is short. The turn goes out for it, as for any blocking call: -/// the worker may be reading through a mount of this very session. +/// so this is short. pub fn cancelTask(s: *Stream) void { const t = s.task orelse return; s.task = null; - pardes.turn.yield(); - var future = t.future; - future.cancel(t.io); - pardes.turn.back(); + join(t.future, t.io, .cancel); t.job.deinit(); } @@ -633,7 +585,7 @@ pub fn start(io: std.Io, gpa: std.mem.Allocator, p: *Pardes, gen: u32, ctx: ?*an const s = &p.stream; // A paint's generation: its own task, its own worker. if (gen & paint_bit != 0) { - for ([_]*?PaintTask{ &s.paint, &s.rows_paint }) |slot| if (slot.*) |*t| if (t.job.gen == gen and t.future == null) { + for ([_]*?TextTask{ &s.paint, &s.rows_paint }) |slot| if (slot.*) |*t| if (t.job.gen == gen and t.future == null) { t.job.out = gpa; t.future = io.concurrent(paintWork, .{ io, t.job, ctx, sink }) catch { const job = t.job; @@ -690,7 +642,7 @@ pub fn runHere(p: *Pardes, gen: u32) void { // never left to hold its slot; the rows or the view ask again. if (gen & paint_bit != 0) { const s = &p.stream; - for ([_]*?PaintTask{ &s.paint, &s.rows_paint }) |slot| if (slot.*) |t| if (t.job.gen == gen and t.future == null) { + for ([_]*?TextTask{ &s.paint, &s.rows_paint }) |slot| if (slot.*) |t| if (t.job.gen == gen and t.future == null) { slot.* = null; t.job.deinit(); }; @@ -769,10 +721,7 @@ pub fn receive(p: *Pardes, b: *const Batch) void { // It sent its last: its join is a return away. if (s.task) |t| { s.task = null; - pardes.turn.yield(); - var future = t.future; - future.await(t.io); - pardes.turn.back(); + join(t.future, t.io, .wait); t.job.deinit(); } } @@ -791,12 +740,7 @@ fn receiveParse(p: *Pardes, b: *const Batch, serial: u32, revision: u32) void { const s = &p.stream; if (s.parse) |t| if (t.job.gen == b.gen) { s.parse = null; - if (t.future) |f| { - pardes.turn.yield(); - var future = f; - future.await(t.io.?); - pardes.turn.back(); - } + if (t.future) |f| join(f, t.io.?, .wait); t.job.deinit(); }; s.parse_ns = b.took_ns; |
