Skip to content

perf(filter): fuse Adaptive filter scoring and avoid redundant row passes - #740

Open
gatewaytofredom wants to merge 1 commit into
image-rs:masterfrom
gatewaytofredom:perf/adaptive-filter-sum
Open

gatewaytofredom wants to merge 1 commit into
image-rs:masterfrom
gatewaytofredom:perf/adaptive-filter-sum

Conversation

@gatewaytofredom

Copy link
Copy Markdown

Summary

Relates to #611.

Filter::Adaptive (the default filter for Fast, Balanced, and High compression) currently runs filter_internal + sum_buffer sequentially for all 4 candidates
(Up, Sub, Avg, Paeth), writing and re-reading output 4 times and recomputing the winning filter a 5th time whenever Paeth does not win.

This replaces adaptive_filter(sum_buffer, ...) with a fused adaptive_filter_sum implementation (100% safe Rust under #![forbid(unsafe_code)], byte-for-byte identical
filter selection and output):

  • aarch64: Scores all 4 filters in a single read-only 32-byte pass using u16 inner accumulators (auto-vectorizes cleanly into NEON ABS + UADDLV), then
    materializes only the winning filter into output.
  • x86_64 and other targets: Uses a register-friendly 2-stage 32-byte stack-chunk pass (scoring Up/Sub/Avg without writing output, then computing Paeth
    directly into output with early abort once sum_paeth > min_cost), avoiding SSE2 XMM register spills while eliminating redundant full-row writes/re-reads.

Benchmarks (1920×1080 RGBA, 7.91 MB raw, identical 3,820,442 B output)

Platform Preset Before After Delta
Apple M4 (aarch64) Fast + Adaptive 9.93 ms (796.7 MB/s) 8.03 ms (985.5 MB/s) +23.7% faster
Apple M4 (aarch64) Balanced + Adaptive 369.53 ms 361.69 ms +2.2% faster
Intel i5-12400 (x86_64 default SSE2) Fast + Adaptive 13.73 ms (576.0 MB/s) 13.37 ms (591.5 MB/s) +2.7% faster

Testing

  • Added filter::test::adaptive_filter_sum_matches_reference verifying exact RowFilter and output byte equality against adaptive_filter(sum_buffer, ...) across all
    BytesPerPixel, lengths (bpp..513), zero-cost short-circuits, -128 boundary patterns, and random rows.
  • cargo test passes (102 unit + 8 integration + 8 doc tests) on both x86_64-unknown-linux-gnu and aarch64-apple-darwin.

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.

1 participant