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/Places.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/Places.zig')
| -rw-r--r-- | src/Places.zig | 96 |
1 files changed, 89 insertions, 7 deletions
diff --git a/src/Places.zig b/src/Places.zig index cff7c22e..a60a0d65 100644 --- a/src/Places.zig +++ b/src/Places.zig @@ -67,6 +67,10 @@ pub fn nowNs() u64 { return @as(u64, @intCast(ts.sec)) *| std.time.ns_per_s +| @as(u64, @intCast(ts.nsec)); } +/// What the replay digest leaves out (src/walk.zig): the rail's ticks, +/// made again from the places when they are drawn. +pub const replica = .{ .derived = .{ "ticks", "ticks_gen", "re" } }; + started: bool = false, provenance: Provenance = .search, /// The query pane that fed them, by serial; 0 for `*` and the register. @@ -178,6 +182,7 @@ pub fn firstFrom(hits: []const Hit, at: usize) usize { pub fn search(set: *Places, p: *Pardes, pane: *Pane, provenance: Provenance, from: u32, pattern: []const u8, literal: bool, seek: ?usize, moves: bool) void { const f = if (pane.file) |*file| file else return; const gpa = p.gpa; + set.derive(gpa); // The same question again (a key that left the pattern as it was): // the places stand, the current one too. if (set.started and set.re != null and set.literal == literal and std.mem.eql(u8, set.pattern, pattern)) { @@ -194,14 +199,12 @@ pub fn search(set: *Places, p: *Pardes, pane: *Pane, provenance: Provenance, fro if (std.ascii.isUpper(c)) break false; } else true; set.literal = literal; - const arena = p.scratch.allocator(); - const folded = if (set.fold) std.ascii.allocLowerString(arena, pattern) catch return else pattern; - set.re = if (literal) null else normal.SearchRegex.compile(gpa, folded) catch null; + set.derive(gpa); if (set.re == null) { + // It does not compile: the characters it is. set.literal = true; - var escaped: std.ArrayList(u8) = .empty; - normal.appendSearchLiteral(&escaped, arena, folded) catch return; - set.re = normal.SearchRegex.compile(gpa, escaped.items) catch return; + set.derive(gpa); + if (set.re == null) return; } set.started = true; set.revision = f.revision +% 1; // `fill` starts it over for the text as it is @@ -211,6 +214,25 @@ pub fn search(set: *Places, p: *Pardes, pane: *Pane, provenance: Provenance, fro set.fill(p, pane, 0, frame_steps); } +/// `re` made from the pattern: derived (Places.replica), so neither a dump +/// nor a rollback copy holds it, and a set loaded with a pattern and none +/// was a list's to `fill` (a client that joined found no places an edit +/// made, 2026-10-30). A list's places have no pattern. +fn derive(set: *Places, gpa: std.mem.Allocator) void { + if (set.re != null or set.pattern.len == 0) return; + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + const folded = if (set.fold) std.ascii.allocLowerString(a, set.pattern) catch return else set.pattern; + if (!set.literal) { + set.re = normal.SearchRegex.compile(gpa, folded) catch null; + return; + } + var escaped: std.ArrayList(u8) = .empty; + normal.appendSearchLiteral(&escaped, a, folded) catch return; + set.re = normal.SearchRegex.compile(gpa, escaped.items) catch null; +} + /// A list's places in this text, as it gives them (sorted here), in place /// of whatever fed them before: a language server's references, say. /// `current`: the one at or after this offset is current. @@ -260,6 +282,14 @@ fn freshHay(set: *Places, gpa: std.mem.Allocator, f: *File.State) bool { if (set.hay.len > 0) gpa.rawFree(set.hay, .of(u8), @returnAddress()); set.hay = &.{}; set.hay = memory.unfilled(gpa, f.content.len) catch return false; + // What is not lowered yet is in the digest and a dump as well: zeros, + // the same in every process, not whatever its heap held (a replica's + // diverged). A pass over the text, as the edit that made it was. + // ponytail: 0.4 ms for 10 MB, 10 ms for 100 MB in one frame (ReleaseFast, + // page faults and all; lowering it is four times that, over frames). A + // digest and a dump of the lowered pieces alone if a huge text's first + // search ever shows. + @memset(set.hay, 0); set.low.resize(gpa, (f.content.len + chunk_bytes - 1) / chunk_bytes, false) catch return false; set.low.unsetAll(); set.hay_revision = f.revision; @@ -385,6 +415,7 @@ fn restart(set: *Places, p: *Pardes, f: *File.State) void { pub fn fill(set: *Places, p: *Pardes, pane: *Pane, bytes: usize, steps: u64) void { const started_ns = nowNs(); if (!set.started) return; + set.derive(p.gpa); const f = if (pane.file) |*file| file else return set.clear(p.gpa); const gpa = p.gpa; if (f.revision != set.revision or f.content.len != set.len) { @@ -477,7 +508,9 @@ pub fn fill(set: *Places, p: *Pardes, pane: *Pane, bytes: usize, steps: u64) voi if (short) break; // A frame's time spent (a whole search's for n and N, whose steps // are mvzr's whole budget). - if (started_ns != 0 and bytes != std.math.maxInt(usize) and nowNs() - started_ns > frame_ns) break; + // The clock's verdict is the session's (src/tape.zig): a replica + // finds as many places a frame as this did. + if (started_ns != 0 and bytes != std.math.maxInt(usize) and pardes.tape.over(started_ns, frame_ns)) break; } if (set.hits.items.len != found_before) { set.gen +%= 1; @@ -874,3 +907,52 @@ test "zz stress analysis cost" { std.testing.allocator.free(a.colors); std.debug.print("decls {d} us ({d}), analysis with colours {d} us\n", .{ (t1 - t0) / 1000, d.len, (t2 - t1) / 1000 }); } + +test "a lowered copy's pieces not searched yet are the same in every process: a digest and a dump take them" { + // Two sessions searching one text, each heap handing out other bytes in + // what is not written yet (rollback.Epoch.fill): their places agree. + const rollback = @import("rollback.zig"); + var text: std.ArrayList(u8) = .empty; + defer text.deinit(std.testing.allocator); + for (0..2000) |i| try text.print(std.testing.allocator, "line {d} of a text in four pieces\n", .{i}); + var digests: [2]u64 = undefined; + for (&digests, [_]u8{ 0x11, 0x5a }) |*d, b| { + var e: rollback.Epoch = .{ .child = std.testing.allocator }; + defer e.deinit(); + rollback.Epoch.fill = b; + defer rollback.Epoch.fill = null; + const p = try Pardes.init(e.allocator(), .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + const pane = try p.setTestFile(text.items); + // The view's piece found now, the rest a frame at a time after. + pane.places.search(p, pane, .typed, 0, "piece", false, null, false); + try std.testing.expect(!pane.places.complete() and pane.places.fold); + d.* = pardes.walk.digest(Places, &pane.places); + } + try std.testing.expectEqual(digests[0], digests[1]); +} + +test "a session's places loaded from its dump find what an edit makes as the session's do" { + // A client's core is the session's dump; `re` is derived, in no dump: + // made again from the pattern, else the loaded set took itself for a + // list's and found nothing an edit made (a soak's rejoin loop). + const gpa = std.testing.allocator; + const a = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer a.deinit(); + const pane = try a.setTestFile("alpha beta\ngamma alpha\n"); + typeKeys(a, "e*"); + try std.testing.expectEqual(@as(usize, 2), pane.places.hits.items.len); + const bytes = try pardes.dump.dumpBytes(a); + defer gpa.free(bytes); + const b = try pardes.dump.load(gpa, a.opts, bytes, null); + defer b.deinit(); + try std.testing.expectEqual(pardes.walk.digest(Pardes, a), pardes.walk.digest(Pardes, b)); + for ([_]*Pardes{ a, b }) |p| { + const pn = p.panes[p.active].?; + const new = try gpa.dupe(u8, "alpha beta\ngamma alpha alpha\n"); + File.setContentSpan(p, &pn.file.?, new, null); + pn.places.fill(p, pn, 0, frame_steps); + try std.testing.expectEqual(@as(usize, 3), pn.places.hits.items.len); + } + try std.testing.expectEqual(pardes.walk.digest(Pardes, a), pardes.walk.digest(Pardes, b)); +} |
