Bilateral speedup - #798
Open
CattleProdigy wants to merge 2 commits into
Open
Conversation
cospectrum
requested changes
Jul 14, 2026
| for i in 0..num_channels { | ||
| out_channels[i] = (channel_sums[i] / weight_sum).as_(); | ||
| // Bottom edge | ||
| for y in (height - radius)..height { |
Contributor
There was a problem hiding this comment.
I think this overlaps with the previous loop of y in 0..radius, for example:
radius = 7
height = 10
0..radius <=> 0..7
(height - radius)..height <=> (10 - 7)..10 <=> 3..10
Contributor
There was a problem hiding this comment.
This will do
debug_assert!(radius <= height)
let interior_end = (height - radius).max(radius);
for y in 0..radius {}
for y in radius..interior_end {}
for y in interior_end..height {}| /// (used for the interior). Because `CLAMP` is a const generic, each variant | ||
| /// monomorphizes to straight-line code with the unused branch eliminated. | ||
| #[inline(always)] | ||
| fn filter<const CLAMP: bool>(&self, x: u32, y: u32) -> P { |
| } | ||
|
|
||
| impl LutGaussianEuclideanColorDistance { | ||
| /// Creates a new [`GaussianEuclideanColorDistance`] using a given sigma value, which |
Contributor
There was a problem hiding this comment.
[LutGaussianEuclideanColorDistance]
| pub fn new(sigma: f32) -> Self { | ||
| assert!( | ||
| sigma > 0.0, | ||
| "GaussianEuclideanColorDistance sigma must be positive" |
Contributor
There was a problem hiding this comment.
LutGaussianEuclideanColorDistance
| /// a look up table. This gives substantial speed up for pixel's with a u8 subpixel. | ||
| /// | ||
| /// This implements [`ColorDistance`]. | ||
| pub struct LutGaussianEuclideanColorDistance { |
| channel_sums[i] += weight * f32::from(*c); | ||
| } | ||
| // Right edge | ||
| for x in (width - radius)..width { |
Contributor
There was a problem hiding this comment.
overlaps similar to comment below
| let num_channels = P::CHANNEL_COUNT as usize; | ||
| let out_channels = out_pixel.channels_mut(); | ||
| for i in 0..num_channels { | ||
| out_channels[i] = (channel_sums[i] / weight_sum).as_(); |
Contributor
There was a problem hiding this comment.
Probably faster with zip because out_channels is a slice, plus will not go out of bounds
| sigma > 0.0, | ||
| "GaussianEuclideanColorDistance sigma must be positive" | ||
| ); | ||
| let mut lut = Vec::with_capacity(512); |
Contributor
There was a problem hiding this comment.
maybe lets allocate [f32; 512] on the stack?
cospectrum
requested changes
Jul 14, 2026
|
|
||
| let radius = i16::from(radius); | ||
| let (width, height) = image.dimensions(); | ||
| let radius = (radius as u32).min(width).min(height); |
Contributor
There was a problem hiding this comment.
I think we should document that radius is clamped to be in bounds
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.
Even after the last round of performance improvements to the bilateral filter there was still a pretty big performance drop starting after 0.25. It turned out to mostly because of confusing the window-radius vs window-size argument change. Nonetheless, I kept making performance improvements.
This PR has two more improvements:
u8-subpixel pixel types. This LUT implementation has twice the memory footprint, but is faster since it avoids an absolute value.Timing
Dynamic Color-Distance
Baseline
gray: 70ms
rgb: 116ms
With Clamping Rework
gray: 70ms -> 51ms
rgb: 116 -> 93ms
LUT-based Color Distance
New LUT
gray: 38ms
rgb: 79ms
New LUT + Clamping Rework
gray: 38ms -> 17ms
rgb: 79ms -> 59ms
0.25 LUT-based gray only
gray: 70ms