Repository navigation
canvas: Reuse parsed SVG paths across drawings - #4213
Draft
lurenjia534 wants to merge 1 commit into
Draft
lurenjia534 wants to merge 1 commit into
lurenjia534 wants to merge 1 commit into
Conversation
Parse SVG once in the QuickJS Path2D constructor and share immutable geometry through recording snapshots. Keep composite paths and animated arguments bound to each drawing, and retain the browser string decoder. Cover shared geometry and snapshot/animation semantics with two focused regression tests. The sharing test fails on main and passes here. Linux x86_64 development builds, 64 strokes per redraw: event medians fall from 204 to 108 us for 4 segments and 1432 to 125.5 us for 128. Both versions submit 1 op / 1 mount item / 9 nodes. One-use paths cost about the same; construction alone now pays for parsing. Add a reusable benchmark and document the measurements and tradeoff.
Member
|
❌ The win on reused paths is real, but eager parsing makes construct-only workloads ~20x slower, and a public trait method is added just for plumbing.
Reviewed head: b395c7b (read-only diff review; not built locally). |
lurenjia534
marked this pull request as draft
December 3, 2026 13:01
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
QuickJS's
Path2D(svg)kept the SVG source in its recording. Everyfill,strokeorclipdecoded that source again, including repeated uses of the same path in later drawings.Parse the SVG once in the constructor and keep an immutable
Arc<[PathOp]>in the native recording. A drawing that uses that SVG segment alone receives the same geometry. Copies andaddPathsnapshots share the segment; composite paths assemble their operations in order, with animated arguments still decoded against the current drawing's slots. This avoids sharing drawing-local animation indices.The browser recorder's existing
[CV_SVG_PATH, string]representation remains supported. There is no global cache, new canvas opcode, or change to theDrawingformat consumed by platform renderers.Public API
nsh-core,nsh_core::js::host::HostInput:An optional hook for a host input to supply immutable canvas geometry containing only constant coordinates. Its default implementation returns
None, so existing host implementations continue to use the string decoder without adding a method.The hook is needed to pass the QuickJS recorder's parsed geometry through the shared decoder. These runtime-owned recording and decoding steps cannot be changed by an app or an nshm. This adds no JavaScript/TypeScript API.
Performance
Compared with main
59a945f04b1340482962532dec945c33df8fe2d2on Linux x86_64, Rust 1.100.1. Both CLIs use the workspace's development optimization levels, with debug info and incremental compilation disabled. Three alternating before/after runs; five warmups followed by 20 measured presses per mode per run. The table reports median / nearest-rank p95 of the pooled 60event jssamples, in microseconds.Every redraw has the same 1 op / 1 mount item / 9 nodes; construction alone submits no drawing. The retained-path event median falls by about 47% for four line segments and 91% for 128. Constructing and drawing once is essentially unchanged. Construction alone is slower because it now includes parsing, even if the path is never drawn.
These measurements cover the JS callback, recording and decoding, not native path construction, painting or frame rate. Composite paths still assemble their geometry, and recording snapshots still allocate small lists.
The counter control (
nsh test examples/counter/App.tsx --trace, three alternating runs, 25 presses each, first five discarded) has identical commit sequences, including 56 ops / 50 mount items / 15 nodes initially. Its event medians are 162.5 / 147 microseconds and first-frame ranges 6.75–7.94 / 6.08–10.13 ms; no broader runtime speedup is claimed.The reproducible workload is
examples/bench/CanvasPaths.tsx, with results and methodology indocs/dev/performance.md. Press each ofid:reuse,id:freshandid:construct25 times, discard each mode's first five samples, and changeSEGMENTSto 4 for the small path. Redirect the full trace because it prints all path operations.How to Test
Completed locally:
cargo build -p nsh-cli.cargo test -p nsh-engine --test engine canvas::: 10 passed, including the two new regression tests and existing OffscreenCanvas, hit-test and redraw coverage.cargo test -p nsh-core --lib canvas::tests::: 10 passed, reusing the existing SVG parser and malformed-prefix checks.Arc::ptr_eq, while the snapshot/animation test passes. This confirms the test catches the original problem.nsh test --tracebefore/after for the path benchmark and counter control, as above. An additional headless probe verified empty/invalid SVG paths do not fall back to the current context path, valid SVG hit testing works, and malformed data retains its valid prefix.cargo xtask compat: passed, including the checkout apps and the full coverage-claim checks.cargo clippy -p nsh-core -p nsh-js -p nsh-engine --all-targets: passed.git diff --check.Only two regression tests were added. One checks real emitted geometry and sharing across fill/stroke/clip, copies,
addPathand redraws; the other protects mutation snapshots, self-add, composite paths and animation-slot rebinding. There are no timing thresholds or duplicate parser tests.Whole-workspace
cargo +nightly fmt --all -- --checkreports the same pre-existing formatting differences in 18 unchanged files on both the baseline and this branch; the two diagnostic logs are byte-for-byte identical. The changed Rust files pass. Native GUI/FPS checks and the full multi-platform workspace suite were not run for this recorder/decoder change.