Skip to content

Avoid redundant work in curve and stroke fitting - #41

Open
Dikluwe wants to merge 1 commit into
logolabs:mainfrom
Dikluwe:codex/preserve-camera-contours
Open

Dikluwe wants to merge 1 commit into
logolabs:mainfrom
Dikluwe:codex/preserve-camera-contours

Conversation

@Dikluwe

@Dikluwe Dikluwe commented Nov 20, 2026 •

Copy link
Copy Markdown

Changes

Reduce CPU work in curve and stroke fitting by reusing tangent measurements and identical chain fits, refining only the winning closed cut, computing boundary cost only for viable primitive candidates, and skipping stroke fitting when the outline score is non-finite.

Validation

  • Compared the base (0633985) and this patch (3b483c2) across the full Inkvec gate on Linux x86_64. All 1,968 SVG pairs (246 icons × 8 conditions) were byte-for-byte identical, with zero failures.
  • 760 Rust library tests passed across inkvec-cli, inkvec-fit, and inkvec-trace (8 ignored).
  • On the Flower 2 Lanczos reproducer with --lossy auto, median single-threaded CPU time fell from 2.48 s to 0.59 s across five paired runs.

@Dikluwe
Dikluwe marked this pull request as ready for review November 20, 2026 16:49
@Stefatorus Stefatorus self-assigned this Nov 21, 2026
@Stefatorus Stefatorus added bug Something isn't working enhancement New feature or request labels Nov 21, 2026
@Stefatorus

Stefatorus commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

On it. I'm also trying to see if we can reproduce this issue for later and potentially improve the test suite to catch it in the future. Additionally, I want to see if i can do additional improvements to your solution.

@Stefatorus

Copy link
Copy Markdown
Member

Thanks for the detailed write-up, benchmarks, and reproducer files (helped a lot with debugging on my end).

It seems to me that the distortion and slowdown in these examples come from Lanczos ringing in the input PNGs (lanczos has negative lobes).

The issue happens due to the following chain:

  1. Because Lanczos has negative lobes, downsampling creates a 1–2 px dark undershoot rim around every edge (#0e2a4c / [14, 42, 76] at 256 px — 13 levels darker than the actual #1b3555 / [27, 53, 85] ink, with 1,683 pixels darker than the ink; #0d284a at 128 px).
  2. Since the file is a PNG (--lossy auto → lossless) and ringing_score probes the 3–7 px band for 8×8 DCT ringing, the noise guard stays off (ringing=0.0000, edge_width=1.00)
  3. Because that dark rim sits outside the #1b3555–#ffffff convex blend segment (blend=false), has OKLab distance 0.048 > 0.035, and covers 1.48% > 0.4% of the image, the palette extractor admits #0e2a4c as a 3rd fake ink. That shatters a 2-colour / 5-face icon into 134 faces (camera-256) and 257 faces (flower-2-256), which is both what blows up outline_chi2 (making the distorted miter stroke win) and what makes flower-2 slow.

In short, lanczos is treated baddly when --lossy mode is auto on Lanczos PNGs.

For comparison:

  1. On a standard antialiased opaque render of Lucide camera (no Lanczos ringing) with your exact flags (--mode quality --restore off --sr off), Inkvec already produces 2 colours, 5 faces, ~1.1 KB (clean + strokes, MAE ~0.11–0.14).
  2. On your exact Lanczos PNGs, passing --lossy on (which enables the noise-aware intake for resampled/rung PNGs) yields lower MAE results than the ones proposed in your commits.
pr41_comparison

However, the bit-exact CPU optimizations in this PR are genuinely clean and useful! I'd be happy to merge a trimmed version of this PR (or a fresh PR) containing just those:

Keep (all bit-exact, no snapshot changes):

  • Commit 56a011b - precomputing tangent lengths/angles in SpanScorer (candidates.rs, candidates/bounded.rs, multimodel/scan.rs, tangents.rs).
  • Deferring refine() in solve_closed to the winning cut only (crates/inkvec-fit/src/multimodel.rs).
  • ChainFits memoization and moving boundary_cost inside the if let Some(...) branch (crates/inkvec-trace/src/ribbon.rs).
  • Skipping !chi2_outline.is_finite() before calling fit_face (crates/inkvec-cli/src/ribbons.rs).

Drop from this PR:

  • despeckle_after_blend (crates/inkvec-trace/src/regions.rs, lib.rs, and bindings/contract/cases.json).
  • BudgetLimitedMisfit heuristic pruning (crates/inkvec-trace/src/ribbon.rs).
  • Commit e3d3d44 — the lossless-only miter guard (crates/inkvec-cli/src/ribbons.rs).

If you're interested in handling 1 to 2 px Lanczos/sinc undershoot halos automatically without --lossy on, feel free to open a separate issue (that fix belongs upstream in intake/palette overshoot rejection rather than in post-blend despeckle and ribbon gating)

Let me know if you'd like to strip this PR down to the bit-exact changes (or open a new PR for them)! I can also help with integrating the needed PR work, if you need anything from me.

@Dikluwe

@Dikluwe
Dikluwe force-pushed the codex/preserve-camera-contours branch from e3d3d44 to 3b483c2 Compare November 21, 2026 17:53
@Dikluwe Dikluwe changed the title Preserve camera contours and reduce curve-fitting CPU cost Avoid redundant work in curve and stroke fitting Nov 21, 2026

Dikluwe commented Nov 21, 2026

Copy link
Copy Markdown
Author

@Stefatorus Thanks for tracing the issue to Lanczos ringing. I trimmed this PR to the CPU optimizations you listed. The updated head is 3b483c2.

I compared it against 0633985 on Linux x86_64 using the full gate’s 246 icons and all eight Quality and Fast conditions. All 1,968 SVG pairs were byte-for-byte identical, with zero failures.

@Stefatorus

Copy link
Copy Markdown
Member

It seems the quality ratchet is failing, can you check the reason CI & bindings fail and double-check the code? It might be code lines per file.

Dikluwe commented Dec 1, 2026

Copy link
Copy Markdown
Author

@Stefatorus I reproduced the seven tiny_* contract failures on the PR base (0633985) and the trimmed patch (3b483c2) side by side on the same ubuntu-26.04 runner. Both produced identical byte counts and SHA-256 hashes for all seven failing cases. The quality ratchet fails only because cargo llvm-cov runs tests/contract.rs and aborts before reaching the lines-per-file check. (Side-by-side run)

Root cause

I traced the divergence to base.hypot(extra) in inflate_for_curvature (crates/inkvec-trace/src/contour.rs). Both arguments match bit for bit, but the runner's hypot@@GLIBC_2.35 differs by 1 ULP on two vertices. That 1-ULP shift in vertex sigma changes the weights in refine_junctions, propagates through L-BFGS, and flips a discrete curve-fitting decision on one contour in tiny_defaults.

Although both the runner and the baseline .deb report libc6 2.43-2ubuntu2.7 amd64, their libm.so.6 binaries differ (c5f5b131… on the runner vs. 37b9e6c9… in the baseline .deb). The runner's hypot@@GLIBC_2.35 is compiled with FMA (vfmadd132sd + vsqrtsd), whereas the baseline build uses the software SSE2 path. Loading only the runner's libm.so.6 locally—with the exact same Inkvec binary, loader, libc, and CPU—reproduced all seven failures and the exact 8,385-byte tiny_defaults SVG (8ba1bef6…). (Runner library capture)

Why this happens

Neither libm build is buggy: glibc's contract for hypot is $\le 1\text{ ULP}$, not 0.5-ULP exact rounding, and both the FMA and SSE2 paths satisfy it. The mismatch occurs because bindings/contract/cases.json expects an exact SHA-256 keyed only by x86_64-linux-gnu, which implicitly couples the contract to the host distribution's libm.so.6 build (e.g., baseline amd64 vs. amd64v3).

Next steps

How would you prefer to handle the Linux contract baseline on main?

  • Short term: pin the Linux CI runner image (e.g., ubuntu-24.04) or re-bless cases.json for ubuntu-26.04.
  • Long term (if the exact SHA-256 contract should hold across libm builds): replace system libm calls (starting with hypot) with a deterministic pure-Rust implementation (e.g., libm::hypot) in a separate PR, or relax the contract beyond exact SHA-256 when system libm is used.

I'm happy to test a deterministic hypot in a separate PR if you'd like, and I'll rebase #41 once main is green.

This branch has not been deployed

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

Labels

bug Something isn't working enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants