Skip to content

Unlock custom brushes, natural fill effects and intuitive hatching in p5.js

Add movepos_ms to benchmark (Position.movePos hot path). New baseline with 7 metrics: total_ms ~28ms

acamposuribed9f0b61dd2e8
diff --git a/autoresearch.jsonl b/autoresearch.jsonl
index 8c0425b..551e6cf 100644
--- a/autoresearch.jsonl
+++ b/autoresearch.jsonl
@@ -4,3 +4,5 @@
{"run":3,"commit":"2772809","metric":24.827,"metrics":{"trig_ms":6.229,"gauss_ms":2.958,"plot_ms":5.54,"hatch_ms":4.684,"intersect_ms":3.347,"intersect_lines_ms":2.068},"status":"keep","description":"Pre-warm trig lookup table at module load. Eliminates isNaN check + lazy store overhead. total: 25.3→24.8ms (-2%)","timestamp":1774084070274,"segment":0,"confidence":10.79843444227007}
{"run":4,"commit":"7c00241","metric":23.735,"metrics":{"trig_ms":5.151,"gauss_ms":2.849,"plot_ms":5.62,"hatch_ms":4.709,"intersect_ms":3.17,"intersect_lines_ms":2.236},"status":"keep","description":"angleToIdx: avoid % modulo for common range [-360,720). trig_ms: 6.2→5.2ms (-17%). total: 24.8→23.7ms (-4.5%)","timestamp":1774084169302,"segment":0,"confidence":8.247036805988744}
{"run":5,"commit":"56cb43e","metric":23.233,"metrics":{"trig_ms":5.125,"gauss_ms":2.871,"plot_ms":5.663,"hatch_ms":3.413,"intersect_ms":3.64,"intersect_lines_ms":2.521},"status":"keep","description":"scanlineHatch: flat typed array buffers (no 200 small allocs), skip redundant trig, skip gradient mult when not needed. hatch_ms: 4.7→3.4ms (-27%). total: 23.7→23.2ms (-2%)","timestamp":1774084241237,"segment":0,"confidence":6.512820512820498}
+{"run":6,"commit":"949618d","metric":22.768,"metrics":{"trig_ms":5.277,"gauss_ms":2.972,"plot_ms":5.535,"hatch_ms":3.448,"intersect_ms":3.424,"intersect_lines_ms":2.112},"status":"keep","description":"Inline pressure() and angle() in Plot: remove curving() overhead, skip wrap-around check for pressure. plot_ms: 5.7→5.5ms. total: 23.2→22.8ms (-2%)","timestamp":1774084318038,"segment":0,"confidence":7.199049881235151}
+{"run":7,"commit":"949618d","metric":28.556,"metrics":{"trig_ms":5.727,"gauss_ms":3.855,"plot_ms":5.109,"hatch_ms":4.348,"intersect_ms":4.285,"intersect_lines_ms":5.233},"status":"discard","description":"Sequential fast path in calcIndex causes JIT deoptimization of unrelated benchmarks. intersect_lines_ms 2x worse, gauss 30% worse. Revert.","timestamp":1774084395778,"segment":0,"confidence":4.753450439146796}
diff --git a/benchmark/bench.mjs b/benchmark/bench.mjs
index 70a7f7a..6b3d419 100644
--- a/benchmark/bench.mjs
+++ b/benchmark/bench.mjs
@@ -9,17 +9,21 @@
*/
import { performance } from "perf_hooks";
-import { setTargetState } from "../src/core/target.js";
+import { setTargetState, setTargetRuntime } from "../src/core/target.js";
import {
cos, sin, gaussian, map, rr, randInt, constrain, dist, rotate, intersectLines, seed,
} from "../src/core/utils.js";
import { Plot } from "../src/core/plot.js";
import { Polygon } from "../src/core/polygon.js";
import { hatch, getHatchLines } from "../src/hatch/hatch.js";
+import { isFieldReady, Position, noField } from "../src/core/flowfield.js";
// Setup minimal environment
-setTargetState({ Cwidth: 800, Cheight: 600, Density: 1 });
+setTargetState({ Cwidth: 800, Cheight: 600, Density: 1, Renderer: { loaded: true } });
+setTargetRuntime({ isCanvasReady: () => {}, syncDensity: () => 1 });
seed(42);
+isFieldReady();
+noField();
// ============================================================
// BENCHMARK RUNNER
@@ -121,7 +125,31 @@ function benchIntersect() {
const intersectMs = medianOf(Array.from({ length: RUNS }, () => bench(benchIntersect, 500)));
// ============================================================
-// 6. INTERSECT LINES: raw intersectLines() from utils.js
+// 6. POSITION.movePos() — core stroke rendering loop
+// ============================================================
+const movePlot = new Plot("curve");
+for (let i = 0; i < 10; i++) movePlot.addSegment(i * 36, 100, 0.8);
+movePlot.endPlot(0, 1.0);
+
+function benchMovePos() {
+ // Simulate a brush stroke: moveTo (direction-based) + plotTo (plot-based)
+ let total = 0;
+ for (let i = 0; i < 5; i++) {
+ const pos = new Position(400, 300);
+ pos.moveTo(45 + i * 30, 200, 1);
+ total += pos.x;
+ }
+ for (let i = 0; i < 5; i++) {
+ const pos = new Position(400, 300);
+ pos.plotTo(movePlot, 500, 1, 1);
+ total += pos.x;
+ }
+ return total;
+}
+const movePosMs = medianOf(Array.from({ length: RUNS }, () => bench(benchMovePos, 200)));
+
+// ============================================================
+// 7. INTERSECT LINES: raw intersectLines() from utils.js
// ============================================================
function benchIntersectLines() {
let count = 0;
@@ -138,7 +166,7 @@ const intersectLinesMs = medianOf(Array.from({ length: RUNS }, () => bench(bench
// ============================================================
// TOTAL (primary metric)
// ============================================================
-const total_ms = trigMs + gaussMs + plotMs + hatchMs + intersectMs + intersectLinesMs;
+const total_ms = trigMs + gaussMs + plotMs + hatchMs + intersectMs + intersectLinesMs + movePosMs;
console.log(`METRIC total_ms=${total_ms.toFixed(3)}`);
console.log(`METRIC trig_ms=${trigMs.toFixed(3)}`);
@@ -147,3 +175,4 @@ console.log(`METRIC plot_ms=${plotMs.toFixed(3)}`);
console.log(`METRIC hatch_ms=${hatchMs.toFixed(3)}`);
console.log(`METRIC intersect_ms=${intersectMs.toFixed(3)}`);
console.log(`METRIC intersect_lines_ms=${intersectLinesMs.toFixed(3)}`);
+console.log(`METRIC movepos_ms=${movePosMs.toFixed(3)}`);
diff --git a/benchmark/loader.mjs b/benchmark/loader.mjs
index 8965cb0..1bdf201 100644
--- a/benchmark/loader.mjs
+++ b/benchmark/loader.mjs
@@ -1,10 +1,36 @@
/**
- * Custom ESM loader that stubs .vert and .frag GLSL files so that
- * source modules can be imported in Node.js without a browser/WebGL context.
+ * Custom ESM loader that stubs GLSL shader files and browser-only modules
+ * so that source modules can be imported in Node.js without a browser/WebGL context.
*/
export async function load(url, context, nextLoad) {
+ // Stub GLSL shader files
if (url.endsWith('.vert') || url.endsWith('.frag')) {
return { format: 'module', source: 'export default "";', shortCircuit: true };
}
+ // Stub compositor_runtime — provides GL compositor hooks not needed for pure computation
+ if (url.includes('compositor_runtime') || url.includes('renderer_runtime')) {
+ return {
+ format: 'module',
+ source: `
+ export const setCompositorRuntime = () => {};
+ export const setRendererRuntime = () => {};
+ export const clearTarget = () => {};
+ export const ensureBlendShaderProgram = () => {};
+ export const ensureBlendSourceFramebuffer = () => {};
+ export const createFramebuffer = () => null;
+ export const runBlendShaderPass = () => {};
+ export const blitSourceToFramebuffer = () => {};
+ export const blendToTarget = () => {};
+ export const composite = () => {};
+ export const create2DCanvas = () => null;
+ export const get2DContext = () => null;
+ export const blitDefaultFramebufferSource = () => {};
+ export const beginDirectMaskDraw = () => null;
+ export const endDirectMaskDraw = () => {};
+ export const resetDirectShaderTracking = () => {};
+ `,
+ shortCircuit: true,
+ };
+ }
return nextLoad(url, context);
}
diff --git a/benchmark/test_flowfield.mjs b/benchmark/test_flowfield.mjs
new file mode 100644
index 0000000..6483db6
--- /dev/null
+++ b/benchmark/test_flowfield.mjs
@@ -0,0 +1,23 @@
+import { setTargetState, setTargetRuntime } from "../src/core/target.js";
+import { seed } from "../src/core/utils.js";
+import { isFieldReady, Position, noField } from "../src/core/flowfield.js";
+import { Plot } from "../src/core/plot.js";
+
+// Provide mock Renderer with loaded=true to skip Mix.load()
+setTargetState({ Cwidth: 800, Cheight: 600, Density: 1, Renderer: { loaded: true } });
+setTargetRuntime({ isCanvasReady: () => {}, syncDensity: () => 1 });
+seed(42);
+isFieldReady();
+noField();
+
+const plot = new Plot("curve");
+for (let i = 0; i < 10; i++) plot.addSegment(i * 36, 100, 0.8);
+plot.endPlot(0, 1);
+
+const pos = new Position(400, 300);
+pos.moveTo(45, 200, 1);
+console.log("pos after moveTo:", pos.x.toFixed(2), pos.y.toFixed(2));
+
+const pos2 = new Position(400, 300);
+pos2.plotTo(plot, 500, 1, 1);
+console.log("pos2 after plotTo:", pos2.x.toFixed(2), pos2.y.toFixed(2));