From 5a9ad72e45bcac6a0e96af70664b1685e4f10595 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 6 Nov 2026 06:24:42 -0300 Subject: 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 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- test/pdf_bench.zig | 614 ------------------------------------ test/pdf_pointer_bench.zig | 141 +++++---- test/pdf_scroll_bench.zig | 735 ++++++++++++-------------------------------- test/pdf_sections_bench.zig | 517 ------------------------------- test/rollback_bench.zig | 4 +- 5 files changed, 272 insertions(+), 1739 deletions(-) delete mode 100644 test/pdf_bench.zig delete mode 100644 test/pdf_sections_bench.zig diff --git a/test/pdf_bench.zig b/test/pdf_bench.zig deleted file mode 100644 index 9054698b..00000000 --- a/test/pdf_bench.zig +++ /dev/null @@ -1,614 +0,0 @@ -//! Headless MuPDF raster scoreboard. -//! -//! zig build pdf-bench -//! zig build pdf-bench -- --json -//! zig build pdf-bench -- --reps 25 --warmup 4 -//! zig build pdf-bench -- --path other.pdf --page 2 --pages 6 -//! -//! The benchmark links the real src/pdf.zig wrapper and C exception bridge in -//! ReleaseFast. Timed regions include MuPDF rasterization directly into the -//! allocator-owned RGBA output; tint scenarios additionally include the exact -//! in-place PDF transform used by the core. Checksum/free/reporting stay -//! outside. "cold" means a fresh Document and MuPDF page cache, opened from -//! the file's bytes (read once, untimed, as the view reads a file whole), -//! while warm scenarios retain one Document. -const std = @import("std"); -const bench_util = @import("bench_util.zig"); -const nowNs = bench_util.nowNs; -const fatal = bench_util.Fatal("pdf-bench").f; -const pdf = @import("mupdf"); - -pub const std_options: std.Options = .{ .log_level = .err }; - -const gpa = std.heap.page_allocator; -const default_reps: usize = 25; -const default_warmup: usize = 3; -const default_multi_pages: usize = 8; -const checksum_seed: u64 = 0x7061_7264_6573_5044; - -const Config = struct { - path: []const u8 = "docs/design.pdf", - /// the file at `path`, read whole before anything is timed - bytes: []const u8 = "", - page: usize = 0, - reps: usize = default_reps, - warmup: usize = default_warmup, - multi_pages: usize = default_multi_pages, - json: bool = false, -}; - -const Profile = struct { - id: []const u8, - description: []const u8, - viewport_w: u32, - viewport_h: u32, - request: pdf.RenderRequest, -}; - -const profiles = [_]Profile{ - .{ - .id = "kitty-96", - .description = "Kitty 96 DPI, bandwidth-capped raster", - .viewport_w = 0, - .viewport_h = 0, - .request = .{ .dpi = 96, .max_dimension = 1200 }, - }, - .{ - .id = "sdl-192-1600x900", - .description = "SDL 192 DPI, physical 1600x900 viewport minimum", - .viewport_w = 1600, - .viewport_h = 900, - .request = .{ - .dpi = 192, - .minimum_width = 1600, - .minimum_height = 900, - .max_dimension = 4096, - }, - }, -}; - -/// Kept orthogonal so a later benchmark can add another pixel treatment -/// without changing lifecycle/cold-cache accounting. -const Decoration = enum { plain, highlighted }; -const Lifecycle = enum { cold_document_page, warm_page, warm_document_pass }; - -const Scenario = struct { - id: []const u8, - lifecycle: Lifecycle, - decoration: Decoration, - tint: pdf.TintMode = .disabled, -}; - -// Fixed benchmark colors, independent of user config and host defaults. They -// are Pardes's helix page background/foreground and make run-to-run tint -// checksums a stable optimization target. -const tint_colors: pdf.TintColors = .{ - .background = .{ 0x08, 0x08, 0x08 }, - .foreground = .{ 0xbd, 0xbd, 0xbd }, -}; - -// Append only: baseline readers and the cross-scenario checks below rely on -// the original four scenarios retaining their indices and meaning. -const scenarios = [_]Scenario{ - .{ .id = "cold-document-plain", .lifecycle = .cold_document_page, .decoration = .plain }, - .{ .id = "warm-page-plain", .lifecycle = .warm_page, .decoration = .plain }, - .{ .id = "warm-page-highlighted", .lifecycle = .warm_page, .decoration = .highlighted }, - .{ .id = "warm-document-pass", .lifecycle = .warm_document_pass, .decoration = .plain }, - // The checked-in design page is grayscale. Reuse the original deterministic - // highlight decoration here so filtered/full exercise source chroma and - // must retain distinct correctness checksums. - .{ .id = "warm-page-filtered", .lifecycle = .warm_page, .decoration = .highlighted, .tint = .filtered }, - .{ .id = "warm-page-full", .lifecycle = .warm_page, .decoration = .highlighted, .tint = .full }, -}; - -const highlights = [_]pdf.Highlight{ - pdf.Highlight.init(.{ - .ul = .{ .x = 0.10, .y = 0.12 }, - .ur = .{ .x = 0.90, .y = 0.12 }, - .ll = .{ .x = 0.10, .y = 0.20 }, - .lr = .{ .x = 0.90, .y = 0.20 }, - }, .{ 0xff, 0xd5, 0x4f, 0x70 }, .search), - pdf.Highlight.init(.{ - .ul = .{ .x = 0.18, .y = 0.46 }, - .ur = .{ .x = 0.82, .y = 0.46 }, - .ll = .{ .x = 0.18, .y = 0.54 }, - .lr = .{ .x = 0.82, .y = 0.54 }, - }, .{ 0x55, 0xa7, 0xff, 0x78 }, .selection), -}; - -const Identity = struct { - pixels: u64 = 0, - rgba_bytes: u64 = 0, - checksum: u64 = checksum_seed, - - fn eql(a: Identity, b: Identity) bool { - return a.pixels == b.pixels and a.rgba_bytes == b.rgba_bytes and - a.checksum == b.checksum; - } -}; - -const Timed = struct { ns: u64, identity: Identity }; - -const Result = struct { - profile: usize, - scenario: usize, - identity: Identity, - min_ns: u64, - median_ns: u64, - p90_ns: u64, - max_ns: u64, - sampled_ns: u64, -}; - -pub fn main(init: std.process.Init) !void { - const args = try init.minimal.args.toSlice(init.arena.allocator()); - var config = parseArgs(args); - config.bytes = std.Io.Dir.cwd().readFileAlloc(init.io, config.path, gpa, .unlimited) catch |err| - fatal("cannot read {s}: {t}", .{ config.path, err }); - - var probe = pdf.Document.openBytes(config.bytes) catch |err| - fatal("cannot open {s}: {t}", .{ config.path, err }); - const document_pages = probe.pages; - probe.deinit(); - if (config.page >= document_pages) - fatal("--page {d} is outside the document's {d} pages", .{ config.page + 1, document_pages }); - const multi_pages = @min(config.multi_pages, document_pages); - - var results: [profiles.len * scenarios.len]Result = undefined; - for (profiles, 0..) |profile, pi| { - for (scenarios, 0..) |scenario, si| { - results[pi * scenarios.len + si] = try measure( - config, - pi, - profile, - si, - scenario, - multi_pages, - ); - } - checkProfileResults(results[pi * scenarios.len ..][0..scenarios.len], profile); - } - - if (config.json) - reportJson(init.io, config, document_pages, multi_pages, &results) - else - reportText(init.io, config, document_pages, multi_pages, &results); -} - -fn parseArgs(args: []const []const u8) Config { - var config: Config = .{}; - var i: usize = 1; - while (i < args.len) : (i += 1) { - const arg = args[i]; - if (std.mem.eql(u8, arg, "--json")) { - config.json = true; - } else if (std.mem.eql(u8, arg, "--reps")) { - i += 1; - if (i >= args.len) usage(); - config.reps = parsePositive("--reps", args[i], 1000); - } else if (std.mem.eql(u8, arg, "--warmup")) { - i += 1; - if (i >= args.len) usage(); - config.warmup = std.fmt.parseUnsigned(usize, args[i], 10) catch - fatal("--warmup expects an integer", .{}); - if (config.warmup > 100) fatal("--warmup must be at most 100", .{}); - } else if (std.mem.eql(u8, arg, "--path")) { - i += 1; - if (i >= args.len or args[i].len == 0) usage(); - config.path = args[i]; - } else if (std.mem.eql(u8, arg, "--page")) { - i += 1; - if (i >= args.len) usage(); - config.page = parsePositive("--page", args[i], std.math.maxInt(usize)) - 1; - } else if (std.mem.eql(u8, arg, "--pages")) { - i += 1; - if (i >= args.len) usage(); - config.multi_pages = parsePositive("--pages", args[i], 10_000); - } else if (std.mem.eql(u8, arg, "--help") or std.mem.eql(u8, arg, "-h")) { - printUsage(); - std.process.exit(0); - } else usage(); - } - return config; -} - -fn usage() noreturn { - printUsage(); - std.process.exit(2); -} - -fn printUsage() void { - std.debug.print( - "usage: pardes-pdf-bench [--json] [--reps N] [--warmup N] " ++ - "[--path FILE] [--page N] [--pages N]\n", - .{}, - ); -} - -fn measure( - config: Config, - profile_index: usize, - profile: Profile, - scenario_index: usize, - scenario: Scenario, - multi_pages: usize, -) !Result { - const samples = try gpa.alloc(u64, config.reps); - defer gpa.free(samples); - var sample_at: usize = 0; - var expected: ?Identity = null; - var sampled_ns: u64 = 0; - const rounds = config.warmup + config.reps; - - if (scenario.lifecycle == .cold_document_page) { - for (0..rounds) |round| { - const timed = try timeCold( - config.bytes, - config.page, - profile.request, - scenario.decoration, - scenario.tint, - ); - try verifyIdentity(&expected, timed.identity, profile, scenario, round); - if (round >= config.warmup) { - samples[sample_at] = timed.ns; - sample_at += 1; - sampled_ns +|= timed.ns; - } - } - } else { - var document = try pdf.Document.openBytes(config.bytes); - defer document.deinit(); - for (0..rounds) |round| { - const timed = switch (scenario.lifecycle) { - .warm_page => try timePage( - &document, - config.page, - profile.request, - scenario.decoration, - scenario.tint, - ), - .warm_document_pass => try timeDocumentPass( - &document, - multi_pages, - profile.request, - scenario.decoration, - scenario.tint, - ), - .cold_document_page => unreachable, - }; - try verifyIdentity(&expected, timed.identity, profile, scenario, round); - if (round >= config.warmup) { - samples[sample_at] = timed.ns; - sample_at += 1; - sampled_ns +|= timed.ns; - } - } - } - - std.debug.assert(sample_at == samples.len); - std.mem.sort(u64, samples, {}, std.sort.asc(u64)); - const p90_index = @min(samples.len - 1, (samples.len * 90 + 99) / 100 - 1); - return .{ - .profile = profile_index, - .scenario = scenario_index, - .identity = expected.?, - .min_ns = samples[0], - .median_ns = samples[samples.len / 2], - .p90_ns = samples[p90_index], - .max_ns = samples[samples.len - 1], - .sampled_ns = sampled_ns, - }; -} - -fn timeCold( - bytes: []const u8, - page: usize, - request: pdf.RenderRequest, - decoration: Decoration, - tint: pdf.TintMode, -) !Timed { - const started = nowNs(); - var document = try pdf.Document.openBytes(bytes); - errdefer document.deinit(); - const rendered = try renderPage(&document, page, request, decoration, tint); - const elapsed = @max(@as(u64, 1), nowNs() -| started); - const identity = try consumeRender(rendered, page, .{}); - document.deinit(); - return .{ .ns = elapsed, .identity = identity }; -} - -fn timePage( - document: *pdf.Document, - page: usize, - request: pdf.RenderRequest, - decoration: Decoration, - tint: pdf.TintMode, -) !Timed { - const started = nowNs(); - const rendered = try renderPage(document, page, request, decoration, tint); - const elapsed = @max(@as(u64, 1), nowNs() -| started); - return .{ - .ns = elapsed, - .identity = try consumeRender(rendered, page, .{}), - }; -} - -fn timeDocumentPass( - document: *pdf.Document, - page_count: usize, - request: pdf.RenderRequest, - decoration: Decoration, - tint: pdf.TintMode, -) !Timed { - const rendered = try gpa.alloc(pdf.Render, page_count); - defer gpa.free(rendered); - var elapsed: u64 = 0; - { - var initialized: usize = 0; - errdefer for (rendered[0..initialized]) |item| gpa.free(item.rgba); - const started = nowNs(); - for (rendered, 0..) |*item, page| { - item.* = try renderPage(document, page, request, decoration, tint); - initialized += 1; - } - elapsed = @max(@as(u64, 1), nowNs() -| started); - } - var identity: Identity = .{}; - for (rendered, 0..) |item, page| - identity = try consumeRender(item, page, identity); - return .{ .ns = elapsed, .identity = identity }; -} - -fn renderPage( - document: *pdf.Document, - page: usize, - request: pdf.RenderRequest, - decoration: Decoration, - tint: pdf.TintMode, -) !pdf.Render { - const rendered = switch (decoration) { - .plain => document.render(gpa, page, request, &.{}), - .highlighted => document.render(gpa, page, request, &highlights), - }; - const fresh = try rendered; - errdefer gpa.free(fresh.rgba); - // Keep the original scenarios instruction-for-instruction identical. - // Only the appended tint scenarios enter the linear pixel pass. - if (tint != .disabled) try pdf.tintRgba(fresh.rgba, tint, tint_colors); - return fresh; -} - -/// Hash every output byte after the timer stops. Reporting and cross-round -/// equality make the pixels observable, while keeping checksum cost out of the -/// renderer latency. Page/dimension mixing makes a multi-page pass ordered. -fn consumeRender(rendered: pdf.Render, page: usize, prior: Identity) !Identity { - defer gpa.free(rendered.rgba); - const pixels = std.math.mul(usize, rendered.width, rendered.height) catch - return error.BadRasterDimensions; - const expected_bytes = std.math.mul(usize, pixels, 4) catch - return error.BadRasterDimensions; - if (rendered.rgba.len != expected_bytes) return error.BadRasterLength; - var checksum = std.hash.Wyhash.hash( - prior.checksum ^ (@as(u64, page) *% 0x9e37_79b9_7f4a_7c15), - rendered.rgba, - ); - checksum ^= @as(u64, rendered.width) *% 0xbf58_476d_1ce4_e5b9; - checksum ^= @as(u64, rendered.height) *% 0x94d0_49bb_1331_11eb; - return .{ - .pixels = std.math.add(u64, prior.pixels, @intCast(pixels)) catch - return error.CounterOverflow, - .rgba_bytes = std.math.add(u64, prior.rgba_bytes, @intCast(expected_bytes)) catch - return error.CounterOverflow, - .checksum = checksum, - }; -} - -fn verifyIdentity( - expected: *?Identity, - got: Identity, - profile: Profile, - scenario: Scenario, - round: usize, -) !void { - if (expected.*) |want| { - if (!want.eql(got)) { - std.debug.print( - "pdf-bench: unstable pixels in {s}/{s} round {d}: " ++ - "expected {x:0>16}, got {x:0>16}\n", - .{ profile.id, scenario.id, round, want.checksum, got.checksum }, - ); - return error.UnstableRaster; - } - } else expected.* = got; -} - -fn checkProfileResults(results: []const Result, profile: Profile) void { - std.debug.assert(results.len == scenarios.len); - const cold = results[0].identity; - const warm = results[1].identity; - const marked = results[2].identity; - const filtered = results[4].identity; - const full = results[5].identity; - if (!cold.eql(warm)) - fatal("{s}: cold and warm plain renders produced different pixels", .{profile.id}); - if (warm.pixels != marked.pixels or warm.rgba_bytes != marked.rgba_bytes) - fatal("{s}: highlighted render changed raster dimensions", .{profile.id}); - if (warm.checksum == marked.checksum) - fatal("{s}: highlighted render did not change pixels", .{profile.id}); - if (marked.pixels != filtered.pixels or marked.rgba_bytes != filtered.rgba_bytes or - marked.pixels != full.pixels or marked.rgba_bytes != full.rgba_bytes) - fatal("{s}: tint changed raster dimensions", .{profile.id}); - if (marked.checksum == filtered.checksum or marked.checksum == full.checksum or - filtered.checksum == full.checksum) - fatal("{s}: tint did not change pixels", .{profile.id}); -} - -fn medianMpixPerSecond(result: Result) f64 { - return @as(f64, @floatFromInt(result.identity.pixels)) * 1000.0 / - @as(f64, @floatFromInt(result.median_ns)); -} - -fn medianGibPerSecond(result: Result) f64 { - return @as(f64, @floatFromInt(result.identity.rgba_bytes)) * 1_000_000_000.0 / - (@as(f64, @floatFromInt(result.median_ns)) * 1024.0 * 1024.0 * 1024.0); -} - -fn ms(ns: u64) f64 { - return @as(f64, @floatFromInt(ns)) / 1_000_000.0; -} - -fn mib(bytes: u64) f64 { - return @as(f64, @floatFromInt(bytes)) / (1024.0 * 1024.0); -} - -const report_max_bytes = 64 * 1024; - -fn reportText( - io: std.Io, - config: Config, - document_pages: usize, - multi_pages: usize, - results: []const Result, -) void { - var storage: [report_max_bytes]u8 = undefined; - var out: std.Io.Writer = .fixed(&storage); - out.print( - "pardes PDF render benchmark (ReleaseFast)\n" ++ - "path: {s} ({d} pages), selected page: {d}, multi-page pass: {d} pages\n" ++ - "warmup: {d}, sampled reps: {d}; cold = fresh Document (OS cache uncontrolled)\n" ++ - "tint colors: background=#{x:0>2}{x:0>2}{x:0>2}, foreground=#{x:0>2}{x:0>2}{x:0>2}\n\n", - .{ - config.path, - document_pages, - config.page + 1, - multi_pages, - config.warmup, - config.reps, - tint_colors.background[0], - tint_colors.background[1], - tint_colors.background[2], - tint_colors.foreground[0], - tint_colors.foreground[1], - tint_colors.foreground[2], - }, - ) catch return; - out.writeAll( - "profile scenario min ms median p90 max ms MPix/s GiB/s pixels/op RGBA MiB checksum\n" ++ - "-----------------------------------------------------------------------------------------------------------------------------------\n", - ) catch return; - for (results) |result| { - const profile = profiles[result.profile]; - const scenario = scenarios[result.scenario]; - out.print( - "{s:<20} {s:<26} {d:>8.2} {d:>8.2} {d:>8.2} {d:>8.2} {d:>7.1} {d:>6.2} {d:>10} {d:>9.2} {x:0>16}\n", - .{ - profile.id, - scenario.id, - ms(result.min_ns), - ms(result.median_ns), - ms(result.p90_ns), - ms(result.max_ns), - medianMpixPerSecond(result), - medianGibPerSecond(result), - result.identity.pixels, - mib(result.identity.rgba_bytes), - result.identity.checksum, - }, - ) catch return; - } - out.writeAll("\nrequests:\n") catch return; - for (profiles) |profile| { - out.print( - " {s}: dpi={d}, viewport-min={d}x{d}, max-dimension={d} — {s}\n", - .{ - profile.id, - profile.request.dpi, - profile.viewport_w, - profile.viewport_h, - profile.request.max_dimension, - profile.description, - }, - ) catch return; - } - std.Io.File.stdout().writeStreamingAll(io, out.buffered()) catch {}; -} - -fn reportJson( - io: std.Io, - config: Config, - document_pages: usize, - multi_pages: usize, - results: []const Result, -) void { - var storage: [report_max_bytes]u8 = undefined; - var out: std.Io.Writer = .fixed(&storage); - const path_json = std.json.Stringify.valueAlloc(gpa, config.path, .{}) catch return; - defer gpa.free(path_json); - out.print( - "{{\"benchmark\":\"pardes-pdf-render\",\"build\":\"ReleaseFast\"," ++ - "\"path\":{s},\"document_pages\":{d},\"selected_page\":{d}," ++ - "\"multi_pages\":{d},\"warmup\":{d},\"reps\":{d}," ++ - "\"tint_colors\":{{\"background\":\"{x:0>2}{x:0>2}{x:0>2}\"," ++ - "\"foreground\":\"{x:0>2}{x:0>2}{x:0>2}\"}},\"profiles\":[", - .{ - path_json, - document_pages, - config.page + 1, - multi_pages, - config.warmup, - config.reps, - tint_colors.background[0], - tint_colors.background[1], - tint_colors.background[2], - tint_colors.foreground[0], - tint_colors.foreground[1], - tint_colors.foreground[2], - }, - ) catch return; - for (profiles, 0..) |profile, i| { - out.print( - "{s}{{\"id\":\"{s}\",\"dpi\":{d},\"minimum_width\":{d}," ++ - "\"minimum_height\":{d},\"max_dimension\":{d}}}", - .{ - if (i == 0) "" else ",", - profile.id, - profile.request.dpi, - profile.request.minimum_width, - profile.request.minimum_height, - profile.request.max_dimension, - }, - ) catch return; - } - out.writeAll("],\"results\":[") catch return; - for (results, 0..) |result, i| { - out.print( - "{s}{{\"profile\":\"{s}\",\"scenario\":\"{s}\"," ++ - "\"min_ns\":{d},\"median_ns\":{d},\"p90_ns\":{d}," ++ - "\"max_ns\":{d},\"sampled_ns\":{d},\"pixels\":{d}," ++ - "\"rgba_bytes\":{d},\"median_mpix_s\":{d:.3}," ++ - "\"median_gib_s\":{d:.3},\"checksum\":\"{x:0>16}\"}}", - .{ - if (i == 0) "" else ",", - profiles[result.profile].id, - scenarios[result.scenario].id, - result.min_ns, - result.median_ns, - result.p90_ns, - result.max_ns, - result.sampled_ns, - result.identity.pixels, - result.identity.rgba_bytes, - medianMpixPerSecond(result), - medianGibPerSecond(result), - result.identity.checksum, - }, - ) catch return; - } - out.writeAll("]}\n") catch return; - std.Io.File.stdout().writeStreamingAll(io, out.buffered()) catch {}; -} - -fn parsePositive(name: []const u8, text: []const u8, maximum: usize) usize { - return bench_util.parsePositive(fatal, name, text, maximum); -} 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; -} 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); -} diff --git a/test/pdf_sections_bench.zig b/test/pdf_sections_bench.zig deleted file mode 100644 index 30b89e09..00000000 --- a/test/pdf_sections_bench.zig +++ /dev/null @@ -1,517 +0,0 @@ -//! ReleaseFast scoreboard for the PDF outline -> +PdfSections path. -//! -//! zig build pdf-sections-bench -Doptimize=ReleaseFast -//! zig build pdf-sections-bench -Doptimize=ReleaseFast -- --json -//! zig build pdf-sections-bench -Doptimize=ReleaseFast -- --warmup 5 --reps 31 -//! -//! Setup, destruction, semantic checksums, and reporting are outside each -//! timer. The two interactive scenarios batch tiny real-core operations and -//! report nanoseconds per operation; `operations_per_sample` makes that -//! normalization explicit in both output formats. -const std = @import("std"); -const bench_util = @import("bench_util.zig"); -const nowNs = bench_util.nowNs; -const fatal = bench_util.Fatal("pdf-sections-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_sections_bench_config"); - -pub const std_options: std.Options = .{ .log_level = .err }; - -const gpa = std.heap.smp_allocator; -const cached_batch = 128; -const step_batch = 256; -const near_limit_items = pdf.max_outline_items; -const near_limit_depth = 64; - -const Config = struct { - json: bool = false, - warmup: usize = 5, - reps: usize = 31, -}; - -const Identity = struct { - items: u64, - bytes: u64, - checksum: u64, - - fn eql(a: Identity, b: Identity) bool { - return a.items == b.items and a.bytes == b.bytes and - a.checksum == b.checksum; - } -}; - -const Sample = struct { - ns: u64, - identity: Identity, -}; - -const Result = struct { - name: []const u8, - operations_per_sample: usize, - min_ns: u64, - median_ns: u64, - p90_ns: u64, - max_ns: u64, - identity: Identity, -}; - -pub fn main(init: std.process.Init) !void { - if (!bench_config.release_fast_core) - fatal("the production core must be ReleaseFast; add -Doptimize=ReleaseFast", .{}); - const args = try init.minimal.args.toSlice(init.arena.allocator()); - const config = parseArgs(args); - - const fixture = @embedFile("pdf-fixtures/outline.pdf"); - var path_buf: [256]u8 = undefined; - const path = try std.fmt.bufPrint(&path_buf, "/tmp/pardes-pdf-sections-bench-{d}.pdf", .{libc.getpid()}); - try writeFile(path, fixture); - defer unlink(path); - - var document = try pdf.Document.openBytes(fixture); - defer document.deinit(); - var outline = try document.outline(gpa); - defer outline.deinit(gpa); - - const synthetic = try makeWorstShape(); - defer gpa.free(synthetic); - - const results = [_]Result{ - try measureOutline(fixture, config), - try measureMaterialize(path, outline.entries, config), - try measureCachedControl(path, config), - try measureCachedReopen(path, config), - try measureOrdinalStep(path, config), - try measureWorstShape(synthetic, config), - }; - - if (config.json) - reportJson(init.io, config, &results) - else - reportText(config, &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")) { - 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 fatal("usage: pardes-pdf-sections-bench [--json] [--warmup N] [--reps N]", .{}); - } - return config; -} - -fn measureOutline(fixture: []const u8, config: Config) !Result { - const samples = try gpa.alloc(u64, config.reps); - defer gpa.free(samples); - var expected: ?Identity = null; - for (0..config.warmup + config.reps) |round| { - // Document open/close is setup: this row isolates MuPDF outline load, - // bridge flattening, and the Zig-owned two-allocation copy. - var document = try pdf.Document.openBytes(fixture); - defer document.deinit(); - const started = nowNs(); - var outline = try document.outline(gpa); - const elapsed = nowNs() -| started; - const identity = outlineIdentity(outline.entries); - outline.deinit(gpa); - try verifyIdentity("outline-load-flatten-real", &expected, identity, round); - if (round >= config.warmup) samples[round - config.warmup] = @max(1, elapsed); - } - return summarize("outline-load-flatten-real", 1, samples, expected.?); -} - -fn measureMaterialize( - path: []const u8, - entries: []const pdf.OutlineEntry, - config: Config, -) !Result { - const samples = try gpa.alloc(u64, config.reps); - defer gpa.free(samples); - var expected: ?Identity = null; - for (0..config.warmup + config.reps) |round| { - const started = nowNs(); - const content = try pardes.panes.Pdf.SectionRows.render(gpa, path, entries); - const elapsed = nowNs() -| started; - const identity = contentIdentity(content); - gpa.free(content); - try verifyIdentity("sections-first-materialize", &expected, identity, round); - if (round >= config.warmup) samples[round - config.warmup] = @max(1, elapsed); - } - return summarize("sections-first-materialize", 1, samples, expected.?); -} - -fn measureCachedReopen(path: []const u8, config: Config) !Result { - const core = try preparedCore(path); - defer core.deinit(); - const prepared = preparedReopen(core); - const samples = try gpa.alloc(u64, config.reps); - defer gpa.free(samples); - var expected: ?Identity = null; - for (0..config.warmup + config.reps) |round| { - var observed: u64 = 0; - const started = nowNs(); - for (0..cached_batch) |operation| { - perturbReopen(prepared, operation); - // Without the barriers ReleaseFast can observe that every call - // leaves the same final fields and coalesce the whole batch into - // one re-arm. The empty asm emits no instruction; its portable - // memory clobber makes each production call's reads and writes - // independently observable to the optimizer. - compilerBarrier(); - pardes.panes.Pdf.openSections(core, 0); - compilerBarrier(); - observed +%= observeReopen(prepared); - } - const elapsed = nowNs() -| started; - var identity = sectionsOutputIdentity(core); - identity.checksum = mix(identity.checksum, observed); - try verifyIdentity("sections-clean-cached-reopen", &expected, identity, round); - if (round >= config.warmup) - samples[round - config.warmup] = @max(1, elapsed / cached_batch); - } - return summarize("sections-clean-cached-reopen", cached_batch, samples, expected.?); -} - -/// Cost floor for the anti-elision workload above. This performs the same -/// varying state writes, barriers, and observation but deliberately omits the -/// core call. Rearming once after the timer keeps fixture state canonical. -fn measureCachedControl(path: []const u8, config: Config) !Result { - const core = try preparedCore(path); - defer core.deinit(); - const prepared = preparedReopen(core); - const samples = try gpa.alloc(u64, config.reps); - defer gpa.free(samples); - var expected: ?Identity = null; - for (0..config.warmup + config.reps) |round| { - var observed: u64 = 0; - const started = nowNs(); - for (0..cached_batch) |operation| { - perturbReopen(prepared, operation); - compilerBarrier(); - compilerBarrier(); - observed +%= observeReopen(prepared); - } - const elapsed = nowNs() -| started; - pardes.panes.Pdf.openSections(core, 0); - var identity = sectionsOutputIdentity(core); - identity.checksum = mix(identity.checksum, observed); - try verifyIdentity("sections-perturb-observe-control", &expected, identity, round); - if (round >= config.warmup) - samples[round - config.warmup] = @max(1, elapsed / cached_batch); - } - return summarize("sections-perturb-observe-control", cached_batch, samples, expected.?); -} - -fn measureOrdinalStep(path: []const u8, config: Config) !Result { - const core = try preparedCore(path); - defer core.deinit(); - const owner = core.panes[0].?; - const samples = try gpa.alloc(u64, config.reps); - defer gpa.free(samples); - var expected: ?Identity = null; - for (0..config.warmup + config.reps) |round| { - owner.search_row = null; - core.active = 0; - // Untimed first step parks on row zero. Timed n/N then alternates the - // root and child, which share a page but carry exact y placement. - core.update(.{ .key = .{ .cp = 'n', .text = "n" } }); - const started = nowNs(); - for (0..step_batch) |step| { - compilerBarrier(); - core.update(.{ .key = .{ - .cp = if (step & 1 == 0) 'n' else 'N', - .text = if (step & 1 == 0) "n" else "N", - } }); - compilerBarrier(); - } - const elapsed = nowNs() -| started; - drainEffects(core); - const identity = stepIdentity(core); - try verifyIdentity("sections-ordinal-destination-step", &expected, identity, round); - if (round >= config.warmup) - samples[round - config.warmup] = @max(1, elapsed / step_batch); - } - return summarize("sections-ordinal-destination-step", step_batch, samples, expected.?); -} - -fn measureWorstShape(entries: []const pdf.OutlineEntry, config: Config) !Result { - const samples = try gpa.alloc(u64, config.reps); - defer gpa.free(samples); - var expected: ?Identity = null; - for (0..config.warmup + config.reps) |round| { - const started = nowNs(); - const content = try pardes.panes.Pdf.SectionRows.render(gpa, "/tmp/near-limit.pdf", entries); - const elapsed = nowNs() -| started; - const identity = contentIdentity(content); - gpa.free(content); - try verifyIdentity("rows-near-limit-worst-shape", &expected, identity, round); - if (round >= config.warmup) samples[round - config.warmup] = @max(1, elapsed); - } - return summarize("rows-near-limit-worst-shape", 1, samples, expected.?); -} - -fn preparedCore(path: []const u8) !*pardes.Pardes { - const core = try pardes.Pardes.init(gpa, .{ .file = path, .cols = 80, .rows = 28 }); - errdefer core.deinit(); - drainEffects(core); - pardes.panes.Pdf.openSections(core, 0); - drainEffects(core); - _ = sectionsOutputIdentity(core); - return core; -} - -const PreparedReopen = struct { - core: *pardes.Pardes, - owner: *pardes.Pane, - output: *pardes.Pane, - output_id: usize, -}; - -fn preparedReopen(core: *pardes.Pardes) PreparedReopen { - const owner = core.panes[0] orelse fatal("PDF pane disappeared", .{}); - const output_id = owner.search_pane orelse fatal("+PdfSections was not armed", .{}); - return .{ - .core = core, - .owner = owner, - .output = core.panes[output_id] orelse fatal("+PdfSections pane disappeared", .{}), - .output_id = output_id, - }; -} - -/// The output bytes and generated revision stay clean. Only transient UI -/// state varies, so every invocation has observable reset work while still -/// exercising the production clean-output cache guard. -fn perturbReopen(prepared: PreparedReopen, operation: usize) void { - const marker: i32 = @intCast(operation % 31 + 1); - const output_file = &prepared.output.file.?; - output_file.scroll = @intCast(operation % 17 + 1); - prepared.output.body.cur_row = marker; - prepared.output.body.cur_col = marker + 1; - prepared.output.body.vsel.active = true; - prepared.output.body.nsel = 1; - prepared.owner.search_pane = null; - prepared.owner.search_row = operation % 23; - prepared.core.active = prepared.output_id; -} - -fn observeReopen(prepared: PreparedReopen) u64 { - const file = prepared.output.file.?; - var observed: u64 = file.scroll; - observed = observed *% 131 +% @as(u64, @intCast(@max(0, prepared.output.body.cur_row))); - observed = observed *% 131 +% @as(u64, @intCast(@max(0, prepared.output.body.cur_col))); - observed = observed *% 131 +% @intFromBool(prepared.output.body.vsel.active); - observed = observed *% 131 +% prepared.output.body.nsel; - observed = observed *% 131 +% (prepared.owner.search_pane orelse std.math.maxInt(usize)); - observed = observed *% 131 +% (prepared.owner.search_row orelse std.math.maxInt(usize)); - observed = observed *% 131 +% prepared.core.active; - return observed; -} - -fn sectionsOutputIdentity(core: *pardes.Pardes) Identity { - const owner = core.panes[0] orelse fatal("PDF pane disappeared", .{}); - const output_id = owner.search_pane orelse fatal("+PdfSections was not armed", .{}); - const output = core.panes[output_id] orelse fatal("+PdfSections pane disappeared", .{}); - const file = output.file orelse fatal("+PdfSections has no file", .{}); - var identity = contentIdentity(file.content); - identity.checksum = mix(identity.checksum, output.serial); - identity.checksum = mix(identity.checksum, file.revision); - identity.checksum = mix(identity.checksum, output_id); - return identity; -} - -fn stepIdentity(core: *pardes.Pardes) Identity { - var identity = sectionsOutputIdentity(core); - const owner = core.panes[0].?; - const view = owner.pdf.?; - identity.checksum = mix(identity.checksum, view.page); - identity.checksum = mix(identity.checksum, @as(u64, @bitCast(view.document_scroll_y))); - identity.checksum = mix(identity.checksum, owner.search_row orelse std.math.maxInt(usize)); - identity.checksum = mix(identity.checksum, view.next_raster_revision); - return identity; -} - -fn contentIdentity(content: []const u8) Identity { - return .{ - .items = std.mem.count(u8, content, "\n"), - .bytes = content.len, - .checksum = std.hash.Wyhash.hash(0, content), - }; -} - -fn outlineIdentity(entries: []const pdf.OutlineEntry) Identity { - var checksum: u64 = 0; - var bytes: u64 = 0; - for (entries) |entry| { - checksum = mix(checksum, entry.depth); - if (entry.title) |title| { - bytes += title.len; - checksum = std.hash.Wyhash.hash(checksum, title); - } else checksum = mix(checksum, std.math.maxInt(u64)); - switch (entry.destination) { - .none => checksum = mix(checksum, 0), - .internal => |destination| { - checksum = mix(checksum, 1); - checksum = mix(checksum, destination.page); - checksum = mix(checksum, if (destination.x) |x| @as(u32, @bitCast(x)) else std.math.maxInt(u32)); - checksum = mix(checksum, if (destination.y) |y| @as(u32, @bitCast(y)) else std.math.maxInt(u32)); - }, - .external => |uri| { - bytes += uri.len; - checksum = mix(checksum, 2); - checksum = std.hash.Wyhash.hash(checksum, uri); - }, - } - } - return .{ .items = entries.len, .bytes = bytes, .checksum = checksum }; -} - -fn makeWorstShape() ![]pdf.OutlineEntry { - const entries = try gpa.alloc(pdf.OutlineEntry, near_limit_items); - for (entries, 0..) |*entry, ordinal| { - const depth: u8 = @intCast(ordinal % near_limit_depth); - entry.* = .{ - .depth = depth, - .title = "Worst shape outline title", - .destination = if (depth + 1 == near_limit_depth) - .{ .internal = .{ - .page = ordinal / near_limit_depth, - .x = 11.25, - .y = 47.5, - } } - else - .none, - }; - } - return entries; -} - -fn verifyIdentity( - name: []const u8, - expected: *?Identity, - got: Identity, - round: usize, -) !void { - if (expected.*) |want| { - if (!want.eql(got)) { - std.debug.print("pdf-sections-bench: unstable identity in {s} round {d}: expected {x:0>16}, got {x:0>16}\n", .{ - name, round, want.checksum, got.checksum, - }); - return error.UnstableIdentity; - } - } else expected.* = got; -} - -fn summarize( - name: []const u8, - operations_per_sample: usize, - samples: []u64, - identity: Identity, -) Result { - std.debug.assert(samples.len != 0); - std.mem.sort(u64, samples, {}, std.sort.asc(u64)); - return .{ - .name = name, - .operations_per_sample = operations_per_sample, - .min_ns = samples[0], - .median_ns = samples[samples.len / 2], - .p90_ns = samples[(samples.len * 9) / 10 -| 1], - .max_ns = samples[samples.len - 1], - .identity = identity, - }; -} - -/// A host benchmark compiler barrier. MuPDF benchmarks are build-and-run -/// steps, so they run on a native backend only; an empty volatile -/// asm with only the abstract memory clobber is valid on every native backend -/// Zig can run here and costs no machine instruction. -inline fn compilerBarrier() void { - asm volatile ("" ::: .{ .memory = true }); -} - -fn reportText(config: Config, results: []const Result) void { - std.debug.print("pardes PDF sections benchmark (ReleaseFast)\n", .{}); - std.debug.print("warmup: {d}, sampled reps: {d}; medians are ns/op\n\n", .{ config.warmup, config.reps }); - std.debug.print("{s:<42} {s:>10} {s:>10} {s:>10} {s:>10} {s:>8} {s:>8} {s}\n", .{ - "scenario", "min", "median", "p90", "max", "items", "bytes", "checksum", - }); - std.debug.print("{s}\n", .{&@as([124]u8, @splat('-'))}); - for (results) |result| std.debug.print( - "{s:<42} {d:>10} {d:>10} {d:>10} {d:>10} {d:>8} {d:>8} {x:0>16}\n", - .{ - result.name, - result.min_ns, - result.median_ns, - result.p90_ns, - result.max_ns, - result.identity.items, - result.identity.bytes, - result.identity.checksum, - }, - ); - std.debug.print("\nBatched rows divide elapsed time by operations/sample: cached={d}, step={d}.\n", .{ cached_batch, step_batch }); -} - -const json_report_max_bytes = 16 * 1024; - -fn reportJson(io: std.Io, config: Config, results: []const Result) void { - var storage: [json_report_max_bytes]u8 = undefined; - var out: std.Io.Writer = .fixed(&storage); - out.print("{{\"benchmark\":\"pardes-pdf-sections\",\"build\":\"ReleaseFast\",\"warmup\":{d},\"reps\":{d},\"results\":[", .{ - config.warmup, config.reps, - }) catch return; - for (results, 0..) |result, i| out.print( - "{s}{{\"scenario\":\"{s}\",\"operations_per_sample\":{d},\"min_ns\":{d},\"median_ns\":{d},\"p90_ns\":{d},\"max_ns\":{d},\"items\":{d},\"bytes\":{d},\"checksum\":\"{x:0>16}\"}}", - .{ - if (i == 0) "" else ",", - result.name, - result.operations_per_sample, - result.min_ns, - result.median_ns, - result.p90_ns, - result.max_ns, - result.identity.items, - result.identity.bytes, - result.identity.checksum, - }, - ) catch return; - out.writeAll("]}\n") catch return; - std.Io.File.stdout().writeStreamingAll(io, out.buffered()) catch {}; -} - -fn writeFile(path: []const u8, bytes: []const u8) !void { - var path_buf: [4096]u8 = undefined; - const path_z = try std.fmt.bufPrintSentinel(&path_buf, "{s}", .{path}, 0); - const fd = libc.open(path_z, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true }, @as(libc.mode_t, 0o600)); - if (fd < 0) return error.OpenFailed; - defer _ = libc.close(fd); - var offset: usize = 0; - while (offset < bytes.len) { - const written = libc.write(fd, bytes.ptr + offset, bytes.len - offset); - if (written < 0) { - if (libc.errno(written) == .INTR) continue; - return error.WriteFailed; - } - offset += @intCast(written); - } -} - -fn unlink(path: []const u8) void { - var path_buf: [4096]u8 = undefined; - const path_z = std.fmt.bufPrintSentinel(&path_buf, "{s}", .{path}, 0) catch return; - _ = libc.unlink(path_z); -} - -fn parseCount(flag: []const u8, text: []const u8) usize { - return bench_util.parsePositive(fatal, flag, text, 10_000); -} diff --git a/test/rollback_bench.zig b/test/rollback_bench.zig index 1260f47e..d734f5cf 100644 --- a/test/rollback_bench.zig +++ b/test/rollback_bench.zig @@ -68,7 +68,7 @@ fn writeFile(path: []const u8, lines: usize) !void { } fn build(s: Session, dir: []const u8, a: std.mem.Allocator) !*pardes.Pardes { - const core = try pardes.Pardes.init(a, .{ .tty_only = true, .image_allocator = a, .pdf_allocator = a, .tree_sitter_allocator = a, .frame_allocator = a }); + const core = try pardes.Pardes.init(a, .{ .tty_only = true, .image_allocator = a, .tree_sitter_allocator = a, .frame_allocator = a }); core.update(.{ .resize = .{ .cols = 200, .rows = 60 } }); pump(core); // The shell: pane 0 of a tty boot, its output fed as a pty's. @@ -203,7 +203,7 @@ pub fn main(init: std.process.Init) !void { const bytes = try pardes.dump.dumpBytes(core); const t1 = nowNs(); dump_len = bytes.len; - const loaded = try pardes.dump.load(gpa, .{ .tty_only = true, .image_allocator = gpa, .pdf_allocator = gpa, .tree_sitter_allocator = gpa, .frame_allocator = gpa }, bytes, null); + const loaded = try pardes.dump.load(gpa, .{ .tty_only = true, .image_allocator = gpa, .tree_sitter_allocator = gpa, .frame_allocator = gpa }, bytes, null); y.* = nowNs() - t1; x.* = t1 - t0; loaded.deinit(); -- cgit v1.3