GitHub Blog
1. Don't stop early: Case-folding source code at memory speed
一个引人入胜的优化案例:移除提前退出分支使编译器实现向量化,将大小写折叠性能提升超过 15 倍。文章还介绍了紧凑的 Unicode 表与缓冲区复用策略,对大规模搜索与文本处理很有参考价值。 (score: 0.93)
GitHub engineers describe how they accelerated case folding for Blackbird, their code search engine. The key insight is counterintuitive: removing an early-exit branch from the ASCII fast path allows the compiler to vectorize, jumping performance from ~3 GiB/s to over 45 GiB/s. They also present a compact 1776-byte Unicode case-folding table that avoids decoding characters by using a paged bitmap, packed run ranges, and little-endian byte arithmetic, plus an allocation strategy that reuses the input buffer whenever possible.
- GitHub's code search engine Blackbird indexes over 180 million repositories, so every byte of source code is case-folded and speed is critical.
- The biggest ASCII performance win came from removing an early-exit optimization: a fully branchless sweep lets LLVM vectorize, raising throughput from ~3 GiB/s to more than 45 GiB/s.
- Case folding differs from lowercasing: it is context-free and locale-independent; the crate implements simple folds (statuses C and S), not full multi-character folds.
- The compact Unicode table packs 1484 fold mappings into 1776 bytes using a paged bitmap, run-length interval compression, and a little-endian byte-addition fold that avoids decoding UTF-8.
- The function avoids unnecessary allocation by taking the input String by value and returning the original buffer unchanged for pure ASCII or non-folding multibyte text; a secondary buffer is allocated only for length-changing folds.