Skip to content

PSD: avoid redundant plane copies, per-pixel mask tests and repeated quad solves (#1136) - #1137

Merged
h-a-n-a-s-h-i merged 1 commit into
storytold:mainfrom
vangel76:psd-optimise
Oct 13, 2026
Merged

h-a-n-a-s-h-i merged 1 commit into
storytold:mainfrom
vangel76:psd-optimise

Conversation

@vangel76

Copy link
Copy Markdown
Contributor

Fixes #1136

What changed (crates/psd; output is bit-identical)

  • O3: raw channel data is borrowed from the file (Cow), not copied; the merged image's ZIP planes are un-predicted in place in the inflated buffer (no per-plane to_vec); the 32-bit un-predictor reuses one scratch row instead of copying every row.
  • O5: the layer-mask loop evaluates the "inside the mask rect" test once per row (the column range is computed once), applies the constant factor to the outside parts as a slice, and skips it when it is exactly 1.0 (e.g. no mask data and default colour 255). No mask data: one bulk pass, no per-row test. Mask reads use get with the default colour as fallback instead of indexing.
  • O6: SmartObject::map, placed_bounds and placed_size keep their signatures; internally the quad's projective matrix is solved once per call and shared (render_placed solves it once for the bounds, the size and the 49x49 grid; placed_size no longer recomputes bounds separately inside a bake). In the triangle loop the source corners and the barycentric coefficients are hoisted out of the pixel loop (same operations, same order, so results do not change; the per-pixel division by the denominator is kept on purpose, a reciprocal would change rounding).

Tests (crates/psd/src/tests_opt.rs)

The replaced implementations are kept in the test module as references (ref_unpredict, ref_decode_channel, ref_composite, ref_layer_pixels, ref_map, ref_placed_bounds, ref_render_placed).

  • Equality, compared bit for bit: composite and layer pixels for depths 8 / 16 / 32 x raw / RLE / ZIP / ZIP with prediction (channels and merged image re-encoded from synthetic planes); the same on truncated files (errors compared too); layer masks moved to overlap the layer/canvas partly, wholly or not at all, default colour 0 / 255 / 90, inverted, disabled, with and without mask data, canvas and layer-sized output; placed bakes for perspective, affine, degenerate quads and warp styles/custom mesh, size None / Some, several k.
  • Counters (test-only, per thread, no timings): the 32-bit un-predictor allocates 1 scratch row for a 9-row plane (reference: 9); the mask loop performs 30 inside-tests for a 30-row canvas (reference: 40 x 30 per-pixel), 12 for the layer's own 12 rows, 0 when there is no mask data; placed_bounds and a whole render_placed solve the quad once (was 1089 per bounds call, plus again for the size, plus 2401 for the grid).
  • Pointer checks: a raw plane is a Cow::Borrowed view into the file data; a ZIP plane handed out is a sub-slice of the inflated buffer.
  • Sanity: I mutated the mask column range and the barycentric coefficient once; the new tests failed on both.

Commands run

  • cargo test -p effectcraft-psd (24 pass)
  • cargo test -p effectcraft-media layered, cargo test -p effectcraft-engine psd (pass)
  • cargo clippy --workspace --all-targets -- -D warnings (clean)
  • cargo fmt --all --check (clean), cargo xtask layers (ok)
  • cargo check -p effectcraft-psd --target wasm32-unknown-unknown (ok)

Not run: the full cargo xtask ci, the full workspace test suite. No timings or speed claims are made.

Deliberately left out

🤖 Generated with Claude Code

…quad solves (storytold#1136)

Raw channels are borrowed, ZIP planes are un-predicted in place in the inflated buffer,
the 32-bit un-predictor uses one scratch row, the layer mask tests the rect per row
instead of per pixel (and skips a constant factor of 1.0), and a placed-layer bake
solves the quad once and hoists per-triangle invariants. Output is bit-identical;
tests compare against the replaced implementations and count the avoided work.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@h-a-n-a-s-h-i
h-a-n-a-s-h-i merged commit ac9d4eb into storytold:main Oct 13, 2026
h-a-n-a-s-h-i pushed a commit that referenced this pull request Oct 13, 2026
…1139 land together

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@echelon

echelon commented Oct 13, 2026

Copy link
Copy Markdown
Member

Merged. Thank you for the contribution, @vangel76! It landed on main together with a batch of other community PRs, after a full workspace build and test run. If you are not on our Discord yet, please join us: https://discord.gg/artcraft

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PSD: avoid redundant plane copies, per-pixel mask tests and repeated quad solves

3 participants