diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 01:10:28 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | 9d10fc880bf4e62f8421d351ce15bddeec075322 (patch) | |
| tree | c452f42bb3c6fa94033eff9fad9851106f2a024c /tools/bench.zig | |
| parent | d302e4887c4c3fa534fec73a2141d3dc78436ccd (diff) | |
| download | sketchbook-9d10fc880bf4e62f8421d351ce15bddeec075322.tar.gz sketchbook-9d10fc880bf4e62f8421d351ce15bddeec075322.zip | |
examples: manual, Figure 2-1 of the Intel SDM comes alive, as a talk
examples/manual.zig is a live scene with pauses, played by the stock
player: page 31 of the SDM's Vol. 2 (Typst's import of the PDF, every
glyph a shape) fades in on paper beside a title; the camera closes on
Figure 2-1, the instruction format, while the rest of the page greys
and its footnotes go; each field (Prefixes, Opcode, ModR/M, SIB,
Displacement, Immediate) is tinted and glows in turn; 83 C0 01, add
eax, 1, is written below; each byte flies into its field as the fields
it has none for fade; C0 morphs down the arrow into the ModR/M byte's
boxes as 11000000, which splits into 11, 000 and 000 over Mod, Reg and
R/M, each with what it says (direct, /0 ADD, EAX); and the camera goes
back to the page, filled in. Twelve steps, 18 s played straight.
The labels, the ModR/M boxes, the figure and its footnotes are parts of
the page, by rectangles in the page's units read off pdftotext -bbox
(points to units: (pt - 306) * 4/3, (pt - 396) * 4/3) and checked with
the guides; each tint is the s.rect of its label's rectangle. The bits
are parts of a Typst object, cut by character (0.6 em of DejaVu Sans
Mono at 14 pt).
The PDF is never committed. build.zig's -Dmanual names it, by default
~/01-resources/intel-devref-ii.pdf; when the file is not there the
scene is one line saying how to build with it. zig build manual plays
it in a window. test/manual.script, run by zig build test (and
test-manual) only when the file is there, steps through the talk with
pixel checks (the paper, a tint where it should be and not where it
should not, a step back) and exports it twice at 10 fps, the same
frames both times.
zig build bench also times the manual's page (its one #image source,
when the build has the PDF), with a budget of its own for a drawing of
over 1000 shapes: parse under 25 ms, CPU draw under 10, GPU record under
4, at 1280x720. Measured: 6.4, 1.9 and 0.77 ms on a quiet run; 14.8, 3.5
and 1.4 at load average 34; once 28.6 ms to parse, at that load, which
failed it (the simple objects' 1 ms is as sensitive: 0.87 ms then). The
manual's simple objects are not benched: a two-glyph 83 fitted to the
whole canvas takes 1.1-1.2 ms on the CPU.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'tools/bench.zig')
| -rw-r--r-- | tools/bench.zig | 101 |
1 files changed, 62 insertions, 39 deletions
diff --git a/tools/bench.zig b/tools/bench.zig index faa769e..5412866 100644 --- a/tools/bench.zig +++ b/tools/bench.zig @@ -1,17 +1,29 @@ //! 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). +//! measured here). The objects: the compiled test scene's, and the manual +//! example's page of the Intel SDM (when the build has the PDF), thousands +//! of shapes, with a budget of its own. const std = @import("std"); const vector = @import("vector"); const scene = @import("scene").scene; const typst = @import("typst"); +const manual = @import("manual").scene; +const manual_typst = @import("manual_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; +/// Drawings of more count as a page (Figure 2-1's page of the SDM is 1954 +/// shapes; measured on a loaded 16-core machine: parse 6-12 ms, CPU draw +/// 2-5 ms, GPU record 0.85-1.7 ms). Parsing is once per object, drawing +/// every frame. +const page_shapes = 1000; +const page_parse_ms = 25; +const page_cpu_ms = 10; +const page_gpu_ms = 4; /// Stand-in for `sketch.Host`: the renderers only need images (and to free /// the GPU backend's paint buffer, which this bench never makes). @@ -34,48 +46,59 @@ pub fn main(init: std.process.Init) !void { 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; + if (try bench(gpa, io, index, object.source.typst, typst.find(object.source.typst).?)) failed = true; + }; + // The manual's page of the SDM, when the build has the PDF. + for (manual.typst_sources, 0..) |source, index| if (std.mem.startsWith(u8, source, "#image(")) { + if (try bench(gpa, io, index, source, manual_typst.find(source).?)) 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 }); + 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 }); + std.debug.print("a page (over {d}) parse in under {d}ms, draw on the CPU in {d}ms and record for the GPU in {d}ms.\n", .{ page_shapes, page_parse_ms, page_cpu_ms, page_gpu_ms }); if (failed) return error.TooSlow; } +/// Times one SVG, prints its line, and says whether it is over budget. +fn bench(gpa: std.mem.Allocator, io: std.Io, index: usize, source: []const u8, svg: []const u8) !bool { + 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(source), shapes, ms(p), ms(c), ms(g), ms(p + c) }); + if (shapes <= simple_shapes) return p + c > std.time.ns_per_ms or p + g > std.time.ns_per_ms; + if (shapes > page_shapes) return p > page_parse_ms * std.time.ns_per_ms or c > page_cpu_ms * std.time.ns_per_ms or g > page_gpu_ms * std.time.ns_per_ms; + return false; +} + /// 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]; |
