Skip to content

Bilateral speedup#766

Merged
cospectrum merged 6 commits into
image-rs:mainfrom
CattleProdigy:bilateral-speedup
Apr 16, 2026
Merged

Bilateral speedup#766
cospectrum merged 6 commits into
image-rs:mainfrom
CattleProdigy:bilateral-speedup

Conversation

@CattleProdigy

@CattleProdigy CattleProdigy commented Mar 25, 2026

Copy link
Copy Markdown
Contributor

At <dayjob> we noticed a pretty serious regression in performance in the bilateral filter when upgrading from 0.25 to 0.26. So I worked on speeding it up.

Not allocating more than necessary is by far the most significant improvement. The cartesian_product helper has some noticeable overhead (10s of ms). I also tried a well-known approximation for the exp(), I'm a little more skeptical of adding that one though. LMK

Baseline:
gray 500x500 r=5:
  median:   901.39ms  mean:   901.54ms  min:   900.65ms  max:   903.81ms
rgb  500x500 r=5:
  median:   939.59ms  mean:   940.08ms  min:   938.23ms  max:   942.73ms

Skip allocations and cartesian_product:
gray 500x500 r=5:
  median:   101.20ms  mean:   101.27ms  min:   101.12ms  max:   101.59ms
rgb  500x500 r=5:
  median:   155.27ms  mean:   155.30ms  min:   155.15ms  max:   155.53ms

exp approximation (+ previous optimizations)
gray 500x500 r=5:
  median:    74.99ms  mean:    75.01ms  min:    74.90ms  max:    75.14ms
rgb  500x500 r=5:
  median:   123.16ms  mean:   123.19ms  min:   122.96ms  max:   123.66ms

Replace per-pixel Vec allocations in weighted_average with fixed-size
stack arrays, and replace itertools cartesian_product with plain nested
for loops.
@cospectrum

Copy link
Copy Markdown
Contributor

could you please add unit-tests (or better proptests) to check that the error of fast_exp is less than 4%?

@CattleProdigy

Copy link
Copy Markdown
Contributor Author

could you please add unit-tests (or better proptests) to check that the error of fast_exp is less than 4%?

See changes here: c0bbe01

I tested all negative f32 values until -87 where f32::exp underflows anyway.

Comment thread src/filter/bilateral.rs
Comment thread src/filter/bilateral.rs Outdated
@cospectrum cospectrum merged commit 9211130 into image-rs:main Apr 16, 2026
19 checks passed
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.

2 participants