summaryrefslogtreecommitdiff
path: root/test/pdf_scroll_bench.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-06 06:24:42 -0300
committerGabriel Schneider <[email protected]>2026-11-06 11:19:44 -0300
commit5a9ad72e45bcac6a0e96af70664b1685e4f10595 (patch)
treeea8e0b4fe58debe0f78fde16638f6142f8c671c6 /test/pdf_scroll_bench.zig
parentc1917b949b9580de8bf0977874a210f0266812da (diff)
downloadpardes-5a9ad72e45bcac6a0e96af70664b1685e4f10595.tar.gz
pardes-5a9ad72e45bcac6a0e96af70664b1685e4f10595.zip
kinds #15, the PDF benches: pdf_scroll_bench and pdf_pointer_bench drive the addon's pane through the core (jobs inline, or BENCH_WORKERS=1 on threads; the pointer bench counts the rows its hover uploads), pdf_bench and pdf_sections_bench, which timed the core's PDF internals, go
Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'test/pdf_scroll_bench.zig')
-rw-r--r--test/pdf_scroll_bench.zig735
1 files changed, 194 insertions, 541 deletions
diff --git a/test/pdf_scroll_bench.zig b/test/pdf_scroll_bench.zig
index 837e3acb..93067d03 100644
--- a/test/pdf_scroll_bench.zig
+++ b/test/pdf_scroll_bench.zig
@@ -1,118 +1,58 @@
-//! Scoreboard for FAST scrolling of the continuous PDF strip.
+//! Scoreboard for FAST scrolling of a PDF's continuous strip: a pane of the
+//! pdf addon (addons/pdf/pdf.c), driven through the core as a shell drives
+//! it.
//!
//! zig build pdf-scroll-bench -Doptimize=ReleaseFast the numbers
//! zig build pdf-scroll-bench -Doptimize=ReleaseFast -- --json
//! zig build pdf-scroll-bench -Doptimize=ReleaseFast -- --warmup 1 --reps 15
//! zig build pdf-scroll-bench -Doptimize=ReleaseFast -- --path other.pdf
//! zig build pdf-scroll-bench -Doptimize=ReleaseFast -- --cell 16x32 a 2x display
-//! zig build pdf-scroll-bench -Doptimize=Debug -- --reps 1 the diagnosis
-//! zig build pdf-scroll-bench -Dtracy=~/05-genizah/tracy the zones
//!
-//! Every optimize mode runs. A profile or a crash wants safety checks, line
-//! numbers and un-inlined frames; only the SCOREBOARD wants ReleaseFast, so the
-//! mode is stamped in both output formats and shouted about on stderr rather
-//! than being refused — a benchmark you cannot run under a debugger is a
-//! benchmark whose regressions you cannot explain.
+//! A shell pump drains every input the OS queued since the last present and
+//! then renders once, so a trackpad fling is a BATCH of wheel notches in one
+//! frame. Every timed region is that: `ticks_per_frame` wheel presses
+//! through `Pardes.update`, one `render()` into a reset arena, and the
+//! effect drain, the page jobs the addon asked for run there and then (a
+//! shell's workers run them beside the frame; here they are in it, as the
+//! core's own PDF code rendered on the loop before the addon). Rows are
+//! per-FRAME latency.
//!
-//! What a fling IS, and why one-tick-per-frame benchmarks miss it: a shell
-//! pump drains every input the OS queued since the last present and then calls
-//! render() once. A trackpad fling therefore arrives as a BATCH of wheel
-//! notches inside one frame, and the frame budget is spent on the batch plus
-//! the single render — not on one notch. Every timed region below is exactly
-//! that: `ticks_per_frame` real `Mouse.wheel_down`/`wheel_up` presses through
-//! `Pardes.update`, one `render()` into a reset frame arena, and the effect
-//! drain, which is the loop src/tui/tui.zig and src/gui/gui.zig both run.
-//!
-//! Rows are per-FRAME latency, not per-notch: a dropped frame is what a reader
-//! feels, and the batch is indivisible from the reader's point of view.
-//!
-//! Two checksums, because they answer two different questions:
-//!
-//! `visual` is the no-regression proof. Frame count, emitted image count,
-//! every placement rectangle, the presented RGBA (strided every
-//! frame, in full every `full_rgba_stride`-th frame), and the
-//! final scroll offset and page. Any optimization that changes
-//! ONE presented pixel or ONE placement changes this. It must be
-//! bit-identical across builds.
-//! `transport` is the texture-upload trace: the (page, raster generation)
-//! pair per emitted image, with generations rebased on the
-//! counter's value at run start. Caching work legitimately
-//! changes this while `visual` stays fixed — reusing a retained
-//! raster keeps its old generation, which is precisely the
-//! retransmit the revision mechanism exists to suppress.
-//!
-//! Setup, teardown, checksumming, and reporting are outside every timer.
+//! `visual` hashes what the reader sees (every placement's rectangles and
+//! the rows of its pixels shown) and must be the same every round.
+//! Setup, teardown, checksumming and reporting are outside every timer.
const std = @import("std");
const bench_util = @import("bench_util.zig");
const nowNs = bench_util.nowNs;
const fatal = bench_util.Fatal("pdf-scroll-bench").f;
const mix = bench_util.mix;
-const drainEffects = bench_util.drainEffects;
-const libc = std.c;
const pardes = @import("pardes");
-const pdf = @import("mupdf");
-const bench_config = @import("pdf_scroll_bench_config");
+const bench_config = @import("pdf_bench_config");
pub const std_options: std.Options = .{ .log_level = .err };
const gpa = std.heap.smp_allocator;
-// A reading-sized window. Wide enough that fit-width pages are taller than the
-// viewport (so a fling genuinely crosses page boundaries) and small enough
-// that the Kitty raster policy's 1200px cap is the binding quality term, which
-// is what the tui shell actually ships.
+// A reading-sized window: fit-width pages taller than the viewport, so a
+// fling crosses page boundaries.
const bench_cols: u16 = 120;
const bench_rows: u16 = 40;
const default_cell_pixels: pardes.CellPixels = .{ .w = 8, .h = 16 };
-
-/// config.wheel_rows is one cell per notch, so a notch is `cell_pixels.h`
-/// display pixels. 192 notches is ~3072px — between two and three fit-width
-/// pages of travel inside a single frame, i.e. a hard flick whose momentum the
-/// OS delivers as one burst. This is the number that makes the benchmark about
-/// flinging rather than about nudging.
+/// 192 notches of a cell each, ~3072px: two to three pages in a frame.
const fling_ticks_per_frame: usize = 192;
const slow_ticks_per_frame: usize = 1;
-/// The control row is a fixed wall of frames rather than a whole-document
-/// traversal: one notch per frame over a real document is tens of thousands of
-/// frames, and 240 already crosses several page boundaries at reading speed.
const default_slow_frames: usize = 240;
-/// Guard against a pathological document turning one row into a multi-minute
-/// run. Deterministic either way: the frame count is derived from layout.
const max_frames: usize = 2048;
-/// Full-RGBA hashing is O(document) per frame, so it samples the run instead of
-/// every frame; the strided hash below runs on every frame and every image.
-const full_rgba_stride: usize = 8;
-const checksum_seed: u64 = 0x7061_7264_6573_5343;
const Config = struct {
path: []const u8 = "docs/design.pdf",
json: bool = false,
warmup: usize = 2,
reps: usize = 25,
- /// Override `max_frames` for one run: a 5000-page manual is 2000 frames of
- /// travel per round, which is a fine scoreboard and a poor edit-run loop.
frames: ?usize = null,
- /// One cell in display pixels: 16x32 is the same window on a 2x display.
cell: pardes.CellPixels = default_cell_pixels,
- /// Frames of the one-notch control row.
slow_frames: usize = default_slow_frames,
};
-/// The bit-exact description of one scrolling run. `visual` is the invariant;
-/// see the file header for why `transport` is reported beside it rather than
-/// folded into it.
-const Identity = struct {
- frames: u64,
- images: u64,
- visual: u64,
- transport: u64,
-
- fn eql(a: Identity, b: Identity) bool {
- return a.frames == b.frames and a.images == b.images and
- a.visual == b.visual and a.transport == b.transport;
- }
-};
-
const Result = struct {
name: []const u8,
ticks_per_frame: usize,
@@ -123,289 +63,239 @@ const Result = struct {
p99_ns: u64,
max_ns: u64,
total_ns: u64,
+ /// Rasters the frames showed (distinct keys): pages rendered.
rasterizations: u64,
- rasters_retained: u64,
- /// Most bytes the pane's rasters held after any frame: every slot's
- /// pixels, its clean copy, and the spare buffers kept for reuse.
- peak_raster_bytes: u64,
- identity: Identity,
+ images: u64,
+ visual: u64,
};
const Scenario = enum {
- /// Sustained high-velocity travel from page zero to the last page.
fling_down,
- /// Down to the end and straight back up. Worst case for any cache that
- /// only retains what the current frame shows: the way back finds nothing.
fling_reverse,
- /// One notch per frame. The control: it must not regress.
slow_scroll,
- fn label(scenario: Scenario) []const u8 {
- return switch (scenario) {
- .fling_down => "fling_down",
- .fling_reverse => "fling_reverse",
- .slow_scroll => "slow_scroll",
- };
- }
-
fn ticks(scenario: Scenario) usize {
- return switch (scenario) {
- .fling_down, .fling_reverse => fling_ticks_per_frame,
- .slow_scroll => slow_ticks_per_frame,
- };
+ return if (scenario == .slow_scroll) slow_ticks_per_frame else fling_ticks_per_frame;
}
};
-const Plan = struct {
- scenario: Scenario,
- frames: usize,
- /// Frames of downward travel before `fling_reverse` turns around.
- down_frames: usize,
-
- fn button(plan: Plan, frame: usize) pardes.Mouse.Button {
- return switch (plan.scenario) {
- .fling_reverse => if (frame < plan.down_frames) .wheel_down else .wheel_up,
- .fling_down, .slow_scroll => .wheel_down,
- };
- }
-};
+const Plan = struct { scenario: Scenario, frames: usize, down_frames: usize };
pub fn main(init: std.process.Init) !void {
if (!bench_config.release_fast_core) std.debug.print(
- "WARNING: the core is {s}, not ReleaseFast — these timings are for" ++
- " reading a profile, NOT for the scoreboard\n",
+ "WARNING: the core is {s}, not ReleaseFast — these timings are for reading a profile, NOT for the scoreboard\n",
.{bench_config.core_optimize},
);
const args = try init.minimal.args.toSlice(init.arena.allocator());
const config = parseArgs(args);
-
- // One untimed probe boot supplies the strip height every plan is sized
- // from, so the frame counts below are a property of the document and the
- // viewport rather than of the machine.
+ workers = std.c.getenv("BENCH_WORKERS") != null;
const travel = probeTravel(config);
-
const cap = config.frames orelse max_frames;
+ const per_frame = @as(f64, @floatFromInt(config.cell.h)) * fling_ticks_per_frame;
+ const wanted: usize = @intFromFloat(@min(@as(f64, @floatFromInt(cap)), @ceil(travel.max_scroll / per_frame)));
+ const down = @max(1, @min(cap, wanted));
const results = [_]Result{
- try measure(config, planFor(.fling_down, travel, cap, config.slow_frames)),
- try measure(config, planFor(.fling_reverse, travel, cap, config.slow_frames)),
- try measure(config, planFor(.slow_scroll, travel, cap, config.slow_frames)),
+ try measure(config, .{ .scenario = .fling_down, .frames = down, .down_frames = down }),
+ try measure(config, .{ .scenario = .fling_reverse, .frames = @max(2, down), .down_frames = @max(1, down / 2) }),
+ try measure(config, .{ .scenario = .slow_scroll, .frames = config.slow_frames, .down_frames = config.slow_frames }),
};
-
- if (config.json)
- reportJson(init.io, config, travel, &results)
- else
- reportText(config, travel, &results);
+ if (config.json) reportJson(init.io, config, travel, &results) else reportText(config, travel, &results);
}
fn parseArgs(args: []const []const u8) Config {
var config: Config = .{};
var i: usize = 1;
while (i < args.len) : (i += 1) {
- if (std.mem.eql(u8, args[i], "--json")) {
+ const a = args[i];
+ if (std.mem.eql(u8, a, "--json")) {
config.json = true;
- } else if (std.mem.eql(u8, args[i], "--warmup") and i + 1 < args.len) {
- i += 1;
- config.warmup = parseCount("--warmup", args[i]);
- } else if (std.mem.eql(u8, args[i], "--reps") and i + 1 < args.len) {
- i += 1;
- config.reps = parseCount("--reps", args[i]);
- } else if (std.mem.eql(u8, args[i], "--path") and i + 1 < args.len) {
- i += 1;
- config.path = args[i];
- } else if (std.mem.eql(u8, args[i], "--frames") and i + 1 < args.len) {
- i += 1;
- config.frames = parseCount("--frames", args[i]);
- } else if (std.mem.eql(u8, args[i], "--slow-frames") and i + 1 < args.len) {
- i += 1;
- config.slow_frames = parseCount("--slow-frames", args[i]);
- } else if (std.mem.eql(u8, args[i], "--cell") and i + 1 < args.len) {
- i += 1;
- var it = std.mem.splitScalar(u8, args[i], 'x');
- const w = parseCount("--cell", it.next() orelse "");
- const h = parseCount("--cell", it.next() orelse "");
- if (w > std.math.maxInt(u16) or h > std.math.maxInt(u16)) fatal("--cell {s} is too large", .{args[i]});
- config.cell = .{ .w = @intCast(w), .h = @intCast(h) };
- } else fatal(
- "usage: pardes-pdf-scroll-bench [--json] [--warmup N] [--reps N]" ++
- " [--path FILE] [--frames N] [--slow-frames N] [--cell WxH]",
- .{},
- );
+ continue;
+ }
+ if (i + 1 >= args.len) fatal("usage: pardes-pdf-scroll-bench [--json] [--warmup N] [--reps N] [--path FILE] [--frames N] [--slow-frames N] [--cell WxH]", .{});
+ i += 1;
+ const v = args[i];
+ if (std.mem.eql(u8, a, "--warmup")) config.warmup = parseCount(a, v) else if (std.mem.eql(u8, a, "--reps")) config.reps = parseCount(a, v) else if (std.mem.eql(u8, a, "--path")) config.path = v else if (std.mem.eql(u8, a, "--frames")) config.frames = parseCount(a, v) else if (std.mem.eql(u8, a, "--slow-frames")) config.slow_frames = parseCount(a, v) else if (std.mem.eql(u8, a, "--cell")) {
+ var it = std.mem.splitScalar(u8, v, 'x');
+ config.cell = .{ .w = @intCast(parseCount(a, it.next() orelse "")), .h = @intCast(parseCount(a, it.next() orelse "")) };
+ } else fatal("unknown flag {s}", .{a});
}
return config;
}
-const Travel = struct {
- /// Scrollable display pixels: the whole strip minus one screenful.
- max_scroll: f64,
- /// Display pixels one fling frame's notch batch covers.
- per_frame: f64,
- /// What the document turned out to be, so a row is readable next to a row
- /// from another file: a 7-page paper and a 5000-page manual are not the
- /// same benchmark and the report must not pretend otherwise.
- pages: usize,
- strip_px: u64,
- /// The first frame of a fresh pane: opening the document and rasterizing
- /// what it shows first. One sample, from the probe boot.
- first_frame_ns: u64,
-};
+fn parseCount(flag: []const u8, text: []const u8) usize {
+ return bench_util.parsePositive(fatal, flag, text, 10_000);
+}
-fn probeTravel(config: Config) Travel {
- const path = config.path;
- const core = bootCore(config) catch |err|
- fatal("cannot boot a PDF pane on {s}: {t}", .{ path, err });
- defer core.deinit();
- var frame_arena = std.heap.ArenaAllocator.init(gpa);
- defer frame_arena.deinit();
- const started = nowNs();
- _ = core.render(frame_arena.allocator()) catch |err|
- fatal("first render failed: {t}", .{err});
- drainEffects(core);
- const first_frame_ns = nowNs() -| started;
- const pane = core.panes[0] orelse fatal("PDF pane disappeared", .{});
- if (pane.pdf == null) fatal("{s} did not open as a PDF", .{path});
- const pv = pane.pdf.?;
- if (!pv.layout_valid or pv.document_height == 0)
- fatal("the pixel strip never laid out; is native_images/cell_pixels wired?", .{});
- // Exactly the clamp scrollPdfDocument enforces, so the last fling frame
- // lands on the document end instead of adding a no-op frame to the row.
- const viewport_h: u64 = @as(u64, bench_rows -| pardes.BOX_H) * config.cell.h;
- // Four screenfuls of strip, in pages or in one tall page.
- if (pv.document_height < 4 * viewport_h)
- fatal("{s} is {d}px of strip; a fling benchmark needs at least four screenfuls", .{ path, pv.document_height });
- const notch: f64 = @floatFromInt(config.cell.h);
- if (!std.math.isFinite(notch) or notch <= 0)
- fatal("a notch of {d} display pixels cannot size a fling", .{notch});
- const strip = @as(f64, @floatFromInt(pv.document_height -| viewport_h));
- if (!std.math.isFinite(strip))
- fatal("a {d}-page strip does not fit a float; refusing to invent a plan", .{pv.page_count});
- return .{
- .max_scroll = @max(notch, strip),
- .per_frame = notch * @as(f64, @floatFromInt(fling_ticks_per_frame)),
- .pages = pv.page_count,
- .strip_px = pv.document_height,
- .first_frame_ns = first_frame_ns,
+/// Its jobs: run here, at once; or (BENCH_WORKERS set) on threads of
+/// their own as a shell's workers run them, each landing at the first
+/// drain after it is done, the frame never waiting on one.
+fn drain(core: *pardes.Pardes) void {
+ if (!workers) {
+ while (core.nextEffect()) |e| if (e == .job) core.perform(e);
+ return;
+ }
+ var i: usize = 0;
+ while (i < running.items.len) {
+ const r = running.items[i];
+ if (!r.done.load(.acquire)) {
+ i += 1;
+ continue;
+ }
+ r.thread.join();
+ r.job.land(core);
+ gpa.destroy(r);
+ _ = running.swapRemove(i);
+ }
+ while (core.nextEffect()) |e| if (e == .job) {
+ const j = pardes.host_io.KindJob.take(core, e.job.pane, e.job.serial, e.job.token) orelse continue;
+ const r = gpa.create(Running) catch continue;
+ r.* = .{ .job = j, .thread = undefined };
+ r.thread = std.Thread.spawn(.{}, Running.run, .{r}) catch {
+ gpa.destroy(r);
+ j.run();
+ j.land(core);
+ continue;
+ };
+ running.append(gpa, r) catch {};
};
}
-/// Frames are derived from the document, then capped — `max_frames` is a
-/// property of the BENCHMARK (a row must not become a multi-minute run on a
-/// 5000-page manual), and it caps the whole plan, not merely its downward half.
-/// The cap is applied before any arithmetic on the count, so a document the
-/// float division cannot describe still produces a runnable plan instead of an
-/// overflow.
-fn planFor(scenario: Scenario, travel: Travel, cap: usize, slow: usize) Plan {
- const wanted = travel.max_scroll / travel.per_frame;
- const whole: usize = if (std.math.isFinite(wanted) and wanted >= 1)
- @intFromFloat(@min(@as(f64, @floatFromInt(cap)), @ceil(wanted)))
- else
- 1;
- const down = @min(cap, whole);
- return switch (scenario) {
- .fling_down => .{ .scenario = scenario, .frames = down, .down_frames = down },
- // half down, half back: the reverse row keeps the plan's frame budget
- .fling_reverse => .{
- .scenario = scenario,
- .frames = @max(2, down),
- .down_frames = @max(1, down / 2),
- },
- .slow_scroll => .{ .scenario = scenario, .frames = slow, .down_frames = slow },
- };
+const Running = struct {
+ job: *pardes.host_io.KindJob,
+ thread: std.Thread,
+ done: std.atomic.Value(bool) = .init(false),
+
+ fn run(r: *Running) void {
+ r.job.run();
+ r.done.store(true, .release);
+ }
+};
+var running: std.ArrayList(*Running) = .empty;
+var workers = false;
+
+/// Every job still out, landed: before its core goes.
+fn settle(core: *pardes.Pardes) void {
+ while (running.items.len > 0) drain(core);
}
-/// A pane on a real multi-page document with the native pixel-strip path live
-/// and a genuine pixel viewport delivered the way a shell delivers one.
+const Travel = struct { max_scroll: f64, pages: u32, first_frame_ns: u64 };
+
+/// When bootCore's Look began: the first frame is from there to the first
+/// page landed (the document opened, sized and drawn).
+var look_started: u64 = 0;
+
+/// A core showing `config.path` as the addon's pane, laid out and its
+/// first page landed.
fn bootCore(config: Config) !*pardes.Pardes {
- const core = try pardes.Pardes.init(gpa, .{
- .file = config.path,
- .cols = bench_cols,
- .rows = bench_rows,
- });
+ const core = try pardes.Pardes.init(gpa, .{ .cols = bench_cols, .rows = bench_rows, .tty_only = true });
errdefer core.deinit();
- core.frontend.native_images = true;
- core.update(.{ .resize = .{
- .cols = bench_cols,
- .rows = bench_rows,
- .cell_pixels = config.cell,
- } });
- drainEffects(core);
+ core.view().frontend.native_images = true;
+ core.update(.{ .resize = .{ .cols = bench_cols, .rows = bench_rows, .cell_pixels = config.cell } });
+ pardes.Addon.perform(core, .load, 0, bench_config.pdf_addon);
+ drain(core);
+ var line: [4200]u8 = undefined;
+ const real = try std.Io.Dir.cwd().realPathFileAlloc(std.Io.Threaded.global_single_threaded.io(), config.path, gpa);
+ defer gpa.free(real);
+ look_started = nowNs();
+ if (!core.executeBuiltinLine(core.view().active, std.fmt.bufPrint(&line, "Look {s}", .{real}) catch unreachable)) return error.NoLook;
+ // The text pane it was looked from goes: the PDF alone in the window.
+ const pdf = core.view().active;
+ for (core.panes, 0..) |slot, id| if (slot != null and id != pdf) try core.removePane(id, null);
+ core.view().active = pdf;
+ drain(core);
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ for (0..4) |_| {
+ _ = arena.reset(.retain_capacity);
+ _ = try core.render(arena.allocator());
+ core.update(.tick);
+ drain(core);
+ settle(core);
+ }
+ if ((core.panes[pdf] orelse return error.NoPane).addon == null) return error.NotAPdf;
return core;
}
+fn pdfPane(core: *pardes.Pardes) *pardes.Pane {
+ return core.panes[core.view().active].?;
+}
+
+fn probeTravel(config: Config) Travel {
+ const core = bootCore(config) catch |err| fatal("cannot open {s} as a PDF pane: {t}", .{ config.path, err });
+ defer core.deinit();
+ defer settle(core);
+ const first_frame_ns = nowNs() -| look_started;
+ const slot = pdfPane(core).addon.?;
+ if (slot.thumb_span <= 0) fatal("{s}: the strip never laid out", .{config.path});
+ // The strip is the window over the share of it shown.
+ const window_px = @as(f64, @floatFromInt(pardes.Kind.viewOf(core, core.view().active).rows)) * @as(f64, @floatFromInt(config.cell.h));
+ var pages: u32 = 0;
+ const prefix = slot.prefix.slice();
+ if (std.mem.indexOfScalar(u8, prefix, '/')) |slash| pages = std.fmt.parseInt(u32, prefix[slash + 1 .. prefix.len - 1], 10) catch 0;
+ return .{ .max_scroll = window_px / slot.thumb_span - window_px, .pages = pages, .first_frame_ns = first_frame_ns };
+}
+
fn measure(config: Config, plan: Plan) !Result {
- const name = plan.scenario.label();
const ticks = plan.scenario.ticks();
- const totals = try gpa.alloc(u64, config.reps);
- defer gpa.free(totals);
const pool = try gpa.alloc(u64, config.reps * plan.frames);
defer gpa.free(pool);
- var pool_len: usize = 0;
- const recorder = try gpa.create(Recorder);
- defer gpa.destroy(recorder);
- var expected: ?Identity = null;
- var rasterizations: u64 = 0;
- var rasters_retained: u64 = 0;
- var peak_raster_bytes: u64 = 0;
-
+ const totals = try gpa.alloc(u64, config.reps);
+ defer gpa.free(totals);
+ var n: usize = 0;
+ var expected: ?u64 = null;
+ var rasters: u64 = 0;
+ var images: u64 = 0;
for (0..config.warmup + config.reps) |round| {
const core = try bootCore(config);
defer core.deinit();
- var frame_arena = std.heap.ArenaAllocator.init(gpa);
- defer frame_arena.deinit();
- // Untimed: the steady state a reader flings FROM already has page zero
- // resident and the strip laid out.
- _ = try core.render(frame_arena.allocator());
- drainEffects(core);
- const pane = core.panes[0] orelse fatal("PDF pane disappeared", .{});
- const pv = pane.pdf.?;
- const rect = core.rects[0];
- // Anywhere inside the pane body resolves to this pane's hover, which
- // is what routes the notch; the exact cell is irrelevant to a wheel.
- const wheel: pardes.Mouse = .{
- .button = .wheel_down,
- .kind = .press,
- .col = rect.x + rect.w / 2,
- .row = rect.y + rect.h / 2,
- };
- const base_revision = pv.next_raster_revision;
- recorder.reset(base_revision);
-
+ defer settle(core);
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const id = core.view().active;
+ const rect = core.view().rects[id];
+ var keys: std.AutoHashMap(u64, void) = .init(gpa);
+ defer keys.deinit();
+ var visual: u64 = 0x7061_7264_6573_5343;
+ var shown: u64 = 0;
var total: u64 = 0;
for (0..plan.frames) |frame| {
- var event = wheel;
- event.button = plan.button(frame);
+ const button: pardes.Mouse.Button = if (plan.scenario == .fling_reverse and frame >= plan.down_frames) .wheel_up else .wheel_down;
const started = nowNs();
- for (0..ticks) |_| core.update(.{ .mouse = event });
- _ = frame_arena.reset(.retain_capacity);
- const surface = core.render(frame_arena.allocator()) catch |err|
- fatal("render failed in {s} frame {d}: {t}", .{ name, frame, err });
- drainEffects(core);
+ for (0..ticks) |_| core.update(.{ .mouse = .{ .button = button, .kind = .press, .col = rect.x + rect.w / 2, .row = rect.y + rect.h / 2 } });
+ _ = arena.reset(.retain_capacity);
+ const surface = try core.render(arena.allocator());
+ drain(core);
const elapsed = nowNs() -| started;
total += elapsed;
- recorder.observe(frame, surface, pane);
- peak_raster_bytes = @max(peak_raster_bytes, rasterBytes(pv));
- // The same frame boundary src/tui/tui.zig marks, so a Tracy capture
- // of this benchmark reads like a capture of the real shell.
- pardes.frameMark();
+ for (surface.placements.items) |pl| {
+ shown += 1;
+ try keys.put(pl.key, {});
+ visual = mix(visual, [_]u64{ pl.dst.x, pl.dst.y, pl.dst.w, pl.dst.h, pl.src.x, pl.src.y, pl.src.w, pl.src.h });
+ if (pl.at) |at| visual = mix(visual, [_]u64{ at.x, at.y, at.w, at.h });
+ const bytes = pl.raster.bytes();
+ const stride: usize = pl.raster.stride;
+ var row: u32 = 0;
+ while (row < pl.src.h and (@as(usize, pl.src.y) + row + 1) * stride <= bytes.len) : (row += 7)
+ visual = std.hash.Wyhash.hash(visual, bytes[(@as(usize, pl.src.y) + row) * stride ..][0..stride]);
+ }
if (round >= config.warmup) {
- pool[pool_len] = @max(1, elapsed);
- pool_len += 1;
+ pool[n] = @max(1, elapsed);
+ n += 1;
}
}
-
- const identity = recorder.finish(pv);
- try verifyIdentity(name, &expected, identity, round);
+ if (expected) |want| {
+ if (want != visual and !workers) return error.UnstableVisual;
+ } else expected = visual;
if (round >= config.warmup) {
totals[round - config.warmup] = total;
- rasterizations = pv.next_raster_revision -% base_revision;
- rasters_retained = pv.rasters_len;
+ rasters = keys.count();
+ images = shown;
}
}
-
- std.debug.assert(pool_len == pool.len);
std.mem.sort(u64, pool, {}, std.sort.asc(u64));
std.mem.sort(u64, totals, {}, std.sort.asc(u64));
return .{
- .name = name,
+ .name = @tagName(plan.scenario),
.ticks_per_frame = ticks,
.frames = plan.frames,
.min_ns = pool[0],
@@ -414,261 +304,24 @@ fn measure(config: Config, plan: Plan) !Result {
.p99_ns = pool[(pool.len * 99) / 100 -| 1],
.max_ns = pool[pool.len - 1],
.total_ns = totals[totals.len / 2],
- .rasterizations = rasterizations,
- .rasters_retained = rasters_retained,
- .peak_raster_bytes = peak_raster_bytes,
- .identity = expected.?,
+ .rasterizations = rasters,
+ .images = images,
+ .visual = expected.?,
};
}
-/// What the pane's rasters hold right now (untimed: outside the frame).
-fn rasterBytes(pv: anytype) u64 {
- var total: u64 = 0;
- for (pv.rasters[0..pv.rasters_len]) |raster| total += raster.rgba.len + raster.clean.len;
- for (pv.spare[0..pv.spare_len]) |spare| total += spare.len;
- return total;
-}
-
-/// Accumulates the two checksums across one run. Raster generations are
-/// rebased on the counter's value at run start so the trace describes the
-/// ORDER textures were produced in rather than absolute counter values, which
-/// depend on how much the untimed boot happened to rasterize.
-const Recorder = struct {
- frames: u64,
- images: u64,
- visual: u64,
- transport: u64,
- base_revision: u32,
-
- fn reset(self: *Recorder, base_revision: u32) void {
- self.* = .{
- .frames = 0,
- .images = 0,
- .visual = checksum_seed,
- .transport = checksum_seed,
- .base_revision = base_revision,
- };
- }
-
- /// The `visual` half hashes WHAT THE READER SEES and nothing else: the
- /// destination rectangle, and the pixels inside the source rectangle the
- /// backend samples — walked row by row out of the texture, so a page
- /// delivered as a narrow band and the same page delivered whole hash the
- /// same. Texture dimensions, buffer lengths and revisions are transport,
- /// and they live in the other checksum on purpose: fast scrolling is
- /// ALLOWED to change how pixels are carried and is never allowed to change
- /// which pixels arrive.
- fn observe(self: *Recorder, frame: usize, surface: *const pardes.Surface, pane: *const pardes.Pane) void {
- self.frames += 1;
- self.visual = mix(self.visual, surface.placements.items.len);
- self.transport = mix(self.transport, surface.placements.items.len);
- const full = frame % full_rgba_stride == 0;
- for (surface.placements.items) |place| {
- self.images += 1;
- self.visual = mix(self.visual, place.pane);
- self.visual = mix(self.visual, place.dst.x);
- self.visual = mix(self.visual, place.dst.y);
- self.visual = mix(self.visual, place.dst.w);
- self.visual = mix(self.visual, place.dst.h);
- for (surface.regionList()) |r| if (r.kind == .body and r.owner == place.pane) {
- self.visual = mix(self.visual, @as(u64, @bitCast(r.scroll_px - @floor(r.scroll_px))));
- };
- if (place.at) |rect| {
- self.visual = mix(self.visual, rect.x);
- self.visual = mix(self.visual, rect.y);
- self.visual = mix(self.visual, rect.w);
- self.visual = mix(self.visual, rect.h);
- self.visual = self.hashSource(self.visual, place, place.src, full);
- } else {
- self.visual = mix(self.visual, std.math.maxInt(u64));
- self.visual = if (full)
- std.hash.Wyhash.hash(self.visual, place.raster.bytes())
- else
- stridedHash(self.visual, place.raster.bytes());
- }
- // The revision the key is of, from the slot holding it (0: a
- // page's paper before it lands).
- const revision = for (pane.pdf.?.rasters[0..pane.pdf.?.rasters_len]) |r| {
- if (r.valid and pardes.Placement.keyOf(pane.serial, @intCast(r.page), r.revision) == place.key) break r.revision;
- } else 0;
- self.transport = mix(self.transport, revision -% self.base_revision);
- self.transport = mix(self.transport, place.raster.w);
- self.transport = mix(self.transport, place.raster.h);
- }
- }
-
- /// Hash the source rectangle's pixels out of a texture of `place.iw` pixels
- /// per row. `full` walks every row; otherwise the same stride the whole-
- /// buffer hash used, so the cost stays a fraction of a frame.
- fn hashSource(
- self: *Recorder,
- seed: u64,
- place: pardes.Placement,
- src: anytype,
- full: bool,
- ) u64 {
- _ = self;
- const rgba = place.raster.bytes();
- const stride: usize = place.raster.stride;
- if (stride == 0 or rgba.len < stride) return mix(seed, std.math.maxInt(u64));
- const rows = @min(src.h, @as(u32, @intCast(rgba.len / stride)) -| src.y);
- var hash = mix(seed, rows);
- var row: u32 = 0;
- while (row < rows) : (row += if (full) 1 else 7) {
- const start = (@as(usize, src.y) + row) * stride + @as(usize, src.x) * 4;
- const len = @min(@as(usize, src.w) * 4, rgba.len -| start);
- if (len == 0) break;
- hash = std.hash.Wyhash.hash(hash, rgba[start..][0..len]);
- }
- return hash;
- }
-
- fn finish(self: *Recorder, pv: anytype) Identity {
- var visual = mix(self.visual, @as(u64, @bitCast(pv.document_scroll_y)));
- visual = mix(visual, pv.page);
- visual = mix(visual, pv.page_count);
- visual = mix(visual, pv.document_height);
- return .{
- .frames = self.frames,
- .images = self.images,
- .visual = visual,
- .transport = self.transport,
- };
- }
-};
-
-/// 256 fixed-stride RGBA words. Cheap enough for every image of every frame
-/// while still failing on any change that touches a band of the page, and the
-/// full hash above closes the gap on sampled frames.
-fn stridedHash(seed: u64, rgba: []const u8) u64 {
- if (rgba.len < 4) return mix(seed, rgba.len);
- const stride = @max(@as(usize, 4), (rgba.len / 256) & ~@as(usize, 3));
- var hash = seed;
- var at: usize = 0;
- while (at + 4 <= rgba.len) : (at += stride)
- hash = std.hash.Wyhash.hash(hash, rgba[at..][0..4]);
- return hash;
-}
-
-fn verifyIdentity(
- name: []const u8,
- expected: *?Identity,
- got: Identity,
- round: usize,
-) !void {
- if (expected.*) |want| {
- if (!want.eql(got)) {
- std.debug.print(
- "pdf-scroll-bench: unstable identity in {s} round {d}:\n" ++
- " expected frames={d} images={d} visual={x:0>16} transport={x:0>16}\n" ++
- " got frames={d} images={d} visual={x:0>16} transport={x:0>16}\n",
- .{
- name, round, want.frames, want.images,
- want.visual, want.transport, got.frames, got.images,
- got.visual, got.transport,
- },
- );
- return error.UnstableIdentity;
- }
- } else expected.* = got;
-}
-
fn reportText(config: Config, travel: Travel, results: []const Result) void {
- std.debug.print("pardes PDF fast-scroll benchmark ({s})\n", .{bench_config.core_optimize});
- std.debug.print(
- "{s}: {d} pages, {d}px strip at {d}x{d} cells of {d}x{d}px\n",
- .{
- config.path, travel.pages, travel.strip_px, bench_cols,
- bench_rows, config.cell.w, config.cell.h,
- },
- );
- std.debug.print("first frame of a fresh pane: {d}us\n", .{travel.first_frame_ns / 1000});
- std.debug.print(
- "warmup: {d}, sampled reps: {d}; every number is ONE FRAME (notch batch + render + drain)\n\n",
- .{ config.warmup, config.reps },
- );
- std.debug.print("{s:<16} {s:>6} {s:>7} {s:>10} {s:>10} {s:>10} {s:>10} {s:>10} {s:>11} {s:>7} {s:>5} {s:>9} {s:<16} {s}\n", .{
- "scenario", "frames", "notches", "min",
- "median", "p90", "p99", "max",
- "total_us", "rasters", "kept", "peak_kib",
- "visual", "transport",
- });
- std.debug.print("{s}\n", .{&@as([160]u8, @splat('-'))});
- for (results) |result| std.debug.print(
- "{s:<16} {d:>6} {d:>7} {d:>10} {d:>10} {d:>10} {d:>10} {d:>10} {d:>11} {d:>7} {d:>5} {d:>9} {x:0>16} {x:0>16}\n",
- .{
- result.name,
- result.frames,
- result.ticks_per_frame,
- result.min_ns,
- result.median_ns,
- result.p90_ns,
- result.p99_ns,
- result.max_ns,
- result.total_ns / 1000,
- result.rasterizations,
- result.rasters_retained,
- result.peak_raster_bytes / 1024,
- result.identity.visual,
- result.identity.transport,
- },
- );
- std.debug.print(
- "\n`rasters` counts MuPDF page rasterizations over one run; `kept` is the retained raster count at the end;\n" ++
- "`peak_kib` is the most the pane's rasters, clean copies and spares held after any frame.\n" ++
- "`visual` pins presented pixels and placement and must never change; `transport` traces texture uploads.\n",
- .{},
- );
+ std.debug.print("pardes PDF fast-scroll benchmark ({s}): {s}, {d} pages, {d:.0}px of travel, cells {d}x{d}px\n", .{ bench_config.core_optimize, config.path, travel.pages, travel.max_scroll, config.cell.w, config.cell.h });
+ std.debug.print("first frame of a fresh pane: {d}us; every number is ONE FRAME (notches + render + drain)\n", .{travel.first_frame_ns / 1000});
+ std.debug.print("{s:<14} {s:>6} {s:>7} {s:>10} {s:>10} {s:>10} {s:>10} {s:>10} {s:>8} {s}\n", .{ "scenario", "frames", "notches", "min", "median", "p90", "p99", "max", "rasters", "visual" });
+ for (results) |r| std.debug.print("{s:<14} {d:>6} {d:>7} {d:>10} {d:>10} {d:>10} {d:>10} {d:>10} {d:>8} {x:0>16}\n", .{ r.name, r.frames, r.ticks_per_frame, r.min_ns, r.median_ns, r.p90_ns, r.p99_ns, r.max_ns, r.rasterizations, r.visual });
}
-const json_report_max_bytes = 16 * 1024;
-
fn reportJson(io: std.Io, config: Config, travel: Travel, results: []const Result) void {
- var storage: [json_report_max_bytes]u8 = undefined;
+ var storage: [16 * 1024]u8 = undefined;
var out: std.Io.Writer = .fixed(&storage);
- out.print(
- "{{\"benchmark\":\"pardes-pdf-scroll\",\"build\":\"{s}\",\"path\":\"{s}\"," ++
- "\"pages\":{d},\"strip_px\":{d},\"first_frame_ns\":{d},\"warmup\":{d},\"reps\":{d},\"results\":[",
- .{
- bench_config.core_optimize, config.path, travel.pages,
- travel.strip_px, travel.first_frame_ns, config.warmup,
- config.reps,
- },
- ) catch return;
- for (results, 0..) |result, i| out.print(
- "{s}{{\"scenario\":\"{s}\",\"frames\":{d},\"notches_per_frame\":{d}," ++
- "\"min_ns\":{d},\"median_ns\":{d},\"p90_ns\":{d},\"p99_ns\":{d},\"max_ns\":{d},\"total_ns\":{d}," ++
- "\"rasterizations\":{d},\"rasters_retained\":{d},\"peak_raster_bytes\":{d},\"images\":{d}," ++
- "\"visual\":\"{x:0>16}\",\"transport\":\"{x:0>16}\"}}",
- .{
- if (i == 0) "" else ",",
- result.name,
- result.frames,
- result.ticks_per_frame,
- result.min_ns,
- result.median_ns,
- result.p90_ns,
- result.p99_ns,
- result.max_ns,
- result.total_ns,
- result.rasterizations,
- result.rasters_retained,
- result.peak_raster_bytes,
- result.identity.images,
- result.identity.visual,
- result.identity.transport,
- },
- ) catch return;
+ out.print("{{\"benchmark\":\"pardes-pdf-scroll\",\"build\":\"{s}\",\"path\":\"{s}\",\"pages\":{d},\"strip_px\":{d:.0},\"first_frame_ns\":{d},\"warmup\":{d},\"reps\":{d},\"results\":[", .{ bench_config.core_optimize, config.path, travel.pages, travel.max_scroll, travel.first_frame_ns, config.warmup, config.reps }) catch return;
+ for (results, 0..) |r, i| out.print("{s}{{\"scenario\":\"{s}\",\"frames\":{d},\"notches_per_frame\":{d},\"min_ns\":{d},\"median_ns\":{d},\"p90_ns\":{d},\"p99_ns\":{d},\"max_ns\":{d},\"total_ns\":{d},\"rasterizations\":{d},\"images\":{d},\"visual\":\"{x:0>16}\"}}", .{ if (i == 0) "" else ",", r.name, r.frames, r.ticks_per_frame, r.min_ns, r.median_ns, r.p90_ns, r.p99_ns, r.max_ns, r.total_ns, r.rasterizations, r.images, r.visual }) catch return;
out.writeAll("]}\n") catch return;
std.Io.File.stdout().writeStreamingAll(io, out.buffered()) catch {};
}
-
-comptime {
- // The bench links the real MuPDF wrapper the core uses; keep the import
- // load-bearing so a build that silently dropped it fails here.
- _ = pdf.Document;
-}
-
-fn parseCount(flag: []const u8, text: []const u8) usize {
- return bench_util.parsePositive(fatal, flag, text, 10_000);
-}