Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

GitHub Engineering 18 min read Deep dive hardMaster

Don’t stop early: Case-folding source code at memory speed

GitHub's Blackbird code search case-folds every byte of 480TB of source, and the fastest ASCII path came from removing the early exit at the first non-ASCII byte. A branchless sweep that ORs bytes into an accumulator and lowercases arithmetically lets LLVM vectorize to over 45 GiB/s on an Apple M4, versus 3.1 GiB/s for the naive break loop. A two-pass chunked scan reaches about 23 GiB/s, and branchless code without vectorization is actually slower. The open-source Rust crate casefold also avoids heap allocation by reusing the input buffer. For non-ASCII text, it rejects non-folding characters with a 64-code-point page bitmap read straight from UTF-8 lead bytes, and run-length/stride-encoded fold ranges shrink Unicode's 1484 simple folds to 1776 bytes.

Assumes: UTF-8 encodingSIMD vectorization basicsRust strings and unsafe pointersbranch prediction

Related posts

  • Rope Optimizations, Part 1

    Zed mediumAdept

    Zed's Rope, a SumTree of 128-byte chunks, sped up offset-to-point translation by storing u128 bitmasks per chunk marking newlines, char boundaries and UTF-16 widths, replacing a per-character loop. With the bitmask, row and column come from masking up to the offset and calling count_ones and leading_zeros, which compile to single CPU instructions and avoid branches. A micro-benchmark shows 57ns versus 1ns, and point_to_offset got up to 70% faster in production. The same technique adds a tabs bitmask, since tab rendering depends on column position and showed up as costly in profiles; wiring it into higher layers comes in a later post.

  • The technology behind GitHub’s new code search

    GitHub Engineering mediumAdept

    GitHub built Blackbird, a Rust code search engine, because general text search engines handled punctuation, regex, and GitHub's scale poorly, and brute-force grep would yield about 0.01 QPS. It uses ngram inverted indices with lazily intersected posting-list iterators, shards by Git blob ID to deduplicate content, and delta-encodes repositories ordered by a similarity-based minimum spanning tree. Kafka decouples crawling from indexing and provides commit-level query consistency. Shards serve queries at about 100 ms p99 (roughly 640 QPS per 64-core host), and 115 TB of code fits in a 25 TB index that can be fully rebuilt in about 18 hours.

  • Copy Hunting

    TigerBeetle mediumAdept

    Reading Zig's emitted LLVM IR as plain text and grepping it is a practical substitute for staring at disassembly when chasing implicit memcopies and binary bloat. A small line-by-line tool, copyhound, flags large `llvm.memcpy` calls per function and also sums IR lines per generic function name to expose monomorphization blowup, the same idea as cargo-llvm-lines. In TigerBeetle it traced copies to `std.meta.eql`, which was replaced with a comptime-checked word-wise byte comparison. In the Zig compiler it found an inlined float-parsing slow path costing 10 KB of stack, fixed with `@setCold`. Making comptime parameters runtime in a version-printing function cut 300 bytes from the binary.

  • More Than 2x Faster Hashing in ClickHouse Using Rust

    ClickHouse mediumAdept

    ClickHouse added optional Rust library support to its C++/CMake build using Corrosion-rs, with BLAKE3 as the first library, exposed to C++ through a Rust shim function using C-compatible FFI types and a header file. BLAKE3 hashes more than 2x faster than SHA224/SHA256 and slightly faster than MD5, while staying secure and resistant to length extension. Perf flamegraphs show the shim's type conversion costs about 1.15% of query time. Integration hurdles included MemorySanitizer false positives on Rust memory, fixed with __msan_unpoison and explicit byte conversion, plus cross-platform linking, with aarch64-darwin still unsupported. The integration also had to stay optional, statically linked, cross-compilable and compatible with sanitizers.

  • A brief history of code search at GitHub

    GitHub Engineering mediumAdept

    GitHub's code search evolved from a public-only Solr index plus `git grep` for private repos, to a decade-long Elasticsearch deployment that grew to 162 nodes, 1.25PB of storage and over 53 billion indexed files. Natural-language text analysis fits source code poorly. GitHub's custom tokenizer dropped punctuation and could not do substring or regex matching. An experiment that indexed special characters made indexing 43-100% slower, grew the index 18-28% and slowed typical queries about 2.1x. A short-lived exact-match beta also failed to balance its resource cost against corpus growth. Existing trigram engines like Russ Cox's and Zoekt also fell short at GitHub's scale, so GitHub built Blackbird, a bespoke engine targeting sub-second p95 global exact-match and regex queries without using much more resources than the Elasticsearch cluster.

The quest for software engineering knowledge.

Look:
Layout: