libdivide (libdivide.com) is an awesome library.
I'm using it to replace an unsigned 32-bit and unsigned 64-bit division in my hardened malloc implementation: github.com/AndroidHardeni.
The divisors vary per size class so the compiler can't take care of optimizing it.
Conversation
It completely solves a huge performance problem I would have had to deal with by hand and only added 16 bytes to the binary size.
Before:
478.092526 msec
2,000,329,069 cycles
3,525,042,263 instructions
After:
322.796288 msec
1,349,630,342 cycles
3,625,044,439 instructions
Replying to
That's a microbenchmark of the malloc implementation using various size classes and writing data to the memory allocations. It's a drastic performance improvement since it's a perfect use case for the library. It only needs to set up the two divisors for each size class at init.
