Add NearestFrom for faster fast_blur#2868
Add NearestFrom for faster fast_blur#2868RunDevelopment wants to merge 4 commits intoimage-rs:mainfrom
NearestFrom for faster fast_blur#2868Conversation
|
We may want to spin everything related to |
|
Depends. For example, I also want to add a trait to make RGB->Luma conversions faster. This would also need to be a super trait of So I'm not too sure that the code in |
After thinking about it a bit more, I implemented your suggestion. I don't see it growing a lot now, but there's also no harm in doing so. |
After talking about it here, I removed the
f32::roundin the hot path offast_blur.I did this by adding a new private trait:
NearestFrom. This has similar semantics toNumCast::from, but it will round to nearest for float->int conversions and saturate on numeric bounds instead of returningOption. This makes it a natural fit for performance-sensitive code that needs to convert f32 to subpixels.For now, I only used the trait in
fast_blur, but other operations can use it as well for both correctness and performance. I specifically designed the trait to have uses beyondfast_blur.This PR does make
fast_blursignificantly faster, even for theu8case. Here are the benchmark results from my machine (Intel i7-8700K):That's between 6-7 times faster.
Note that this is no competition for #2846 and its fixed-point implementation for u8. On my machine, #2846 reaches 8ms on the same benchmark.
I also want to mention that some implementations of
NearestFromstill leave performance on the table. See this comment for example. I just optimized the important primitives for now. Everything else can follow later.