Bench: example plug-in Invert vs built-in Invert on 24 MP

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Brandon ThomasandClaude Opus 5.5 committed 2026-10-04 16:08:20 -04:00
1 parent 708e8f6cba
commit f5bd5b0aea
2 files changed
+54 -3

No files matched your search

@@ -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::<u64>().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<f32> = (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");
}
}
+5 -3
View File
@@ -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