diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-19 21:16:22 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-20 00:01:23 -0300 |
| commit | 6ca2655317e7a576dd74ab23c1bb69bba020641b (patch) | |
| tree | 669e392120658987e5b82acf7bb5a5c316e5eae5 /core | |
| parent | 26acd94bc20b70fe924ad76dee9c2ddaa1f4011f (diff) | |
| download | pardes-6ca2655317e7a576dd74ab23c1bb69bba020641b.tar.gz pardes-6ca2655317e7a576dd74ab23c1bb69bba020641b.zip | |
far client: a text's declarations a worker parsed are a dump's (its revision dumped and digested, the declarations parsed again from the text at the load), and a query's rows read no others, so a client joined from a dump makes a `/` query's rows as its session does: a `/` (or Symbols) in a file groups its rows under the declarations they are in once a parse of the text exists (Query.ownerDecls), the pane's `context` (sticky headers', a worker's answer, sync's) or the locations cache's (a Diff's); both were kept out of the dump as made again from the text, which a worker's answer is not (an input a client joined since never saw), so a client joined after the session's worker parsed a file found none, and its replay of the session's next row in that query asked for a parse the session never asked: `diverged: the core called search where the tape has a host` (a real session's: tty and window run parses on workers; a user with a far client who had typed a `/` in a file before it joined, then any key in that query, saw the client join again); now `File.State.context` leaves File.State's `.derived`: `ContextCache.revision` is dumped and digested, its `declarations` derived and its `allocator` local (root.zig's kept_out says why), and a load (dump.zig's loadKnowing and loadImage) parses the declarations again from the text, with the syntax allocator, where the dumped revision is the text's (`ContextCache.reload`): never loaded with the core's allocator (dump.zig's test of that, which pinned that the context was not restored at all, now pins it parsed again; a first try that dumped the declarations themselves crashed a Restore's rollback in `owned cwd restore ...`), and `ownerDecls` takes the pane's context alone (the cache's loop and the never-used `small` parse here deleted: a Diff's parse no longer groups a query's rows, the worker's does); the test `a client joined from a dump makes a `/` query's rows as the session does, though only the session has the owner's declarations from a worker's parse` (end of core/replica.zig, a host whose parses run on a worker as a tty's, their batches the session's inputs) failed on the base with that divergence and passes; Places' rail ticks: by reading `gen` moves wherever the hits do (clear, feed, shift, restart, a search's new hits, the current one), and the test `a client joined from a dump lands a click on a rail's tick as the session does, the session's ticks drawn before the places changed` (the session draws its ticks, the client changes the places and clicks every row of the rail) passes on the base; it shows a fair misprediction of the key that changed the places, 3 runs in 4: a step's places are a frame's time of search (`tape.over`), and the session's drawn caches find more in it than a cold client; gates on the seven together (the stack's top, load 18 to 50 beside them): tui core-test 855/999 (144 skipped), shell-test 1035/1038, pane-test 183/184, term-test 15/15, kitty-test 45/45, behaviour-test 275/275 and --replay 20 seeds 0 failures, process-test 3 of 144 (lspcomplete and lspcompletemoved, the base's, and headersweep-restored, which passes alone), gui unit-test 1633/1637 with the base's BaselineHarnessMismatch; replicas --clients 5 and --seeds 8: diverged 0, unequal 0, deltas wrong 0, quiet checks differed 0, unlisted stops 0, corpus 22 of 22, no leak report; the walk over every word and key passes; frame and perf gate against tree1 (farfaults): frames 77/77 byte-identical in Debug, unit-test 1633/1637, perfab corpus2x -0.087% (noise 0.092%, bar 0.4%, pictures identical), perfops 97 counts within 0.1% (largest +0.0329%)
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core')
| -rw-r--r-- | core/File.zig | 18 | ||||
| -rw-r--r-- | core/Query.zig | 34 | ||||
| -rw-r--r-- | core/dump.zig | 12 | ||||
| -rw-r--r-- | core/replica.zig | 118 | ||||
| -rw-r--r-- | core/root.zig | 3 |
5 files changed, 154 insertions, 31 deletions
diff --git a/core/File.zig b/core/File.zig index 020985da..75d8049b 100644 --- a/core/File.zig +++ b/core/File.zig @@ -59,8 +59,13 @@ pub const History = struct { } }; -/// Owned syntax results. Excluded from dumps as State.context is derived. +/// Owned syntax results: a text's declarations at a revision. pub const ContextCache = struct { + /// Its revision is dumped and digested: a worker's parse is an input, + /// which a client joined since never saw, and a step reads what it + /// left (Query.ownerDecls). The declarations are made again from the + /// text at a load (`reload`), with the allocator that made them. + pub const replica = .{ .local = .{"allocator"}, .derived = .{"declarations"} }; allocator: ?std.mem.Allocator = null, declarations: []syntax.ContextDeclaration = &.{}, revision: ?u32 = null, @@ -70,6 +75,15 @@ pub const ContextCache = struct { self.* = .{}; } + /// A loaded text's (its revision the dump's): its declarations parsed + /// again where they are of the text it has. + pub fn reload(self: *ContextCache, gpa: std.mem.Allocator, path: []const u8, content: []const u8, revision: u32) void { + const at = self.revision orelse return; + if (at != revision) return; + const declarations = syntax.contextDeclarations(gpa, path, content) catch return; + self.replace(gpa, declarations, at); + } + /// Takes ownership of declarations allocated by gpa. pub fn replace(self: *ContextCache, gpa: std.mem.Allocator, declarations: []syntax.ContextDeclaration, revision: u32) void { self.deinit(); @@ -83,7 +97,7 @@ pub const State = struct { /// from the text whenever it is wanted. // Syntax caches must not load through the dump loader's core allocator. pub const replica = .{ - .derived = .{ "cap", "line_starts", "line_cap", "highlights", "highlight_start", "syntax_dirty", "context", "node_tree", "node_tree_revision", "node_tree_edits", "node_history", "tables" }, + .derived = .{ "cap", "line_starts", "line_cap", "highlights", "highlight_start", "syntax_dirty", "node_tree", "node_tree_revision", "node_tree_edits", "node_history", "tables" }, // Big texts, dumped and sent by their hash (core/walk.zig). The // text is written in place by `replace`, but never while a join // sends it (memory.zig `pin`): then an edit puts a new buffer in diff --git a/core/Query.zig b/core/Query.zig index dacc11a1..02e17a1b 100644 --- a/core/Query.zig +++ b/core/Query.zig @@ -754,7 +754,7 @@ fn startSource(p: *Pardes, id: usize, q: *Query, source: Source, owner_id: ?usiz const oid = owner_id orelse return; const owner = p.panes[oid].?; const f = if (owner.file) |*file| file else return; - const decls = ownerDecls(p, f, false) orelse { + const decls = ownerDecls(f) orelse { // Parsed on the worker; listed when it is done (declsArrived). Stream.parseFor(p, oid); return; @@ -782,31 +782,15 @@ fn recentRows(p: *Pardes, out: *std.Io.Writer.Allocating) []const u8 { return out.written(); } -/// The owner's declarations, from a parse that already exists: its sticky -/// headers' (TreeContext) at this revision, or the locations cache's. None -/// is made here, on the loop, unless `small` and the text is small: a -/// tree-sitter parse of a 1.2 MB file is seconds in a Debug build. -/// ponytail: no grouping for a big file until the disk engine's worker -/// parses it (phase 4). -fn ownerDecls(p: *Pardes, f: *File.State, small: bool) ?[]const syntax.ContextDeclaration { +/// The owner's declarations, from its parse at this revision (its context: +/// sticky headers' (TreeContext), a worker's answer, a dump's load), or +/// none: never the locations cache's (a Diff's), which a client joined +/// since has not, nor a parse made here (Stream.parseFor asks one). +fn ownerDecls(f: *const File.State) ?[]const syntax.ContextDeclaration { if (f.context.revision) |rev| if (rev == f.revision and f.context.declarations.len > 0) return f.context.declarations; - const cache = &p.locations_cache; - for (cache.entries[0..cache.len]) |entry| { - if (entry.declarations_ready and std.mem.eql(u8, entry.path, f.path) and std.mem.eql(u8, entry.source, f.content)) return entry.analysis.declarations; - } - if (!small or f.content.len > small_parse_bytes or !syntax.supportsPath(f.path)) return null; - var got = p.locations_cache.get(p.tree_sitter_gpa, f.path, f.content, true, false) catch return null; - if (got.owned) { - got.deinit(p.tree_sitter_gpa); - return null; - } - return got.analysis.declarations; + return null; } -/// A text this small is parsed on the loop for its declarations when a -/// Symbols query asks (about 40 ms in a Debug build). -const small_parse_bytes = 64 << 10; - /// The rows as `note` says, and nothing else: no hits. fn noteRows(p: *Pardes, id: usize, note: []const u8) void { const rp = p.panes[id].?; @@ -1043,7 +1027,7 @@ fn writePlacesRows(p: *Pardes, q: *Query, out: *std.ArrayList(u8), rows: *std.Ar const rel = lookPath(&relbuf, path, dir); const width = addressWidth(rel, index.len); var sh: Shades = .{}; - const decls = ownerDecls(p, f, false) orelse &.{}; + const decls = ownerDecls(f) orelse &.{}; var decl_now: ?usize = null; var i: usize = @min(from, hits.len); // the line of the first, found in the index @@ -1107,7 +1091,7 @@ fn makeTextRows(p: *Pardes, id: usize, oid: usize, full: bool) !void { var rows: std.ArrayList(locations.Row) = .empty; var budget: usize = if (full) max_rows else live_rows; // Grouped once a parse of the text exists: asked of the worker now. - if (ownerDecls(p, &owner.file.?, false) == null) Stream.parseFor(p, oid); + if (ownerDecls(&owner.file.?) == null) Stream.parseFor(p, oid); const at = set.current orelse (if (set.seek) |s| Places.firstFrom(set.hits.items, s) else 0); // While typed, a window of rows from just before the current place, so // it sits under line 1. diff --git a/core/dump.zig b/core/dump.zig index 1616f557..3b2b22e2 100644 --- a/core/dump.zig +++ b/core/dump.zig @@ -406,6 +406,7 @@ fn loadKnowing(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, notes: for (others) |o| gpa.free(o.panes); gpa.free(others); l.shared.shrinkRetainingCapacity(at); + for (p.panes) |slot| if (slot) |pane| if (pane.file) |*f| f.context.reload(p.tree_sitter_gpa, f.path, f.content, f.revision); p.readSessionFacts(); return p; } @@ -491,6 +492,7 @@ fn loadImage(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, notes: ?* try views.undumpedOld(p, others, gpa); } l.made.shrinkRetainingCapacity(at); + for (p.panes) |slot| if (slot) |pane| if (pane.file) |*f| f.context.reload(p.tree_sitter_gpa, f.path, f.content, f.revision); p.readSessionFacts(); return p; } @@ -1200,7 +1202,7 @@ test "an old dump's file, a byte dump, is read by -l and by a Restore of its pat try std.testing.expectEqualSlices(u8, bytes, again); } -test "restored syntax context caches are rebuilt rather than loaded with the core allocator" { +test "restored syntax context caches are rebuilt from the text rather than loaded with the core allocator" { const gpa = std.testing.allocator; var syntax_gpa: std.heap.DebugAllocator(.{}) = .init; defer std.testing.expect(syntax_gpa.deinit() == .ok) catch @panic("syntax allocator leaked"); @@ -1237,9 +1239,13 @@ test "restored syntax context caches are rebuilt rather than loaded with the cor defer gpa.free(bytes); const q = try load(gpa, opts, bytes, null); defer q.deinit(); + // This build's: the revision kept, the declarations parsed again from + // the text, with the syntax allocator. + const want = try pardes.syntax.contextDeclarations(syntax_gpa.allocator(), f.path, f.content); + defer syntax_gpa.allocator().free(want); for (q.panes) |slot| if (slot) |restored| if (restored.file) |file| { - try std.testing.expectEqual(@as(usize, 0), file.context.declarations.len); - try std.testing.expectEqual(@as(?u32, null), file.context.revision); + try std.testing.expectEqual(f.context.revision, file.context.revision); + try std.testing.expectEqualSlices(pardes.syntax.ContextDeclaration, want, file.context.declarations); }; } diff --git a/core/replica.zig b/core/replica.zig index 342913ba..bc5eafd1 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -6159,3 +6159,121 @@ test "a far client's click into a shell whose queue is backed up: the session's try std.testing.expectEqual(@as(u32, 0), r.stats.mispredicted); try agrees(a, r); } + +// A `/` in a file groups its rows under the declarations they are in once +// a parse of the text exists (Query.ownerDecls): the pane's context, a +// worker's answer, or the locations cache's (a Diff's). Both were kept out +// of the dump as made again from the text, so a client joined since had +// neither, and its replay of the session's next row asked a parse the +// session never asked: the context is dumped now, the cache's no longer read. +test "a client joined from a dump makes a `/` query's rows as the session does, though only the session has the owner's declarations from a worker's parse" { + // A session's host whose parses and searches run on a worker (a tty's, + // Stream.start), their batches posted to an inbox; `land` hands them to + // the core as the session's inputs, as its loop does. + const WorkerSearch = struct { + const Inbox = @import("dev").host_io.Inbox; + var q: Inbox = undefined; + var core: ?*Pardes = null; + fn search(_: ?*anyopaque, gen: u32) void { + pardes.Stream.start(std.testing.io, std.testing.allocator, core.?, gen, &q, Inbox.pushSearch); + } + const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .search = search }; + fn init(a: *Authority) void { + q = .{ .gpa = std.testing.allocator, .lsp_allocator = std.testing.allocator, .io = std.testing.io }; + core = a.core; + } + // The batches posted until the parse asked has answered (a second's + // wait at most), each a unit the replica takes. + fn land(a: *Authority, r: ?*Replica) !void { + var waited: u32 = 0; + while (a.core.stream.parse != null) : (waited += 1) { + if (waited == 1000) return error.Timeout; + var batch: Inbox.Batch = .{}; + q.takeInto(&batch); + for (batch.slice()) |m| switch (m) { + .search => |b| { + const u = try a.local(.{ .search = b }); + if (r) |c| { + c.confirm(u); + c.settle(); + } + b.deinit(std.testing.allocator); + }, + else => m.deinit(std.testing.allocator, std.testing.allocator), + }; + if (a.core.stream.parse != null) { + const rest: std.c.timespec = .{ .sec = 0, .nsec = std.time.ns_per_ms }; + _ = std.c.nanosleep(&rest, null); + } + } + } + }; + if (comptime !pardes.syntax.enabled) return error.SkipZigTest; + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, .{ .vtable = &WorkerSearch.vtable }, false); + defer a.deinit(); + WorkerSearch.init(a); + defer WorkerSearch.q.deinit(); + // The session's own `/`: its parse asked of the worker, its answer an + // input (the rows grouped once it lands). + _ = try a.local(.{ .command = "Search /pub" }); + try WorkerSearch.land(a, null); + // A client joined now, and a key typed into the query: both its rows. + const r = try joinedNow(a, 1, 0); + defer r.deinit(); + try std.testing.expectEqual(digestOf(a), r.digest()); + try std.testing.expectEqual(a.core.panes[0].?.file.?.context.declarations.len, r.core().panes[0].?.file.?.context.declarations.len); + const u = try gpa.dupe(u8, try a.local(.{ .key = .{ .cp = 'l', .text = "l" } })); + defer gpa.free(u); + r.confirm(u); + r.settle(); + try agrees(a, r); +} + +// A text's places show as ticks on its rail (Places.addTicks), made again +// only when `gen` moves (buildTicks), by a frame or by a step's rail click +// (tickHit), whichever asks first; a dump keeps neither. A frame's ticks +// that a click read after the places changed under them would land it +// elsewhere on a core that drew than on one that did not. +test "a client joined from a dump lands a click on a rail's tick as the session does, the session's ticks drawn before the places changed" { + const gpa = std.testing.allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, TestHost.host(), false); + defer a.deinit(); + _ = try a.local(.{ .command = "Search /pub" }); + const owner: usize = 0; + try std.testing.expect(a.core.panes[owner].?.places.hits.items.len > 1); + // The session draws: its ticks, of `/pub`'s places. + { + const was = Stepping.begin(&a.t, null); + defer was.end(); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + _ = try a.core.render(arena.allocator()); + } + const ticks_gen = a.core.panes[owner].?.places.ticks_gen; + try std.testing.expectEqual(a.core.panes[owner].?.places.gen, ticks_gen); + const r = try joinedFresh(a); + defer r.deinit(); + // Its places changed (`/publ`), then a click on each row of the rail. + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + try sent.append(gpa, try gpa.dupe(u8, try r.input(.{ .key = .{ .cp = 'l', .text = "l" } }))); + const rect = r.core().view().rects[owner]; + const pane = r.core().panes[owner].?; + for (rect.y + pane.tag_rows..rect.y + rect.h) |row| for ([_]pardes.Mouse.Kind{ .press, .release }) |kind| { + try sent.append(gpa, try gpa.dupe(u8, try r.input(.{ .mouse = .{ .button = .left, .kind = kind, .col = rect.x, .row = @intCast(row) } }))); + }; + try std.testing.expect(a.core.panes[owner].?.places.ticks_gen == ticks_gen); + for (sent.items) |m| { + r.confirm(try a.take(m)); + r.settle(); + } + // (The key may be mispredicted: a step's places are a frame's time's + // worth, `tape.over`, the session's verdict, and its drawn caches let + // it find more in that time than the client's speculation did.) + try agrees(a, r); + try std.testing.expectEqual(a.core.panes[owner].?.places.current, r.core().panes[owner].?.places.current); +} diff --git a/core/root.zig b/core/root.zig index ef3d1614..02deeaa6 100644 --- a/core/root.zig +++ b/core/root.zig @@ -745,7 +745,8 @@ const kept_out = [_]struct { []const u8, []const u8, []const u8 }{ .{ "panes.zig views.zig", "pan", "written by steps only (sync, a view unparked) from the pane and its terminal" }, .{ "File.zig", "cap line_starts line_cap", "the text's line index, of its bytes" }, .{ "File.zig", "highlights highlight_start syntax_dirty", "how a text looks, painted by frames: read by frames, and by a query's rows for colours only (out of their digest)" }, - .{ "File.zig", "context", "the declarations of the text at a revision: found in sync (refreshContext), by a parse's answer (a step) or a dump's load" }, + .{ "File.zig", "allocator", "ContextCache's: what its declarations were made with, the parse's or a load's (a machine's handle, no state)" }, + .{ "File.zig", "declarations", "ContextCache's: the text's at its revision (dumped, digested), parsed again from the text at a load" }, .{ "File.zig", "node_tree node_tree_revision node_tree_edits node_history", "the parse of the text at its revision, made by the first key that walks it" }, .{ "File.zig", "tables", "the whole text's, read at once by whoever asks first (Tables.ensure): no reading under way that a frame could move" }, .{ "Text.zig", "room", "a buffer's capacity" }, |
