diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-11 23:46:02 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-12 01:40:04 -0300 |
| commit | 60e07feef401d9044db3a1c925cb9d1f6d20dac9 (patch) | |
| tree | 98957ba8425193f686c8e8692cd10a10f27dbf4b /anim/tools/bench.zig | |
| parent | 9c0696447dbc5cf37e56aee562c7ebba4f4d4e6b (diff) | |
| download | computeranim-60e07feef401d9044db3a1c925cb9d1f6d20dac9.tar.gz computeranim-60e07feef401d9044db3a1c925cb9d1f6d20dac9.zip | |
canim: relations as one data-oriented subsystem; aim; the ASan pair; the introduction's chapters
From the user. Relations between panes are one subsystem: per-frame
tables of places and values in struct-of-arrays, owned by the frame;
the kinds one table of data; a resolver of pure passes (join, filter,
priority, cut) unit-tested on hand-written tables. Ported kinds draw
byte-identical frames; a synthetic map of 4000 places and 6000 values
resolves in 0.56 ms against the old table's 5.7. aim, a word holding a
code address tied to its row, is the first new kind, off unless a scene
turns it on.
The ASan pair: one overflow built plain and with ASan, recorded by
instruction; the frame as bytes eight a row; the plain run goes on alone
in the column, its return address aimed at main's row until the letters
reach it; the fault at ret. The introduction's three chapters are built
and tested, not yet exported. Annotations, rows and scrolls that changed
in one frame now fade; captions stand twelve seconds at most.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNCKgYCGiANnEwcEQxa
Diffstat (limited to 'anim/tools/bench.zig')
| -rw-r--r-- | anim/tools/bench.zig | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/anim/tools/bench.zig b/anim/tools/bench.zig index ad6fe4b..020a259 100644 --- a/anim/tools/bench.zig +++ b/anim/tools/bench.zig @@ -32,9 +32,75 @@ fn steps(rec: *canim.Recording, by: canim.Recording.Stop, function: ?[]const u8) } } +/// `bench relations`: the relations' passes on tables of a working frame's +/// size and of a map's (design/README.md, "4. Ties", "Cost": some 4,000 +/// places and 6,000 values), against the table they replace (a list +/// searched from the start for each lookup, so a join is places times values). +fn relationsCost(gpa: std.mem.Allocator) !void { + const rel = canim.relations; + std.debug.print("{s:<8} {s:>6} {s:>6} {s:>6} | {s:>10} {s:>10} {s:>10} | {s:>12}\n", .{ "tables", "places", "values", "pairs", "sort ns", "join ns", "passes ns", "old scan ns" }); + for ([_][2]usize{ .{ 300, 120 }, .{ 4000, 6000 } }, [_][]const u8{ "working", "map" }) |size, label| { + var rng: std.Random.DefaultPrng = .init(7); + const r = rng.random(); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const rounds = 200; + var took: [4]u64 = @splat(0); + var pairs_n: usize = 0; + for (0..rounds) |_| { + _ = arena.reset(.retain_capacity); + const a = arena.allocator(); + var t: rel.Tables = .{}; + // Places: rows of code (kind 14) and words of the stack (kind 3), and some of every other kind. + for (0..size[0]) |i| { + const kind: u8 = if (i % 3 == 0) 14 else if (i % 3 == 1) 3 else @intCast(1 + r.uintLessThan(u8, 16)); + const addr: u64 = if (kind == 14) 0x401000 + 4 * @as(u64, i) else 0x7fffffffe000 + 8 * @as(u64, i); + t.add(a, .{ .key = rel.keyOf(kind, addr), .box = .{ 0, @floatFromInt(i), 880, @floatFromInt(i + 1) }, .pane = .disasm }); + } + // Values: a third of them code addresses on screen, the rest anything. + for (0..size[1]) |i| { + const v: u64 = if (i % 3 == 0) 0x401000 + 4 * r.uintLessThan(u64, size[0]) else r.int(u64); + t.show(a, .{ .value = v, .box = .{ 0, 0, 0, 0 }, .pane = .stack, .at = 0x7fffffffe000 + 8 * @as(u64, i) }); + } + var clock = rel.clock(); + const by_key = t.byKey(a); + const by_value = t.byValue(a); + took[0] += rel.clock() - clock; + clock = rel.clock(); + var pairs: std.ArrayList(rel.Pair) = .empty; + rel.join(t.values.items(.value), by_value, t.places.items(.key), by_key, 14, ~@as(u64, 0), &pairs, a); + took[1] += rel.clock() - clock; + pairs_n = pairs.items.len; + // The passes over a candidate a pair (the knob on), then cut to the budget. + clock = rel.clock(); + const cands = try a.alloc(rel.Candidate, pairs.items.len); + for (cands, pairs.items) |*c, p| c.* = .{ .kind = .code, .rank = p.value }; + var kept = rel.filter(cands, rel.Knobs.initMany(&.{ .code, .always })); + rel.byPriority(kept); + kept = rel.cut(kept); + took[2] += rel.clock() - clock; + std.mem.doNotOptimizeAway(kept.len); + // The old way: each value's lookup a scan of the places from the start. + clock = rel.clock(); + const keys = t.places.items(.key); + var found: usize = 0; + for (t.values.items(.value)) |v| { + for (keys) |key| if (key == rel.keyOf(14, v)) { + found += 1; + break; + }; + } + std.mem.doNotOptimizeAway(found); + took[3] += rel.clock() - clock; + } + std.debug.print("{s:<8} {d:>6} {d:>6} {d:>6} | {d:>10} {d:>10} {d:>10} | {d:>12}\n", .{ label, size[0], size[1], pairs_n, took[0] / rounds, took[1] / rounds, took[2] / rounds, took[3] / rounds }); + } +} + pub fn main(init: std.process.Init) !void { const gpa = init.gpa; const args = try init.minimal.args.toSlice(init.arena.allocator()); + if (args.len == 2 and std.mem.eql(u8, args[1], "relations")) return relationsCost(gpa); if (args.len >= 3) { inline for (@typeInfo(recordings).@"struct".decl_names) |name| { if (@TypeOf(@field(recordings, name)) == recordings.Program and std.mem.eql(u8, name, args[1])) |
