summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 01:07:16 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commit62cce7f3d96aa054b2bc2055dce86dd8e4205256 (patch)
tree55a8e7d748d51809b9154d0d6d39ef6e6b662bb1 /src
parent70be2e0776d2e61a2d018b264ef5602e7e2c7f86 (diff)
downloadsketchbook-62cce7f3d96aa054b2bc2055dce86dd8e4205256.tar.gz
sketchbook-62cce7f3d96aa054b2bc2055dce86dd8e4205256.zip
Keyframes share pages: each writes only the pages of State that changed
A keyframe was a whole copy of State, so a 4 MiB State edited once a minute wrote 4 MiB a minute. Now a keyframe stores State as `pages`, an xitdb list of 4 KiB blobs (the last one shorter), with its `size`: it starts from the list of the keyframe before it (its slot written, and the cursor frozen, so a keyframe queued in the same commit is copied, not changed), and puts only the pages whose Wyhash differs from that keyframe's, which Sessions keeps in memory (`hashes`, one u64 a page). An all-zero page is an empty slot: no bytes. `written` records the bytes of pages a keyframe wrote, each page being written by exactly one keyframe, so the live size the exit compaction weighs is the batches plus every keyframe's `written`. A State unchanged since the last keyframe is still skipped (but before a reload), now by its page hashes. Keyframes from before pages keep their whole `state` bytes, and `load` reads both (`Loaded.paged` says which). Export and import shared pages once: `copy` remembers each blob's place in the source and the slot it was copied to, and writes that slot again for a page another keyframe already brought (xitdb takes slot writes). Measured (Debug, unit tests): a 4 MiB State, one u64 edited a minute, grows the database 144-153 KB a minute, 86 KB of it the minute's 3,600 ticks; a keyframe alone in a commit is 10,160 B with one changed page and 1,376 B with none (it was 4 MiB either way). A 1 MiB session of ten keyframes a byte apart exports to 1,133,486 B (it was over 10 MiB). The hash pass over State, best of five on a loaded machine: 2.2, 36 and 71 ms at 1, 16 and 32 MiB in Debug; 0.04, 1.3 and 2.9 ms in ReleaseFast. Not done (the spec's later fast paths, nothing needs them yet): restore copying pages without defaults and migration for an unchanged layout, the divergence check comparing pages first, formatPath skipping leaves. Tests: zig build test, 166/166 steps, 134/134 tests. New: a 4 MiB State edited each minute for ten minutes, under 230 KB a minute and every keyframe reading back with exactly the edits made before it; a lone keyframe under 12 KiB with one changed page and under 8 KiB with none; an exported ten-keyframe session under 1 MiB and an eighth, reading back. Changed: the compaction test's live size counts five 8-byte keyframes, not six (the first State is all zeros, an empty page). test-kill, test-past, test-import, test-same and test-paths unchanged. micscope 67/67 steps; spheres 75/75, 3/3 tests; noc e2e 292/292; the drat-meout copy 87/87 steps, 30/30 tests. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'src')
-rw-r--r--src/host/Sessions.zig242
1 files changed, 215 insertions, 27 deletions
diff --git a/src/host/Sessions.zig b/src/host/Sessions.zig
index 751961d..c2af557 100644
--- a/src/host/Sessions.zig
+++ b/src/host/Sessions.zig
@@ -17,12 +17,19 @@
//! `layout` is the state file's header (memory.readLayout reads it)
//! batches list of bytes: a little-endian u64, the first frame's index, then
//! whole frames of raw `abi.Event`s, each frame ending with its tick
-//! keys list of { frame, batch, stretch, why, time, state, line }: the
-//! State before frame `frame` is fed, in stretch `stretch`'s layout;
-//! `batch` is where reading frames from it starts
+//! keys list of { frame, batch, stretch, why, time, size, pages, written, line }:
+//! the State before frame `frame` is fed, in stretch `stretch`'s
+//! layout; `batch` is where reading frames from it starts
//! frames frames committed
//! }
//!
+//! A keyframe's State is `size` bytes in `pages`, a list of `page`-byte
+//! blobs (the last one shorter), an all-zero page an empty slot. A keyframe
+//! starts from the list of the one before it and writes only the pages
+//! whose hash changed (`written` bytes), so every page another keyframe
+//! holds unchanged is shared, never written twice. (Keyframes from before
+//! pages hold their State whole, as `state` bytes; `load` reads both.)
+//!
//! One transaction (`commit`) a second of session time, and at once for a
//! reload, a mark, a clip's start or end, and exit: a crash loses less than
//! a second of play; fsync only at exit and for a mark. A file a crash or
@@ -53,14 +60,23 @@ pub const max_name = 32;
/// hours or more of play at 60 Hz. What it holds stays readable.
pub const memory_max: u64 = 256 << 20;
-pub const K = enum(u64) { version = 1, sessions, marks, clips, started, ended, from, stretches, batches, keys, frames, frame, build, source, layout, batch, stretch, why, time, state, line, name, session, key, start, end };
+pub const K = enum(u64) { version = 1, sessions, marks, clips, started, ended, from, stretches, batches, keys, frames, frame, build, source, layout, batch, stretch, why, time, state, line, name, session, key, start, end, pages, size, written };
fn key(k: K) u64 {
return @intFromEnum(k);
}
pub const Stretch = struct { frame: u64 = 0, build: u64, source: []const u8, layout: []const u8 };
-const Keyframe = struct { frame: u64, batch: u64, stretch: u64, why: Why, time: u64, state: []u8, line: []u8 };
+/// A keyframe's page: the OS page, the grain of sharing.
+pub const page = 4096;
+
+/// A keyframe: `session`'s keyframe `key`.
+const At = struct { session: u64, key: u64 };
+
+/// A keyframe queued: the pages of its State that differ from keyframe
+/// `from`'s (`indices`, ascending; their bytes one after the other in
+/// `pages`, `page` apart).
+const Keyframe = struct { frame: u64, batch: u64, stretch: u64, why: Why, time: u64, size: u64, from: ?At, indices: []u64, pages: []u8, line: []u8 };
gpa: std.mem.Allocator,
/// Files and the wall clock; a memory database runs on `std.Io.failing`.
@@ -89,10 +105,13 @@ batch_time: u64 = 0,
/// The first and latest event time logged: times never go back.
first_time: u64 = 0,
last_time: u64 = 0,
-/// The last keyframe's time, stretch and bytes' hash.
+/// The last keyframe's time and stretch.
key_time: u64 = 0,
key_stretch: u64 = 0,
-key_hash: u64 = 0,
+/// The keyframe the next one starts from (the last one, or the one a run
+/// started from), and the hashes of its pages.
+last: ?At = null,
+hashes: []u64 = &.{},
queued: std.ArrayList(Keyframe) = .empty,
/// The clip being recorded, an index into `clips`.
recording: ?u64 = null,
@@ -165,6 +184,7 @@ pub fn deinit(s: *Sessions) void {
s.dropQueued();
s.queued.deinit(s.gpa);
s.batch.deinit(s.gpa);
+ s.gpa.free(s.hashes);
if (s.file) |f| f.close(s.io);
if (s.buffer) |b| {
b.deinit();
@@ -211,7 +231,8 @@ fn compact(s: *Sessions) !void {
try cwd.rename(new, cwd, s.path, s.io);
}
-/// The bytes of every session's batches and keyframes: what compaction keeps.
+/// The bytes of every session's batches and keyframes' pages (each written
+/// once): what compaction keeps.
/// Reading them all is also what proves a mended file whole.
fn liveSize(db: *Disk) !u64 {
const R = Read(Disk);
@@ -226,7 +247,10 @@ fn liveSize(db: *Disk) !u64 {
}
if (try R.list(session, .keys)) |keys| {
var k = try keys.iterator();
- while (try k.next()) |key_cursor| live += try (try R.field(try R.Map.init(key_cursor), .state)).count();
+ while (try k.next()) |key_cursor| {
+ const e = try R.Map.init(key_cursor);
+ live += if (try e.getCursor(key(.written))) |w| try w.readUint() else try (try R.field(e, .state)).count();
+ }
}
}
return live;
@@ -320,19 +344,38 @@ pub fn frame(s: *Sessions, one: []abi.Event, state: []const u8, why: ?Why) !void
/// Queues a keyframe of `state` before the next frame, and returns its
/// index; an unchanged State is the last keyframe again (but for a
-/// `before_reload` one: the last word in the old layout).
+/// `before_reload` one: the last word in the old layout). Only the pages
+/// whose hash differs from the last keyframe's are copied, and written.
pub fn keyframe(s: *Sessions, why: Why, state: []const u8) !u64 {
if (s.db == .off) return error.NotRecording;
- const hash = std.hash.Wyhash.hash(0, state);
defer s.line_len = 0;
- if (s.keys > 0 and why != .before_reload and hash == s.key_hash and s.key_stretch == s.stretches - 1) return s.keys - 1;
- const state_copy = try s.gpa.dupe(u8, state);
- errdefer s.gpa.free(state_copy);
+ const hashes = try s.gpa.alloc(u64, std.math.divCeil(usize, state.len, page) catch unreachable);
+ errdefer s.gpa.free(hashes);
+ var changed: std.ArrayList(u64) = .empty;
+ defer changed.deinit(s.gpa);
+ for (hashes, 0..) |*h, i| {
+ h.* = std.hash.Wyhash.hash(0, state[i * page .. @min(state.len, (i + 1) * page)]);
+ if (i >= s.hashes.len or s.hashes[i] != h.*) try changed.append(s.gpa, i);
+ }
+ if (s.keys > 0 and why != .before_reload and changed.items.len == 0 and hashes.len == s.hashes.len and s.key_stretch == s.stretches - 1) {
+ s.gpa.free(hashes);
+ return s.keys - 1;
+ }
+ const pages = try s.gpa.alloc(u8, changed.items.len * page);
+ errdefer s.gpa.free(pages);
+ for (changed.items, 0..) |i, k| {
+ const bytes = state[i * page .. @min(state.len, (i + 1) * page)];
+ @memcpy(pages[k * page ..][0..bytes.len], bytes);
+ }
+ const indices = try changed.toOwnedSlice(s.gpa);
+ errdefer s.gpa.free(indices);
const line_copy = try s.gpa.dupe(u8, s.line[0..s.line_len]);
errdefer s.gpa.free(line_copy);
- try s.queued.append(s.gpa, .{ .frame = s.frames, .batch = s.batches, .stretch = s.stretches - 1, .why = why, .time = s.last_time, .state = state_copy, .line = line_copy });
+ try s.queued.append(s.gpa, .{ .frame = s.frames, .batch = s.batches, .stretch = s.stretches - 1, .why = why, .time = s.last_time, .size = state.len, .from = s.last, .indices = indices, .pages = pages, .line = line_copy });
+ s.gpa.free(s.hashes);
+ s.hashes = hashes;
+ s.last = .{ .session = s.session, .key = s.keys };
s.key_time = s.last_time;
- s.key_hash = hash;
s.key_stretch = s.stretches - 1;
s.keys += 1;
return s.keys - 1;
@@ -371,7 +414,8 @@ pub fn stopClip(s: *Sessions) !void {
fn dropQueued(s: *Sessions) void {
for (s.queued.items) |k| {
- s.gpa.free(k.state);
+ s.gpa.free(k.indices);
+ s.gpa.free(k.pages);
s.gpa.free(k.line);
}
s.queued.clearRetainingCapacity();
@@ -456,8 +500,28 @@ fn Commit(comptime DB: type) type {
try k.put(key(.stretch), .{ .uint = q.stretch });
try k.put(key(.why), .{ .uint = @intFromEnum(q.why) });
try k.put(key(.time), .{ .uint = q.time });
- try k.put(key(.state), .{ .bytes = q.state });
+ try k.put(key(.size), .{ .uint = q.size });
try k.put(key(.line), .{ .bytes = q.line });
+ // From the keyframe before's pages, copied as the
+ // transaction writes on (it may be in it too).
+ var from = try k.putCursor(key(.pages));
+ if (q.from) |at| if (try pagesSlot(root, at)) |slot| {
+ try from.write(.{ .slot = slot });
+ try from.freeze();
+ };
+ const pages = try List.init(from);
+ const had = try pages.count();
+ var written: u64 = 0;
+ for (q.indices, 0..) |i, j| {
+ const bytes = q.pages[j * page ..][0..@min(page, q.size - i * page)];
+ const zero = std.mem.allEqual(u8, bytes, 0);
+ if (!zero) written += bytes.len;
+ const data: DB.WriteableData = if (zero) .{ .slot = null } else .{ .bytes = bytes };
+ if (i < had) try pages.put(@intCast(i), data) else try pages.append(data);
+ }
+ const n = std.math.divCeil(u64, q.size, page) catch unreachable;
+ if (had > n) try pages.slice(n);
+ try k.put(key(.written), .{ .uint = written });
}
}
switch (ctx.extra) {
@@ -485,6 +549,13 @@ fn Commit(comptime DB: type) type {
}
}
+ /// The slot of keyframe `at`'s pages; null for one from before pages.
+ fn pagesSlot(root: Map, at: At) !?xitdb.Slot {
+ const R = Read(DB);
+ const ses = try R.at(try R.list(root.readOnly(), .sessions) orelse return null, at.session);
+ return (try R.at(try R.list(ses, .keys) orelse return null, at.key)).getSlot(key(.pages));
+ }
+
fn putStretch(list: List, st: Stretch) !void {
const item = try Map.init(try list.appendCursor());
try item.put(key(.frame), .{ .uint = st.frame });
@@ -513,6 +584,8 @@ pub const Entry = struct {
};
/// A keyframe, its bytes `gpa`'s (`free`).
pub const Loaded = struct {
+ /// Stored in pages (not whole, from before them).
+ paged: bool = true,
frame: u64,
batch: u64,
why: Why,
@@ -594,13 +667,15 @@ pub fn load(s: *Sessions, session: u64, k: u64) !Loaded {
const st = try R.uint(e, .stretch);
const stretch_entry = try R.at(try R.list(ses, .stretches) orelse return error.CorruptSession, st);
const why = std.enums.fromInt(Why, try R.uint(e, .why)) orelse return error.CorruptSession;
- const state = try R.bytes(e, .state, s.gpa);
+ const pages = try R.list(e, .pages);
+ const state = if (pages) |list| try R.pages(list, try R.uint(e, .size), s.gpa) else try R.bytes(e, .state, s.gpa);
errdefer s.gpa.free(state);
const line = try R.bytes(e, .line, s.gpa);
errdefer s.gpa.free(line);
const source = try R.bytes(stretch_entry, .source, s.gpa);
errdefer s.gpa.free(source);
return .{
+ .paged = pages != null,
.frame = try R.uint(e, .frame),
.batch = try R.uint(e, .batch),
.why = why,
@@ -775,6 +850,8 @@ fn Transfer(comptime From: type, comptime To: type) type {
const List = To.ArrayList(.read_write);
pub fn run(ctx: @This(), cursor: *To.Cursor(.read_write)) !void {
+ var copied: std.AutoHashMap(u64, xitdb.Slot) = .init(ctx.gpa);
+ defer copied.deinit();
const src = try R.root(ctx.from);
if (try R.uint(src, .version) != 1) return error.NotASessionsDatabase;
const all = try R.list(src, .sessions) orelse return error.NotASessionsDatabase;
@@ -786,7 +863,7 @@ fn Transfer(comptime From: type, comptime To: type) type {
for (0..try all.count()) |i| {
if (ctx.only) |o| if (o != i) continue;
var to = try sessions.appendCursor();
- try copy(From, To, ctx.gpa, try all.getCursor(@intCast(i)) orelse return error.CorruptSession, &to);
+ try copy(From, To, ctx.gpa, try all.getCursor(@intCast(i)) orelse return error.CorruptSession, &to, &copied);
if (ctx.label) |l| try (try Map.init(to)).put(key(.from), .{ .bytes = try std.fmt.bufPrint(&buf, "import {s}", .{l}) });
}
for ([_]K{ .marks, .clips }) |which| {
@@ -797,7 +874,7 @@ fn Transfer(comptime From: type, comptime To: type) type {
const session = try R.uint(try R.Map.init(e), .session);
if (ctx.only) |o| if (o != session) continue;
var to = try list.appendCursor();
- try copy(From, To, ctx.gpa, e, &to);
+ try copy(From, To, ctx.gpa, e, &to, &copied);
const item = try Map.init(to);
try item.put(key(.session), .{ .uint = base + if (ctx.only == null) session else 0 });
const l = ctx.label orelse continue;
@@ -811,21 +888,27 @@ fn Transfer(comptime From: type, comptime To: type) type {
};
}
-/// Copies the value at `from` (a uint, bytes, or a list or map of them) to `to`.
-fn copy(comptime From: type, comptime To: type, gpa: std.mem.Allocator, from: From.Cursor(.read_only), to: *To.Cursor(.read_write)) anyerror!void {
- switch ((try from.slot()).tag) {
+/// Copies the value at `from` (a uint, bytes, or a list or map of them) to
+/// `to`. Bytes `copied` already (a page keyframes share) are written once:
+/// it maps their place in `from` to their slot in `to`.
+fn copy(comptime From: type, comptime To: type, gpa: std.mem.Allocator, from: From.Cursor(.read_only), to: *To.Cursor(.read_write), copied: *std.AutoHashMap(u64, xitdb.Slot)) anyerror!void {
+ const slot = try from.slot();
+ switch (slot.tag) {
+ .none => try to.write(.{ .slot = null }),
.uint => try to.write(.{ .uint = try from.readUint() }),
.bytes, .short_bytes => {
+ if (slot.tag == .bytes) if (copied.get(slot.value)) |done| return to.write(.{ .slot = done });
const bytes = try from.readBytesAlloc(gpa, null);
defer gpa.free(bytes);
try to.write(.{ .bytes = bytes });
+ if (slot.tag == .bytes) try copied.put(slot.value, try to.slot());
},
.array_list => {
const list = try To.ArrayList(.read_write).init(to.*);
var it = try from.iterator();
while (try it.next()) |c| {
var next = try list.appendCursor();
- try copy(From, To, gpa, c, &next);
+ try copy(From, To, gpa, c, &next, copied);
}
},
.hash_map => {
@@ -834,7 +917,7 @@ fn copy(comptime From: type, comptime To: type, gpa: std.mem.Allocator, from: Fr
while (try it.next()) |c| {
const kv = try c.readKeyValuePair();
var next = try map.putCursor(kv.hash);
- try copy(From, To, gpa, kv.value_cursor, &next);
+ try copy(From, To, gpa, kv.value_cursor, &next, copied);
}
},
else => return error.CorruptSession,
@@ -861,6 +944,19 @@ fn Read(comptime DB: type) type {
fn bytes(map: Map, k: K, gpa: std.mem.Allocator) ![]const u8 {
return (try field(map, k)).readBytesAlloc(gpa, null);
}
+ /// `size` bytes from a keyframe's pages, `gpa`'s.
+ fn pages(l: List, size: u64, gpa: std.mem.Allocator) ![]u8 {
+ const out = try gpa.alloc(u8, size);
+ errdefer gpa.free(out);
+ @memset(out, 0);
+ for (0..try l.count()) |i| {
+ const c = try l.getCursor(@intCast(i)) orelse continue;
+ if ((try c.slot()).tag == .none) continue;
+ const into = out[@min(size, i * page)..@min(size, (i + 1) * page)];
+ if ((c.readBytes(into) catch return error.CorruptSession).len != into.len) return error.CorruptSession;
+ }
+ return out;
+ }
fn list(map: Map, k: K) !?List {
return try List.init(try map.getCursor(key(k)) orelse return null);
}
@@ -1025,7 +1121,8 @@ test "a compacted session database is smaller and holds every session and frame"
try s.commit(.none);
const size = try s.file.?.length(io);
const live_size = try liveSize(&s.db.file);
- try std.testing.expectEqual(@as(u64, 600 * @sizeOf(abi.Event) + 10 * 8 + 6 * 8), live_size);
+ // Six keyframes of 8 bytes, the first all zeros: an empty page.
+ try std.testing.expectEqual(@as(u64, 600 * @sizeOf(abi.Event) + 10 * 8 + 5 * 8), live_size);
try s.compact();
s.deinit();
const compacted = (try tmp.dir.statFile(io, "t.xitdb", .{})).size;
@@ -1067,3 +1164,94 @@ test "a memory session stops recording at its bound, and what it holds still rea
try s.events(s.session, 0, 0, n, &back);
try std.testing.expectEqualSlices(u8, std.mem.sliceAsBytes(live.items[0..n]), std.mem.sliceAsBytes(back.items));
}
+
+test "a session's keyframes write only the pages that changed: a State of megabytes edited each minute, and one never changed" {
+ const gpa = std.testing.allocator;
+ const io = std.testing.io;
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var buf: [std.fs.max_path_bytes]u8 = undefined;
+ const path = try std.fmt.bufPrint(&buf, ".zig-cache/tmp/{s}/t.xitdb", .{&tmp.sub_path});
+ // 4 MiB, every page holding something, one u64 edited a minute.
+ const state = try gpa.alloc(u8, 4 << 20);
+ defer gpa.free(state);
+ for (state, 0..) |*b, i| b.* = @truncate(i *% 7 + 1);
+ var s = try open(gpa, io, .file, path, .{ .build = 7, .source = "", .layout = "a layout" }, 1000);
+ defer s.deinit();
+ const minutes = 10;
+ var sizes: [minutes + 1]u64 = undefined;
+ for (0..minutes * 60 * 60) |f| {
+ const t = f * std.time.ns_per_s / 60;
+ var one = [_]abi.Event{abi.Event.tick(t, std.time.ns_per_s / 60)};
+ if (f % 3600 == 1800) std.mem.writeInt(u64, state[(f / 3600) * 300_000 ..][0..8], f, .little);
+ try s.frame(&one, state, if (f == 0) .start else null);
+ if (f % 3600 == 0) {
+ try s.commit(.none);
+ sizes[f / 3600] = try s.file.?.length(io);
+ }
+ }
+ try s.commit(.none);
+ sizes[minutes] = try s.file.?.length(io);
+ // One keyframe a minute: at 0 s, 60 s, ...
+ try std.testing.expectEqual(@as(u64, minutes), s.keys);
+ // A minute is 216,000 bytes of ticks and a keyframe of one page with
+ // its copied path: under 230 KB a minute, where a whole State a
+ // keyframe was 4 MiB.
+ for (1..minutes) |m| try std.testing.expect(sizes[m + 1] - sizes[m] < 230_000);
+ // Every keyframe reads back whole: the edits made before it, and none after.
+ const want = try gpa.alloc(u8, state.len);
+ defer gpa.free(want);
+ for (0..minutes) |k| {
+ for (want, 0..) |*b, i| b.* = @truncate(i *% 7 + 1);
+ for (0..k) |m| std.mem.writeInt(u64, want[m * 300_000 ..][0..8], m * 3600 + 1800, .little);
+ const loaded = try s.load(s.session, k);
+ defer loaded.free(gpa);
+ try std.testing.expectEqual(@as(u64, k * 3600), loaded.frame);
+ try std.testing.expectEqualSlices(u8, want, loaded.state);
+ }
+ // A keyframe alone, in a commit of its own: one changed page is that
+ // page, its copied path and the keyframe's fields; no changed page (one
+ // kept before a reload) is the fields alone.
+ for ([_]bool{ true, false }) |edit| {
+ if (edit) state[123] +%= 1;
+ const before = try s.file.?.length(io);
+ _ = try s.keyframe(if (edit) .jump else .before_reload, state);
+ try s.commit(.none);
+ try std.testing.expect(try s.file.?.length(io) - before < @as(u64, if (edit) 12 << 10 else 8 << 10));
+ }
+ const last = try s.load(s.session, s.keys - 1);
+ defer last.free(gpa);
+ try std.testing.expectEqualSlices(u8, state, last.state);
+}
+
+test "an exported session keeps its keyframes' shared pages shared" {
+ const gpa = std.testing.allocator;
+ const io = std.testing.io;
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var buf: [std.fs.max_path_bytes]u8 = undefined;
+ const path = try std.fmt.bufPrint(&buf, ".zig-cache/tmp/{s}/t.xitdb", .{&tmp.sub_path});
+ var out_buf: [std.fs.max_path_bytes]u8 = undefined;
+ const out = try std.fmt.bufPrint(&out_buf, ".zig-cache/tmp/{s}/one.xitdb", .{&tmp.sub_path});
+ const state = try gpa.alloc(u8, 1 << 20);
+ defer gpa.free(state);
+ for (state, 0..) |*b, i| b.* = @truncate(i *% 13 + 1);
+ var s = try open(gpa, io, .file, path, .{ .build = 7, .source = "", .layout = "a layout" }, 1000);
+ defer s.deinit();
+ // Ten keyframes a byte apart.
+ for (0..600) |f| {
+ const t = f * std.time.ns_per_s / 60;
+ var one = [_]abi.Event{abi.Event.tick(t, std.time.ns_per_s / 60)};
+ if (f % 60 == 0) state[f * 100] +%= 1;
+ try s.frame(&one, state, if (f == 0) .start else if (f % 60 == 0) .jump else null);
+ }
+ try s.exportSession(0, out);
+ const size = (try std.Io.Dir.cwd().statFile(io, out, .{})).size;
+ // One State and nine pages, not ten States.
+ try std.testing.expect(size < state.len + state.len / 8);
+ var again = try open(gpa, io, .file, out, .{ .build = 7, .source = "", .layout = "a layout" }, 2000);
+ defer again.deinit();
+ const loaded = try again.load(0, 9);
+ defer loaded.free(gpa);
+ try std.testing.expectEqualSlices(u8, state, loaded.state);
+}