diff options
Diffstat (limited to 'src/Stream.zig')
| -rw-r--r-- | src/Stream.zig | 272 |
1 files changed, 265 insertions, 7 deletions
diff --git a/src/Stream.zig b/src/Stream.zig index e42d4a16..da7d12eb 100644 --- a/src/Stream.zig +++ b/src/Stream.zig @@ -218,6 +218,20 @@ 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 } = .{}, +/// A file's colours for query rows being worked out (paintRows), one at a +/// 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, +/// 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 +/// ring of 256; the oldest forgotten. +rows_asked: [256]u64 = @splat(0), +rows_asked_at: u8 = 0, +/// Rows asked for their colours while a 9P write was served: asked again +/// on the next step (Query.produceAll). +rows_later: bool = false, pub fn deinit(s: *Stream, gpa: std.mem.Allocator) void { s.cancelTask(); @@ -244,7 +258,7 @@ pub fn cancelParse(s: *Stream) void { /// 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 }) |slot| { + for ([_]*?PaintTask{ &s.paint, &s.paint_retired, &s.rows_paint }) |slot| { const t = slot.* orelse continue; slot.* = null; t.job.stop.store(true, .release); @@ -310,11 +324,69 @@ pub fn paintAhead(p: *Pardes, serial: u32, revision: u32, path: []const u8, cont p.emit(.{ .search = .{ .gen = gen } }); } -fn paintWork(job: *PaintJob, ctx: ?*anyopaque, sink: Sink) void { +/// A file's colours for the query rows that show its lines, the whole +/// file painted on a worker (rows coloured as the file's language colours +/// them, as Callstack's are): the text of the pane that shows it, else read +/// from disk there. Kept in the locations cache when it lands, where the +/// rows find them (Query.Shades). Not without workers (the rows stay +/// plain), nor for a file bigger than `max_rows_paint_bytes`, nor twice for +/// one revision of a file. +pub fn paintRows(p: *Pardes, path: []const u8) void { + const s = &p.stream; + if (s.rows_paint != null or p.host.vtable.search == null) return; + // Asked while a 9P write is served (a script's Grep): not now, as no + // paint is (the write would wait for it to settle); the next step asks. + if (p.fs.serving) { + s.rows_later = true; + return; + } + if (!syntax.readyApart(path)) return; + var content: ?[]const u8 = null; + var revision: u32 = 0; + for (p.panes) |slot| if (slot) |pane| if (pane.file) |f| if (f.output == null and std.mem.eql(u8, f.path, path)) { + content = f.content; + revision = f.revision; + break; + }; + 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; + }; + 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); + 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 } }); +} + +/// 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 { + // 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. + const read: []u8 = if (job.serial == 0 and job.source.len == 0) + std.Io.Dir.cwd().readFileAlloc(io, job.path, job.out, .limited(max_rows_paint_bytes)) catch @constCast(&[_]u8{}) + else + @constCast(&[_]u8{}); + const source = if (read.len > 0) read else job.source; // Stopped or failed, it still answers (with nothing), so the loop // knows to join it. - const styles: []u8 = syntax.highlightApart(job.out, job.path, job.source, &job.stop) catch @constCast(&[_]u8{}); - _ = sink(ctx, .{ .gen = job.gen, .styles = styles, .painted = .{ .serial = job.serial, .revision = job.revision, .start = job.start }, .end = .done }); + const styles: []u8 = syntax.highlightApart(job.out, job.path, source, &job.stop) catch @constCast(&[_]u8{}); + const text: []u8 = if (job.serial != 0 or read.len > 0) read else job.out.dupe(u8, job.source) catch @constCast(&[_]u8{}); + _ = sink(ctx, .{ .gen = job.gen, .rows = text, .styles = styles, .painted = .{ .serial = job.serial, .revision = job.revision, .start = job.start }, .end = .done }); } /// A paint's answer: its task joined, its stretch kept for when the view @@ -322,6 +394,22 @@ fn paintWork(job: *PaintJob, ctx: ?*anyopaque, sink: Sink) void { fn receivePaint(p: *Pardes, b: *const Batch) void { const s = &p.stream; const painted = b.painted.?; + // A file's colours for query rows: kept where the rows find them, and + // 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(); + } + defer t.job.deinit(); + p.locations_cache.put(p.tree_sitter_gpa, t.job.path, b.rows, b.styles) catch {}; + panes.Query.recolor(p); + p.needs_frame = true; + 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) { slot.* = null; @@ -436,6 +524,7 @@ pub fn query(p: *Pardes, target: u32, kind: Kind, pattern: []const u8, dir: []co s.first_ns = null; s.state = .waiting; s.due_ns = if (how == .typed) p.now_ns + debounce_ns else p.now_ns; + if (kind == .grep) s.rows_asked = @splat(0); } /// Over: the worker canceled, nothing more of it lands. @@ -502,11 +591,11 @@ 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) { - if (s.paint) |*t| if (t.job.gen == gen and t.future == null) { + for ([_]*?PaintTask{ &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, .{ t.job, ctx, sink }) catch { + t.future = io.concurrent(paintWork, .{ io, t.job, ctx, sink }) catch { const job = t.job; - s.paint = null; + slot.* = null; job.deinit(); return; }; @@ -555,6 +644,16 @@ fn makeJob(gpa: std.mem.Allocator, s: *const Stream) !*Job { /// the whole search on the loop, the turn out for it, its batches taken in /// as a worker's would be. Its cap is a hard stop. pub fn runHere(p: *Pardes, gen: u32) void { + // A paint with no worker to run it (a shell not threaded yet): let go, + // 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) { + slot.* = null; + t.job.deinit(); + }; + return; + } if (comptime !filesystem.platform_has_fs) { p.stream.state = .failed; return; @@ -1395,3 +1494,162 @@ test "Stream: a Grep stops at its 2000 lines and waits; More in the query pane's try testing.expectEqual(@as(usize, 3000), qp.query.?.found.items.len); try testing.expect(!qp.query.?.capped); } + +test "zz grep rows timings: a Grep of about 2000 rows on the stress file, a worker (GREP_TIMES=<path>)" { + const Key = pardes.Key; + const path_z = std.c.getenv("GREP_TIMES") orelse return error.SkipZigTest; + const path = std.mem.span(path_z); + const bytes = try std.Io.Dir.cwd().readFileAlloc(testing.io, path, testing.allocator, .limited(8 << 20)); + defer testing.allocator.free(bytes); + const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 50 }); + defer p.deinit(); + const pane = try p.setTestFile(bytes); + p.gpa.free(pane.file.?.path); + pane.file.?.path = try p.gpa.dupe(u8, path); + p.advance(1); + var shell: TestShell = .init(); + TestShell.attach(p); + defer { + p.stream.cancelTask(); + p.stream.cancelParse(); + p.stream.cancelPaint(); + shell.q.deinit(); + } + var frame = std.heap.ArenaAllocator.init(testing.allocator); + defer frame.deinit(); + _ = try p.render(frame.allocator()); + const Stats = struct { + fn print(name: []const u8, xs: []u64) void { + if (xs.len == 0) return; + std.mem.sort(u64, xs, {}, std.sort.asc(u64)); + std.debug.print("{s}: n {d} p50 {d} us p90 {d} us max {d} us\n", .{ name, xs.len, xs[xs.len / 2] / 1000, xs[xs.len * 9 / 10] / 1000, xs[xs.len - 1] / 1000 }); + } + }; + var keys: std.ArrayList(u64) = .empty; + defer keys.deinit(testing.allocator); + var refills: std.ArrayList(u64) = .empty; + defer refills.deinit(testing.allocator); + var idle: std.ArrayList(u64) = .empty; + defer idle.deinit(testing.allocator); + panes.Query.open(p, 0, .grep, "", true); + const qid = p.active; + const q = p.panes[qid].?.query.?; + for (0..3) |round| { + for ("pane") |c| { + const t0 = now(); + p.advance(t0); + p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); + _ = frame.reset(.retain_capacity); + _ = try p.render(frame.allocator()); + try keys.append(testing.allocator, now() - t0); + // the frames till the next key: the debounce, the batches, the paints + const until = now() + 400 * std.time.ns_per_ms; + while (now() < until) { + const made = q.made_gen; + const t1 = now(); + p.advance(t1); + shell.pump(p); + p.update(.tick); + _ = frame.reset(.retain_capacity); + _ = try p.render(frame.allocator()); + try (if (q.made_gen != made) &refills else &idle).append(testing.allocator, now() - t1); + testing.io.sleep(.fromMilliseconds(2), .awake) catch {}; + } + } + // back to an empty pattern for the next round + if (round < 2) for (0..4) |_| p.update(.{ .key = .{ .cp = Key.backspace } }); + } + // Enter: the whole list frozen, 2000 rows, and the frames after it. + var after: std.ArrayList(u64) = .empty; + defer after.deinit(testing.allocator); + const t3 = now(); + p.advance(t3); + p.update(.{ .key = .{ .cp = Key.enter } }); + _ = frame.reset(.retain_capacity); + _ = try p.render(frame.allocator()); + const t_enter = now() - t3; + const until = now() + 1000 * std.time.ns_per_ms; + while (now() < until) { + const t1 = now(); + p.advance(t1); + shell.pump(p); + p.update(.tick); + _ = frame.reset(.retain_capacity); + _ = try p.render(frame.allocator()); + try after.append(testing.allocator, now() - t1); + testing.io.sleep(.fromMilliseconds(2), .awake) catch {}; + } + var coloured: usize = 0; + for (p.panes[qid].?.file.?.location_rows) |row| coloured += @intFromBool(row.kind == .match and std.mem.indexOfNone(u8, row.colors, &.{0}) != null); + std.debug.print("grep rows: {d} found ({s}), {d} rows frozen, {d} coloured; Enter (2000 rows written) {d} us\n", .{ q.found.items.len, @tagName(p.stream.used), p.panes[qid].?.file.?.location_rows.len, coloured, t_enter / 1000 }); + Stats.print("frames after Enter", after.items); + Stats.print("keystroke (update + render)", keys.items); + Stats.print("refill frame (pump + tick + render, rows remade)", refills.items); + Stats.print("other frames", idle.items); +} + +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; + var tmp = testing.tmpDir(.{}); + defer tmp.cleanup(); + const text = "const std = @import(\"std\");\npub fn value() u32 {\n return 42; // value\n}\n"; + try tmp.dir.writeFile(testing.io, .{ .sub_path = "c.zig", .data = text }); + 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(); + // the file open in the pane that asks: its text painted, not the disk's + const pane = try p.setTestFile(text); + 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); + 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.?; + var halt: std.atomic.Value(bool) = .init(false); + const own = try syntax.highlightApart(testing.allocator, "c.zig", text, &halt); + defer testing.allocator.free(own); + var arena = std.heap.ArenaAllocator.init(testing.allocator); + defer arena.deinit(); + const until = now() + 10 * std.time.ns_per_s; + var coloured: usize = 0; + while (now() < until) { + p.advance(now()); + shell.pump(p); + p.update(.tick); + // a frame each, as a shell draws: the rows painted before their colours land + _ = arena.reset(.retain_capacity); + _ = try p.render(arena.allocator()); + coloured = 0; + for (f.location_rows) |row| if (row.kind == .match and row.location_end > 0 and std.mem.indexOfNone(u8, row.colors, &.{0}) != null) { + coloured += 1; + }; + if (coloured == 2 and p.stream.rows_paint == null) break; + testing.io.sleep(.fromMilliseconds(2), .awake) catch {}; + } + try testing.expectEqual(@as(usize, 2), coloured); + // and painted: the frame's colours are the rows' + _ = arena.reset(.retain_capacity); + _ = try p.render(arena.allocator()); + // each row's colours its line's, byte for byte; the match tinted over them + var lines = std.mem.splitScalar(u8, f.content, '\n'); + for (f.location_rows) |row| { + const line = lines.next().?; + if (row.kind != .match or row.location_end == 0) continue; + const ls = @import("modal.zig").lineStartOffset(text, row.at.line - 1); + try testing.expectEqualSlices(u8, own[ls + row.source_column ..][0..row.colors.len], row.colors); + const at = @intFromPtr(line.ptr) - @intFromPtr(f.content.ptr) + row.code_start - f.highlight_start; + try testing.expectEqualSlices(u8, row.colors, f.highlights[at..][0..row.colors.len]); + const dec = panes.Output.decorateRow(f.output, line, row); + try testing.expectEqualStrings("value", line[dec.mark_start..dec.mark_end]); + } +} |
