summaryrefslogtreecommitdiff
path: root/src/Registers.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-30 00:25:19 -0300
committerGabriel Schneider <[email protected]>2026-10-30 00:53:24 -0300
commitf264dbc4a646da695331fe7401fa013cbb720b6a (patch)
treeb8d960c5034d7ebfeb44fa25fa5697abbf386a9f /src/Registers.zig
parentbce88593014a4078dc9259855944f9ddbc19fe54 (diff)
downloadpardes-f264dbc4a646da695331fe7401fa013cbb720b6a.tar.gz
pardes-f264dbc4a646da695331fe7401fa013cbb720b6a.zip
the walker's digest: a blob's hash made once and folded in as a word (a 100 MB file was 5 ms a digest, two a unit, on the authority); blob fields annotated `.blob`, which the rollback copy's pins read too
walk.zig: a field named in a type's `replica.blob` (a file's text, line index and location rows, an undo snapshot's text, a register's values and joined text, the rows an edit dropped, a query's saved rows) holds immutable blobs. A digest hashes one of 4 KiB or more once, keeps the word by its address and length, and folds the length and word in; `forget` drops the word, from memory.freeRaw and freeUnfilled, through which every such blob is freed raw. Only a Debug build (the tests, the harness, replay) hashes the bytes at every digest all the same and panics where the kept word is not their hash: a blob written in place, or freed past its word. A release build, ReleaseSafe the user's own as well as ReleaseFast, takes the word and never panics on a stale one: at worst a divergence, which a replica records and then stops at (nothing resets or rejoins it yet; the notes say so). File.zig: an edit's free of the old text went to `rawFree` itself and so kept its word; the next text of the same length at the same address folded in the old one (the ReleaseFast harness diverged in 162 of 180 replica runs, in `file.content`), and it goes through freeUnfilled now. Registers.zig: `put` makes a new values array rather than writing the old one. rollback.zig: the copy pins what `.blob` names (`pinned` and `pinnedDeep` gone). Measured, ReleaseFast on the perf cores: with a 100 MB file open (PARDES_DIGEST_BENCH=100, replica.zig's bench), a unit of a key into it went from p50 10.45 ms, p99 41.6 ms to p50 24 us, p99 6.1 ms (the first, which makes the word), a digest from 5.18 ms to 10 us. The harness (20 seeds x 120 inputs x 3 clients): rollback p99 9.5 -> 2.6 ms at 30 ms (1.5 and 1.4 ms at 80 and 207), copy p99 0.27 ms; none diverged, none unequal, no delta wrong. Tests: a blob hashed once, its kept word no longer its hash once written in place, its word gone when freed. docs/next-daemon.md: the follow-up. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src/Registers.zig')
-rw-r--r--src/Registers.zig38
1 files changed, 20 insertions, 18 deletions
diff --git a/src/Registers.zig b/src/Registers.zig
index 760aff9a..6df7401d 100644
--- a/src/Registers.zig
+++ b/src/Registers.zig
@@ -12,11 +12,12 @@ const Registers = @This();
list: std.ArrayList(Reg) = .empty,
last_search: u21 = '/',
-/// A value and the joined text are replaced whole, never written in place,
-/// so a replica's rollback copy pins them as it does a file's text
-/// (src/rollback.zig `pinned`), and they are freed raw: a safe build's
-/// free would write over one the copy still holds.
+/// The values (their array too) and the joined text are replaced whole,
+/// never written in place (`put` makes a new array), freed raw: blobs
+/// (src/walk.zig), which a digest hashes once and a replica's rollback copy
+/// pins as it does a file's text.
pub const Reg = struct {
+ pub const replica = .{ .blob = .{ "values", "joined" } };
name: u21,
/// gpa-owned, one per range, in document order
values: [][]u8,
@@ -36,7 +37,7 @@ pub fn deinit(r: *Registers, gpa: std.mem.Allocator) void {
fn freeReg(gpa: std.mem.Allocator, reg: *Reg) void {
for (reg.values) |v| freeRaw(gpa, v);
- gpa.free(reg.values);
+ freeRaw(gpa, reg.values);
if (reg.joined) |j| freeRaw(gpa, j);
}
@@ -65,19 +66,20 @@ pub fn put(r: *Registers, gpa: std.mem.Allocator, name: u21, bytes: []const u8,
};
break :blk &r.list.items[r.list.items.len - 1];
};
- if (fresh or reg.values.len != n) {
- const values = gpa.alloc([]u8, n) catch {
- gpa.free(copy);
- return;
- };
- for (values) |*v| v.* = &.{};
- freeReg(gpa, reg);
- reg.* = .{ .name = name, .values = values };
- }
- freeRaw(gpa, reg.values[i]);
- reg.values[i] = copy;
- if (reg.joined) |j| freeRaw(gpa, j);
- reg.joined = null;
+ // A new array, the old one's values moved into it but value `i`.
+ const values = gpa.alloc([]u8, n) catch {
+ gpa.free(copy);
+ return;
+ };
+ const keep = !fresh and reg.values.len == n;
+ for (values, 0..) |*v, j| v.* = if (keep and j != i) reg.values[j] else &.{};
+ values[i] = copy;
+ if (keep) {
+ freeRaw(gpa, reg.values[i]);
+ freeRaw(gpa, reg.values);
+ if (reg.joined) |j| freeRaw(gpa, j);
+ } else freeReg(gpa, reg);
+ reg.* = .{ .name = name, .values = values };
}
/// The whole register as one text: its values joined by newlines, no