Don’t stop early: Case-folding source code at memory speed
GitHub engineers optimize case-folding performance by removing an early exit optimization and instead, sweeping the whole buffer for non-ASCII characters. This results in a significant speedup to >45 GiB/s when using vectorization on an Apple M4. Their Rust crate, casefold, implements a branch-free, memory-speed case-folding operation. By removing branches and using arithmetic to test ASCII letters, the engineers achieve a memory-bandwidth-limited performance at >45 GiB/s. However, this step can be a pessimization in scalar code, indicating that the optimal solution is highly dependent on the hardware and the specific use case. In a middle ground, using standard library functions to scan a machine word at a time can achieve a faster performance at around 23 GiB/s, which can be suitable as a general-purpose default.