Skip to content

BMP: Faster bitfield reading#2900

Merged
197g merged 5 commits into
image-rs:mainfrom
RunDevelopment:bmp-faster-bitfield
Apr 19, 2026
Merged

BMP: Faster bitfield reading#2900
197g merged 5 commits into
image-rs:mainfrom
RunDevelopment:bmp-faster-bitfield

Conversation

@RunDevelopment
Copy link
Copy Markdown
Member

I noticed that BMP uses LUTs for UNORM conversions. Just like in #2899, this is not optimal for performance, so I replaced it with faster conversions using the multiply-add method.

I also made BitField::read branchless to hopefully allow the compiler to auto-vectorize. This also has the nice side effect that all bitfields, no matter their length, now take the same time to read, which makes performance more consistent.

Here are the benchmark results from decode.rs:

Test Old New Change
load-Bmp/Core_1_Bit.bmp 111.68 µs 112.03 µs -0.7302%
load-Bmp/Core_4_Bit.bmp 201.36 µs 203.95 µs +0.9800%
load-Bmp/Core_8_Bit.bmp 193.11 µs 193.34 µs +0.2818%
load-Bmp/rgb16.bmp 37.932 µs 17.988 µs -52.403%
load-Bmp/rgb24.bmp 12.853 µs 12.397 µs -3.7010%
load-Bmp/rgb32.bmp 12.863 µs 12.364 µs -3.2297%
load-Bmp/pal4rle.bmp 14.527 µs 14.565 µs +1.3192%
load-Bmp/pal8rle.bmp 14.329 µs 14.778 µs +4.3286%
load-Bmp/rgb16-565.bmp 65.078 µs 17.740 µs -72.314%
load-Bmp/rgb32bf.bmp 42.666 µs 16.993 µs -60.177%

As we can see, the common case of 8-bit fields (which require no conversion) is within the noise threshold (that's also what criterion said), while everything else is significantly faster.

Copy link
Copy Markdown
Member

@197g 197g left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice and sweet

Comment thread src/codecs/bmp/decoder.rs Outdated
@197g 197g merged commit bad7b9d into image-rs:main Apr 19, 2026
31 checks passed
@RunDevelopment RunDevelopment deleted the bmp-faster-bitfield branch April 19, 2026 21:25
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