From f5bd5b0aeac792f4a95307b4c1942a2da5a7f8f8 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Sun, 4 Oct 2026 15:26:16 -0400 Subject: [PATCH] Bench: example plug-in Invert vs built-in Invert on 24 MP Co-Authored-By: Claude Opus 5.5 --- crates/engine/examples/bench_plugin_invert.rs | 49 +++++++++++++++++++ docs/plugins.md | 8 +-- 2 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 crates/engine/examples/bench_plugin_invert.rs diff --git a/crates/engine/examples/bench_plugin_invert.rs b/crates/engine/examples/bench_plugin_invert.rs new file mode 100644 index 0000000000..e654801bf1 --- /dev/null +++ b/crates/engine/examples/bench_plugin_invert.rs @@ -0,0 +1,49 @@ +//! Timing: the example WebAssembly Invert plug-in vs the built-in Image › Adjustments › Invert, +//! through the full command path, on a 6000 × 4000 (24 MP) layer. +//! `cargo run --release -p photocraft-engine --example bench_plugin_invert [width] [height] [depth]` +use std::time::Instant; + +use photocraft_engine::Session; +use photocraft_geom::Rect; +use serde_json::json; + +fn main() { + let mut args = std::env::args().skip(1).map(|v| v.parse::().unwrap_or(0)); + let w = args.next().filter(|v| *v > 0).unwrap_or(6000); + let h = args.next().filter(|v| *v > 0).unwrap_or(4000); + let depth = args.next().filter(|v| *v > 0).unwrap_or(8); + let mut s = Session::new(); + s.execute("file.new", json!({"width": w, "height": h, "depth": depth})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + s.edit("texture", |doc, active| { + let surf = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap(); + let fmt = surf.format(); + let row: Vec = (0..w).flat_map(|x| photocraft_raster::from_rgba(&fmt, [(x % 256) as f32 / 255.0, 0.5, 0.25, 1.0])).collect(); + for y in 0..h as i32 { + surf.write_region(Rect::new(0, y, w as i32, y + 1), &row); + } + Ok(()) + }) + .unwrap(); + let wasm = include_bytes!("../../plugins/tests/fixtures/invert.wasm"); + let data: String = { + const T: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + wasm.chunks(3) + .flat_map(|c| { + let n = (u32::from(c[0]) << 16) | (u32::from(*c.get(1).unwrap_or(&0)) << 8) | u32::from(*c.get(2).unwrap_or(&0)); + (0..4).map(move |i| if i <= c.len() { T[(n >> (18 - 6 * i)) as usize & 63] as char } else { '=' }) + }) + .collect() + }; + s.execute("plugin.install", json!({"data": data})).unwrap(); + println!("{w}x{h} ({:.1} MP), {depth}-bit RGBA layer", (w * h) as f64 / 1e6); + for _ in 0..3 { + let t = Instant::now(); + s.execute("image.adjustments.invert", json!({})).unwrap(); + let builtin = t.elapsed().as_secs_f64() * 1e3; + let t = Instant::now(); + s.execute("plugin.run", json!({"id": "org.photocraft.example.invert"})).unwrap(); + let plugin = t.elapsed().as_secs_f64() * 1e3; + println!("built-in invert {builtin:>7.0} ms plug-in invert {plugin:>7.0} ms"); + } +} diff --git a/docs/plugins.md b/docs/plugins.md index 39ccd56443..bc75ff1ed3 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -157,9 +157,11 @@ WASI. ## Performance The interpreter costs roughly 10–100× native speed per instruction, so plug-ins suit per-pixel and -small-neighbourhood filters. Example Invert on a 6000 × 4000 RGBA 8-bit layer (24 MP), release build, -14-core Apple Silicon: see the numbers in the PR / dev log (`cargo run -p photocraft-plugins ---release --example bench_invert`). Peak extra memory is about three bands per worker thread (input, +small-neighbourhood filters. Because bands run in parallel, the example Invert on a 6000 × 4000 +RGBA 8-bit layer (24 MP, release, 14-core Apple Silicon under heavy load) took 1.1–1.6 s through +`plugin.run`, versus 3.6–5.1 s for the (single-threaded) built-in Image › Adjustments › Invert +(`cargo run --release -p photocraft-engine --example bench_plugin_invert`; plug-in alone: +`cargo run --release -p photocraft-plugins --example bench_invert`). Peak extra memory is about three bands per worker thread (input, original for the selection blend, and the instance's memory), i.e. ~12 MiB per thread. ## Not in v1