summaryrefslogtreecommitdiff
path: root/test/pdf_pointer_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_pointer_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_pointer_bench.zig')
-rw-r--r--test/pdf_pointer_bench.zig141
1 files changed, 76 insertions, 65 deletions
diff --git a/test/pdf_pointer_bench.zig b/test/pdf_pointer_bench.zig
index 359d2fc1..7a39dd88 100644
--- a/test/pdf_pointer_bench.zig
+++ b/test/pdf_pointer_bench.zig
@@ -1,25 +1,26 @@
-//! Pointer gestures over a native PDF pane: hovering words (the Look preview
-//! highlight), and dragging a selection to its release.
+//! Pointer gestures over a pane of the pdf addon: hovering words (the Look
+//! preview highlight), and dragging a selection to its release.
//!
//! zig build pdf-pointer-bench -Doptimize=ReleaseFast -- [--label L] [--reps N] [--path FILE]
-//! [--cell WxH] [--scroll NOTCHES] [--verify 1] [--live 1]
+//! [--cell WxH] [--scroll NOTCHES]
//!
//! Every frame is timed the way a shell pump spends it: the event through
-//! `Pardes.update`, one `render()` into a reset arena, the effect drain. A
-//! hover step is its motion frame plus every tick frame the preview animates
-//! before settling, which is when the highlight lands. Stdout is CSV,
-//! `label,scenario:kind,round,ns`, one row per frame and one per step
-//! (`kind=step`), for an external statistics pass.
+//! `Pardes.update`, one `render()` into a reset arena, the effect drain (the
+//! addon's page jobs run there). A hover step is its motion frame plus the
+//! tick frames, 16 ms apart, until the kind asks for no more: its word's
+//! highlight lands on one. Stdout is CSV, `label,scenario:kind,round,ns`, a
+//! row a frame and one a step (`kind=step`). Stderr says how many raster
+//! rows the hover uploaded (a frontend holding a picture patches the rows a
+//! placement says changed, `from`; any other picture is uploaded whole).
//!
-//! `visual` hashes every presented raster in full, with its placement, on the
-//! frame each step settles on. It is the no-regression proof: a change to how highlights
-//! are painted may change what it costs, never one presented pixel.
+//! `visual` hashes every presented raster in full, with its placement, on
+//! the frame each step settles on: the same every round.
const std = @import("std");
const bench_util = @import("bench_util.zig");
const nowNs = bench_util.nowNs;
const mix = bench_util.mix;
const pardes = @import("pardes");
-const pdf = @import("mupdf");
+const bench_config = @import("pdf_bench_config");
pub const std_options: std.Options = .{ .log_level = .err };
@@ -36,11 +37,6 @@ const Config = struct {
/// 8x16 is the Kitty policy's cell; the SDL shell's is closer to 16x35.
cell_w: u16 = 8,
cell_h: u16 = 16,
- /// Re-render every raster that claims to hold the current highlights and
- /// compare it row by row: exactness without a baseline build.
- verify: bool = false,
- /// A shell that takes row patches: selections repaint while dragged.
- live: bool = false,
};
pub fn main(init: std.process.Init) !void {
@@ -49,7 +45,7 @@ pub fn main(init: std.process.Init) !void {
var i: usize = 1;
while (i + 1 < args.len) : (i += 2) {
const value = args[i + 1];
- if (std.mem.eql(u8, args[i], "--path")) config.path = value else if (std.mem.eql(u8, args[i], "--label")) config.label = value else if (std.mem.eql(u8, args[i], "--reps")) config.reps = try std.fmt.parseUnsigned(usize, value, 10) else if (std.mem.eql(u8, args[i], "--scroll")) config.scroll = try std.fmt.parseUnsigned(usize, value, 10) else if (std.mem.eql(u8, args[i], "--verify")) config.verify = std.mem.eql(u8, value, "1") else if (std.mem.eql(u8, args[i], "--live")) config.live = std.mem.eql(u8, value, "1") else if (std.mem.eql(u8, args[i], "--cell")) {
+ if (std.mem.eql(u8, args[i], "--path")) config.path = value else if (std.mem.eql(u8, args[i], "--label")) config.label = value else if (std.mem.eql(u8, args[i], "--reps")) config.reps = try std.fmt.parseUnsigned(usize, value, 10) else if (std.mem.eql(u8, args[i], "--scroll")) config.scroll = try std.fmt.parseUnsigned(usize, value, 10) else if (std.mem.eql(u8, args[i], "--cell")) {
var it = std.mem.splitScalar(u8, value, 'x');
config.cell_w = try std.fmt.parseUnsigned(u16, it.next() orelse "", 10);
config.cell_h = try std.fmt.parseUnsigned(u16, it.next() orelse "", 10);
@@ -79,20 +75,26 @@ const Run = struct {
frames: usize = 0,
steps: usize = 0,
last: ?*const pardes.Surface = null,
+ now: u64 = std.time.ns_per_s,
+ held: [64]u64 = undefined,
+ nheld: usize = 0,
+ hover_rows: u64 = 0,
+ hover_uploads: u64 = 0,
+ page_rows: u64 = 0,
fn frame(run: *Run, scenario: []const u8, kind: []const u8, event: pardes.Event) !void {
const started = nowNs();
run.core.update(event);
- while (run.core.nextEffect()) |_| {}
+ drain(run.core);
_ = run.arena.reset(.retain_capacity);
const surface = try run.core.render(run.arena.allocator());
- while (run.core.nextEffect()) |_| {}
+ drain(run.core);
const elapsed = nowNs() - started;
run.step_ns += elapsed;
run.frames += 1;
print("{s},{s}:{s},{d},{d}\n", .{ run.config.label, scenario, kind, run.round, elapsed });
run.last = surface;
- if (run.config.verify) try verify(run.core, run.arena.allocator());
+ run.uploads(scenario, surface);
}
/// The step, then the tick frames its animation asks for (a hover's
@@ -101,8 +103,11 @@ const Run = struct {
run.step_ns = 0;
try run.frame(scenario, kind, event);
var ticks: usize = 0;
- while (run.core.animationActive() and ticks < 32) : (ticks += 1)
+ while (pardes.Kind.wake(run.core) != null and ticks < 32) : (ticks += 1) {
+ run.now += 16 * std.time.ns_per_ms;
+ run.core.advance(run.now);
try run.frame(scenario, "tick", .tick);
+ }
print("{s},{s}:step,{d},{d}\n", .{ run.config.label, scenario, run.round, run.step_ns });
// Only the settled frame is the invariant: how many frames a preview
// takes to arrive is a setting (look_preview_delay_frames).
@@ -110,6 +115,28 @@ const Run = struct {
run.steps += 1;
}
+ /// The rows a frontend uploads for this frame: a key it held last
+ /// frame none, a patch of a key it held its dirty rows, else every row.
+ fn uploads(run: *Run, scenario: []const u8, surface: *const pardes.Surface) void {
+ var next: [64]u64 = undefined;
+ var n: usize = 0;
+ for (surface.placements.items) |place| {
+ const held = std.mem.indexOfScalar(u64, run.held[0..run.nheld], place.key) != null;
+ const up: u64 = if (held) 0 else if (place.dirty) |d| (if (std.mem.indexOfScalar(u64, run.held[0..run.nheld], d.from) != null) d.h else place.raster.h) else place.raster.h;
+ if (std.mem.eql(u8, scenario, "hover")) {
+ run.hover_rows += up;
+ if (up > 0) run.hover_uploads += 1;
+ run.page_rows = @max(run.page_rows, place.raster.h);
+ }
+ if (n < next.len) {
+ next[n] = place.key;
+ n += 1;
+ }
+ }
+ @memcpy(run.held[0..n], next[0..n]);
+ run.nheld = n;
+ }
+
fn observe(run: *Run, surface: *const pardes.Surface) void {
for (surface.placements.items) |place| {
run.visual = mix(run.visual, [_]u64{ place.pane, place.dst.x, place.dst.y, place.dst.w, place.dst.h, place.raster.w, place.raster.h });
@@ -121,60 +148,46 @@ const Run = struct {
}
};
-var verify_failures: usize = 0;
-
-fn verify(core: *pardes.Pardes, arena: std.mem.Allocator) !void {
- const Pdf = pardes.panes.Pdf;
- const pane = core.panes[0] orelse return;
- const state = pane.pdf orelse return;
- const highlights = try Pdf.buildHighlights(state, arena, Pdf.highlightInput(core, 0, pane));
- for (state.rasters[0..state.rasters_len]) |*r| {
- if (!r.valid or r.rgba.len == 0 or r.tint_key == null) continue;
- const want = highlights.forPage(r.page);
- if (comptime !@hasField(@TypeOf(r.*), "baked")) return else if (!std.mem.eql(u8, std.mem.sliceAsBytes(want), std.mem.sliceAsBytes(r.baked))) continue;
- const shape = try state.document.measureRenderAt(r.page, r.request);
- // The oracle is the WHOLE page: a band's own edge rows can differ.
- const whole = try arena.alloc(u8, shape.len);
- try state.document.renderIntoAtWithPaper(r.page, r.request, shape, shape.wholePage(), want, whole, false);
- try pdf.tintRgba(whole, r.tint_key.?.mode, r.tint_key.?.colors);
- const fresh = whole[r.band_y * shape.stride ..][0 .. r.band_h * shape.stride];
- var first: ?usize = null;
- var last: usize = 0;
- var count: usize = 0;
- for (0..r.band_h) |y| {
- const row = fresh[y * shape.stride ..][0..shape.stride];
- if (std.mem.eql(u8, row, r.rgba[y * shape.stride ..][0..shape.stride])) continue;
- if (first == null) first = y;
- last = y;
- count += 1;
- }
- if (first) |f| {
- verify_failures += 1;
- if (verify_failures <= 20) std.debug.print("verify: page {d} rows {d}..{d} ({d} differ), highlights {d}, band {d}+{d}\n", .{ r.page, f + r.band_y, last + r.band_y, count, want.len, r.band_y, r.band_h });
- }
- }
+/// Its jobs run here, at once, as a shell's workers would.
+fn drain(core: *pardes.Pardes) void {
+ while (core.nextEffect()) |e| if (e == .job) core.perform(e);
}
fn measure(config: Config, round: usize) !u64 {
- const core = try pardes.Pardes.init(gpa, .{ .file = config.path, .cols = cols, .rows = rows });
+ const core = try pardes.Pardes.init(gpa, .{ .cols = cols, .rows = rows, .tty_only = true });
defer core.deinit();
- core.frontend.native_images = true;
- if (comptime @hasField(pardes.Pardes, "native_image_patches")) core.frontend.native_image_patches = config.live;
+ core.view().frontend.native_images = true;
var run: Run = .{ .core = core, .arena = .init(gpa), .config = config, .round = round };
defer run.arena.deinit();
const cell: pardes.CellPixels = .{ .w = config.cell_w, .h = config.cell_h };
+ core.advance(run.now);
core.update(.{ .resize = .{ .cols = cols, .rows = rows, .cell_pixels = cell } });
- _ = try core.render(run.arena.allocator());
- const pane = core.panes[0] orelse return error.NoPane;
- if (pane.pdf == null) return error.NotPdf;
- const rect = core.rects[0];
+ pardes.Addon.perform(core, .load, 0, bench_config.pdf_addon);
+ drain(core);
+ const real = try std.Io.Dir.cwd().realPathFileAlloc(std.Io.Threaded.global_single_threaded.io(), config.path, gpa);
+ defer gpa.free(real);
+ var line: [4200]u8 = undefined;
+ if (!core.executeBuiltinLine(core.view().active, try std.fmt.bufPrint(&line, "Look {s}", .{real}))) return error.NoLook;
+ const id = core.view().active;
+ for (core.panes, 0..) |slot, other| if (slot != null and other != id) try core.removePane(other, null);
+ core.view().active = id;
+ drain(core);
+ for (0..4) |_| {
+ _ = try core.render(run.arena.allocator());
+ core.update(.tick);
+ drain(core);
+ }
+ const pane = core.panes[id] orelse return error.NoPane;
+ if (pane.addon == null) return error.NotPdf;
+ const rect = core.view().rects[id];
const mid_col = rect.x + rect.w / 2;
const mid_row = rect.y + rect.h / 2;
for (0..config.scroll) |_| {
core.update(.{ .mouse = .{ .button = .wheel_down, .kind = .press, .col = mid_col, .row = mid_row } });
+ drain(core);
_ = run.arena.reset(.retain_capacity);
_ = try core.render(run.arena.allocator());
- while (core.nextEffect()) |_| {}
+ drain(core);
}
const top = rect.y + 4;
@@ -203,6 +216,7 @@ fn measure(config: Config, round: usize) !u64 {
}
try run.step("select", "release", .{ .mouse = .{ .button = .left, .kind = .release, .col = right, .row = row + 3 } });
}
+ std.debug.print("pdf-pointer-bench uploads: hover {d} rows in {d} uploads over {d} steps (a page is {d} rows)\n", .{ run.hover_rows, run.hover_uploads, run.steps, run.page_rows });
return mix(run.visual, @as(u64, run.steps));
}
@@ -212,6 +226,3 @@ fn print(comptime format: []const u8, args: anytype) void {
_ = std.c.write(1, line.ptr, line.len);
}
-comptime {
- _ = pdf.Document;
-}