summaryrefslogtreecommitdiff
path: root/tools/bench.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-10 11:27:39 -0300
committerGabriel Schneider <[email protected]>2026-11-12 13:22:02 -0300
commit71e534631bf68f33f802d64794340870374b9f69 (patch)
tree2a45229ce238a4e2ce355683f0e75a7725d4851a /tools/bench.zig
parent00605f7a109845a99f16621b267305a8b995dddc (diff)
downloadsketchbook-71e534631bf68f33f802d64794340870374b9f69.tar.gz
sketchbook-71e534631bf68f33f802d64794340870374b9f69.zip
sketchbook: anim, manim-style scenes as a module (from the game's vecanim), with objects a scene draws itself
The player takes its canvas from the room it has and fits the scene's frame into it, plays through and quits when exported, and draws vector objects in runs around custom ones. The renderers' resvg comparison and the timings come along as `zig build diff` and `zig build bench`; the comparison's reference is padded to whole blocks rather than squeezed, which had put Typst text over the limit. Co-Authored-By: Claude Fable 5.2 <[email protected]>
Diffstat (limited to 'tools/bench.zig')
-rw-r--r--tools/bench.zig96
1 files changed, 96 insertions, 0 deletions
diff --git a/tools/bench.zig b/tools/bench.zig
new file mode 100644
index 0000000..faa769e
--- /dev/null
+++ b/tools/bench.zig
@@ -0,0 +1,96 @@
+//! How long a frame of vector graphics takes, per stage, in release mode:
+//! parsing the SVG, turning it into polygons and coverage (CPU backend), or
+//! into a GPU pass description (GPU backend; the GPU's own time is not
+//! measured here).
+const std = @import("std");
+const vector = @import("vector");
+const scene = @import("scene").scene;
+const typst = @import("typst");
+
+const width = 1280;
+const height = 720;
+const runs = 300;
+/// Drawings up to this many shapes count as simple: they must take < 1ms.
+const simple_shapes = 30;
+
+/// Stand-in for `sketch.Host`: the renderers only need images (and to free
+/// the GPU backend's paint buffer, which this bench never makes).
+const Host = struct {
+ pub fn newImage(_: Host, _: u32, _: u32) @import("abi").Image {
+ return @fromBackingInt(@intCast(1));
+ }
+ pub fn freeImage(_: Host, _: @import("abi").Image) void {}
+ pub fn freeBuffer(_: Host, _: @import("abi").Buffer) void {}
+};
+
+pub fn main(init: std.process.Init) !void {
+ const gpa = std.heap.smp_allocator;
+ const io = init.io;
+ var failed = false;
+ std.debug.print("{s:<24} {s:>7} {s:>10} {s:>10} {s:>10} {s:>10}\n", .{ "object", "shapes", "parse", "cpu draw", "gpu record", "cpu total" });
+ // Every Typst object of the scene being played (a live scene adds them
+ // all whatever the time).
+ var objects_arena: std.heap.ArenaAllocator = .init(gpa);
+ defer objects_arena.deinit();
+ const shot = try scene.shot(objects_arena.allocator(), .{ .t = 0 });
+ for (shot.objects, 0..) |object, index| if (object.source == .typst) {
+ const svg = typst.find(object.source.typst).?;
+ var arena: std.heap.ArenaAllocator = .init(gpa);
+ defer arena.deinit();
+ var cpu: vector.Renderer(.cpu) = try .init(gpa, Host{}, width, height);
+ defer cpu.deinit(Host{});
+ var gpu: vector.Renderer(.gpu) = try .init(gpa, Host{}, width, height);
+ defer gpu.deinit(Host{});
+ const Frame = struct { scratch: std.mem.Allocator };
+
+ var parse_ns: [runs]u64 = undefined;
+ var cpu_ns: [runs]u64 = undefined;
+ var gpu_ns: [runs]u64 = undefined;
+ var shapes: usize = 0;
+ for (0..runs) |i| {
+ _ = arena.reset(.retain_capacity);
+ const t0 = now(io);
+ const d = try vector.svg.parse(arena.allocator(), svg);
+ const t1 = now(io);
+ // Fit to the canvas, as large as it goes: the worst case.
+ const m = vector.fit(d, .{ .w = width, .h = height });
+ cpu.begin(Frame{ .scratch = arena.allocator() }, .{ 0, 0, 0, 1 });
+ try cpu.draw(d, m, 1);
+ const t2 = now(io);
+ gpu.begin(Frame{ .scratch = arena.allocator() }, .{ 0, 0, 0, 1 });
+ try gpu.draw(d, m, 1);
+ std.mem.doNotOptimizeAway(gpu.target.pass());
+ const t3 = now(io);
+ parse_ns[i] = t1 - t0;
+ cpu_ns[i] = t2 - t1;
+ gpu_ns[i] = t3 - t2;
+ shapes = d.shapes.len;
+ }
+ const p = median(&parse_ns);
+ const c = median(&cpu_ns);
+ const g = median(&gpu_ns);
+ std.debug.print("{d:>2} {s:<21} {d:>7} {d:>8.3}ms {d:>8.3}ms {d:>8.3}ms {d:>8.3}ms\n", .{ index, label(object.source.typst), shapes, ms(p), ms(c), ms(g), ms(p + c) });
+ if (shapes <= simple_shapes and (p + c > std.time.ns_per_ms or p + g > std.time.ns_per_ms)) failed = true;
+ };
+ std.debug.print("\nup to {d} shapes must parse and draw in under 1ms at {d}x{d}, filling the canvas.\n", .{ simple_shapes, width, height });
+ if (failed) return error.TooSlow;
+}
+
+/// The start of a Typst source's first line.
+fn label(source: []const u8) []const u8 {
+ const line = source[0 .. std.mem.indexOfScalar(u8, source, '\n') orelse source.len];
+ return line[0..@min(line.len, 21)];
+}
+
+fn now(io: std.Io) u64 {
+ return @intCast(std.Io.Timestamp.now(io, .awake).nanoseconds);
+}
+
+fn median(samples: []u64) u64 {
+ std.mem.sort(u64, samples, {}, std.sort.asc(u64));
+ return samples[samples.len / 2];
+}
+
+fn ms(ns: u64) f64 {
+ return @as(f64, @floatFromInt(ns)) / std.time.ns_per_ms;
+}