diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-24 14:38:19 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-24 21:00:18 -0300 |
| commit | 0d3179f04a91c942fc10b0a05cdd6b8aa958d13e (patch) | |
| tree | 2847000be687597a3a1c6963eae4b77f1cae6cf8 /src/Stream.zig | |
| parent | a87f917559e3712d36e5809d1f457fbf78c8266e (diff) | |
| download | pardes-0d3179f04a91c942fc10b0a05cdd6b8aa958d13e.tar.gz pardes-0d3179f04a91c942fc10b0a05cdd6b8aa958d13e.zip | |
anim 5: a scroll's colours are painted ahead on a worker (Stream.paintAhead), two views on the way it goes, so a page after a page parses nothing on the frame
Diffstat (limited to 'src/Stream.zig')
| -rw-r--r-- | src/Stream.zig | 201 |
1 files changed, 201 insertions, 0 deletions
diff --git a/src/Stream.zig b/src/Stream.zig index eb420b42..ace73ca4 100644 --- a/src/Stream.zig +++ b/src/Stream.zig @@ -68,12 +68,17 @@ pub const Batch = struct { /// text at `revision`. parsed: ?struct { serial: u32, revision: u32 } = null, decls: []syntax.ContextDeclaration = &.{}, + /// 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, + styles: []u8 = &.{}, pub const End = enum { done, failed }; pub fn deinit(b: *const Batch, gpa: std.mem.Allocator) void { gpa.free(b.rows); gpa.free(b.decls); + gpa.free(b.styles); } }; @@ -101,6 +106,41 @@ pub const ParseJob = struct { const ParseTask = struct { 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, + serial: u32, + revision: u32, + /// Where `source` starts in the text. + start: usize, + path: []u8, + source: []u8, + stop: std.atomic.Value(bool) = .init(false), + + pub fn deinit(job: *PaintJob) void { + job.gpa.free(job.path); + job.gpa.free(job.source); + job.gpa.destroy(job); + } +}; + +const PaintTask = struct { 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; + /// Where a worker hands a batch: false when it was turned away (the inbox /// closed, the wait for room canceled), and the worker stops. pub const Sink = *const fn (ctx: ?*anyopaque, batch: Batch) bool; @@ -161,10 +201,23 @@ 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_count: u32 = 0, +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 +/// (the tests count them). +painted_ahead: u32 = 0, +painted_on_loop: u32 = 0, +/// The view the last paint was asked from, and which way it was going. +ahead_seen: struct { serial: u32 = 0, scroll: usize = 0, dir: i2 = 1 } = .{}, pub fn deinit(s: *Stream, gpa: std.mem.Allocator) void { s.cancelTask(); s.cancelParse(); + s.cancelPaint(); s.pattern.deinit(gpa); s.dir.deinit(gpa); } @@ -184,6 +237,96 @@ pub fn cancelParse(s: *Stream) void { t.job.deinit(); } +/// The paint's worker told to stop and joined. +pub fn cancelPaint(s: *Stream) void { + const t = s.paint orelse return; + s.paint = null; + t.job.stop.store(true, .release); + if (t.future) |f| { + pardes.turn.yield(); + var future = f; + future.cancel(t.io.?); + pardes.turn.back(); + } + t.job.deinit(); +} + +/// The painted stretch kept for pane `serial`'s text at `revision`, if it +/// covers bytes `need_start..need_end`: handed over (the caller owns it, +/// tree_sitter_gpa), with where it starts. +pub fn takeAhead(p: *Pardes, serial: u32, revision: u32, need_start: usize, need_end: usize) ?struct { styles: []u8, start: usize } { + const s = &p.stream; + const a = s.ahead_for; + if (s.ahead.len == 0 or a.serial != serial or a.revision != revision) return null; + if (need_start < a.start or need_end > a.start + s.ahead.len) return null; + defer s.ahead = &.{}; + return .{ .styles = s.ahead, .start = a.start }; +} + +/// 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). +pub fn paintAhead(p: *Pardes, serial: u32, revision: u32, path: []const u8, content: []const u8, from: usize, to: usize) void { + const s = &p.stream; + if (p.host.vtable.search == null or p.fs.serving or to <= from or to > content.len) return; + const a = s.ahead_for; + if (s.ahead.len != 0 and a.serial == serial and a.revision == revision and from >= a.start and to <= a.start + s.ahead.len) return; + if (s.paint) |t| { + // One under way for the same text that reaches into the stretch + // asked for is let finish: held keys move the stretch every frame, + // and a paint restarted every frame would never land. + if (t.job.serial == serial and t.job.revision == revision and t.job.start < to and t.job.start + t.job.source.len > from) return; + s.cancelPaint(); + } + 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; + }; + 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 }; + s.paint = .{ .job = job }; + p.emit(.{ .search = .{ .gen = gen } }); +} + +fn paintWork(job: *PaintJob, ctx: ?*anyopaque, sink: Sink) void { + const styles = syntax.highlightApart(job.out, job.path, job.source, &job.stop) catch return; + if (!sink(ctx, .{ .gen = job.gen, .styles = styles, .painted = .{ .serial = job.serial, .revision = job.revision, .start = job.start }, .end = .done })) return; +} + +/// A paint's answer: its task joined, its stretch kept for when the view +/// gets there, if the text is still what was painted. +fn receivePaint(p: *Pardes, b: *const Batch) void { + const s = &p.stream; + const painted = b.painted.?; + if (s.paint) |t| if (t.job.gen == b.gen) { + s.paint = null; + if (t.future) |f| { + pardes.turn.yield(); + var future = f; + future.await(t.io.?); + pardes.turn.back(); + } + t.job.deinit(); + }; + const id = p.paneBySerial(painted.serial) orelse return; + const f = p.panes[id].?.file orelse return; + if (f.revision != painted.revision or b.styles.len == 0) return; + const copy = p.tree_sitter_gpa.dupe(u8, b.styles) catch return; + if (s.ahead.len > 0) p.tree_sitter_gpa.free(s.ahead); + s.ahead = copy; + s.ahead_for = .{ .serial = painted.serial, .revision = painted.revision, .start = painted.start }; + // The next frame looks again: the view may be there already. + p.panes[id].?.file.?.syntax_dirty = true; +} + /// Pane `id`'s declarations asked for, as its text is now, unless a parse of /// that text is already on its way. With workers, on one; without (a test), /// here only for a small text, else not at all (the rows stay ungrouped). @@ -338,6 +481,20 @@ pub fn wake(s: *const Stream, next_frame: u64) ?u64 { /// inbox). Called by the shell for the `search` effect, on the loop. pub fn start(io: std.Io, gpa: std.mem.Allocator, p: *Pardes, gen: u32, ctx: ?*anyopaque, sink: Sink) void { const s = &p.stream; + // A paint's generation: its own task, its own worker. + if (gen & paint_bit != 0) { + if (s.paint) |*t| if (t.job.gen == gen and t.future == null) { + t.job.out = gpa; + t.future = io.concurrent(paintWork, .{ t.job, ctx, sink }) catch { + const job = t.job; + s.paint = null; + job.deinit(); + return; + }; + t.io = io; + }; + return; + } // A parse's generation: its own task. if (s.parse) |*t| if (gen == s.parse_gen and t.job.gen == gen and t.future == null) { t.job.out = gpa; @@ -424,6 +581,7 @@ 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.painted != null) return receivePaint(p, b); if (b.gen != s.gen or (s.state != .searching and s.state != .capped)) { s.dropped += 1; return; @@ -1079,6 +1237,49 @@ test "Stream: a big text's declarations are parsed on a worker, never the loop, try testing.expect(p.stream.parse == null); } +test "Stream: a scroll's colours are painted ahead on a worker, so PageDown after PageDown parses nothing on the frame" { + if (comptime !filesystem.platform_has_fs or !syntax.enabled) return; + const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 40 }); + defer p.deinit(); + var text: std.ArrayList(u8) = .empty; + defer text.deinit(testing.allocator); + for (0..6000) |i| try text.print(testing.allocator, "fn f{d}() void {{\n _ = \"needle\";\n}}\n", .{i}); + const pane = try p.setTestFile(text.items); + p.gpa.free(pane.file.?.path); + pane.file.?.path = try p.gpa.dupe(u8, "/tmp/big.zig"); + p.advance(1); + var shell: TestShell = .init(); + TestShell.attach(p); + defer { + p.stream.cancelTask(); + p.stream.cancelParse(); + p.stream.cancelPaint(); + shell.q.deinit(); + } + var arena: std.heap.ArenaAllocator = .init(testing.allocator); + defer arena.deinit(); + _ = try p.render(arena.allocator()); + const first = p.stream.painted_on_loop; + for (0..20) |_| { + // The worker has had its time (a frame's work at a held key's pace + // is all it needs; here it is waited for, so the test is not a race). + const until = std.Io.Clock.awake.now(testing.io).toMilliseconds() + 60_000; + while (p.stream.paint != null) { + if (std.Io.Clock.awake.now(testing.io).toMilliseconds() > until) return error.Timeout; + shell.pump(p); + testing.io.sleep(.fromMilliseconds(1), .awake) catch {}; + } + p.update(.{ .key = .{ .cp = pardes.Key.page_down } }); + _ = arena.reset(.retain_capacity); + _ = try p.render(arena.allocator()); + // Coloured: the rows shown have their colours. + const f = &pane.file.?; + try testing.expect(f.highlights.len > 0 and f.highlight_start <= f.line_starts[f.scroll]); + } + try testing.expect(p.stream.painted_on_loop == first); + try testing.expect(p.stream.painted_ahead >= 5); +} + test "zz stream timings on the stress file with a worker (QUERY_TIMES=<path>)" { const path_z = std.c.getenv("QUERY_TIMES") orelse return error.SkipZigTest; const path = std.mem.span(path_z); |
