Five weeks to CppCon

An intro to the blog series about blake3pp, my C++ implementation of the BLAKE3 hash function: where it came from, what the series is about, and the vocabulary the later parts use.

An empty function, 2022

Back in May 2022 I spent a weekend writing a BLAKE3 implementation in C++. It was scalar, it passed the four test vectors I had generated with the reference C implementation, and on my laptop it hashed about 780 MB/s. It also contained this hopeful comment:

// SIMD things...
void compress_simd() { }

That was the plan, in its entirety. I wanted to use MIPP, a portable intrinsics wrapper that I found quite elegant. It never happened. Back then I was busy building tipi.build, the company I co-founded, and its source scan engine choked on parts of MIPP. That stopped my weekend project dead.

Other things took over. My quick-and-dirty BLAKE3 implementation stayed pretty much untouched for four years. The repository sat on that same laptop, with its empty README.md, a zero-byte file called HellO (fast-typing typo included) and that empty compress_simd function.

The idea never really went away, though. BLAKE3 belongs to a family of cryptographic designs I like (the ChaCha and Salsa20 lineage), where the algorithm's structure invites a fast implementation instead of fighting it.

The excuse, 2026

By 2026, the parts of C++ I would have needed in 2022 were not just papers anymore: std::execution (senders and receivers, P2300) is in C++26, and so is std::simd. Compilers ship partial versions of both, and there are good libraries standing in where they don't.

I wanted to learn both beyond reading the proposals, and the most reliable way I know to make time for something is to promise a conference audience that it will exist. So I submitted a CppCon talk. The abstract said, among other things, that PCIe 5 drives can push 10 GB/s and that you can hash at that speed in portable C++26 without giving up clean architecture.

I want to be honest about that last sentence: when I wrote it, there was not a single measurement behind it. There was the knowledge that the Rust implementation proves the computing side can go that fast, and a strong opinion that this should be doable with the shiny new libraries in C++26.

Then the talk was accepted.

And then life happened.

The first commit of the new repository is dated 15 August 2026, about five weeks before my talk at CppCon.

The rules

When I finally got to write the first bits of code, I set myself four rules, so that a talk about "clean and fast" would not fall apart under deadline pressure:

  1. One binary, every instruction set. For each CPU architecture (x86-64, ARM64, RISC-V, ...) the library is compiled once per SIMD instruction set it supports, all into the same binary, and picks the best one for the machine it finds itself on, at run time.
  2. Threads through std::execution. No hand-rolled thread pool API in the library. Parallel work is a sender.
  3. The fastest I/O each operating system has. io_uring on Linux, I/O completion ports on Windows, and so on, with a fallback for when the fast path is not available.
  4. The run-time choice is free. Picking the instruction set at run time costs nothing measurable in the hot path.

Five weeks, a lot of machines

What followed was five weeks of the kind of engineering that is mostly getting things to build, and then finding out what you actually built by running it on hardware you didn't expect to own an opinion about.

Some highlights, in no order of importance:

  • A phone. The first phone in the test matrix, my Galaxy S26, came back with the ARM kernel (the SIMD code for ARM) running at 79% of the speed of the C reference (the official BLAKE3 implementation), way out of line with every other ARM core I had tested by then. The kernel was fine; the comparison wasn't. The benchmark ran the reference last, and after thirty seconds of load the scheduler had worked out that the phone's one really fast core was the one to use. Pinned to that core with taskset, the kernel ran at 100%.
  • Big-endian. IBM's POWER and z machines joined the list of supported architectures one late night, because "why not?". With a one-line source change to the kernel, they produced the same digests under emulation as x86, including on big-endian s390x.
  • Three RISC-V boards, one per flavour of vector extension, each picking the right kernel on its own silicon. On one of them the library was 2.1 times faster than the C reference, which has no vectorized RISC-V kernel at all.
  • A microcontroller. At the small end, a Raspberry Pi Pico 2 with its RP2350 running Zephyr, in both of its personalities, Arm Cortex-M33 and Hazard3 RISC-V. No filesystem, no vector unit, and a standard library whose <thread> turned out to be empty.
  • A Ryzen AI MAX+ 395, where AVX-512 was mysteriously slow on Windows until it turned out the Windows power plan was clamping the clocks exactly when the 512-bit units light up. +72% from a power setting.
  • The browser. The library compiles to WebAssembly, where the SIMD build is 2.3 times faster than the Rust-compiled blake3-wasm package, and the threaded build pushes that lead to 11x.

10 GB/s, promised; 43 delivered

Shortly before the talk I rented a bare-metal server with four PCIe 5 drives in a stripe, the first machine I had that could actually test the abstract's claim.

The drives read at 57 GB/s. With hand-tuned settings, the library hashed at 43 GB/s from disk: more than four times the 10 I had promised without a single measurement.

The talk happened, and it went well after a really rocky start (fighting the A/V equipment is no fun). Then the actual work started: tidying up the shortcuts I had taken to get there. That 43 GB/s needed settings tuned by hand for one machine, every operating system had its own way of reading a file, and more than one fallback failed without saying so.

What this series is

The talk showed what I could achieve in five weeks' worth of evenings, with all my AI subscriptions maxed out. What came after it was turning that into a library I am happy to put my name on: one I/O layer instead of a pile of per-platform ones, kernels that caught up with the reference and passed it on long inputs, tools that behave like the Unix tools they replace, an API that settled, and a lot of measurements, some of which made fun of me.

Where possible I'll link to the merge commit that holds the work of each article, so you can check out any point of the story and run it.

Part 1 is about the trace tooling I built first to record where each window's time goes, because everything after it was decided by what it measured. Part 2 is the layer that reads those windows: one contract over every operating system's way of reading a file.

The rest of this post is reference material. Skim it now, or come back when a later part uses a word you don't know.

A field guide

The later parts use a handful of words as if everyone knew them. Here they are, once, with the definitions the code uses.

BLAKE3 in one picture

BLAKE3 is a cryptographic hash, the same kind of thing as SHA-256 or SHA-3: any amount of input, a fixed length fingerprint out, and no practical way to find two inputs with the same fingerprint.

Its shape is a tree:

                        root
                   /            \
             parent              parent
            /      \            /      \
        chunk 0  chunk 1    chunk 2  chunk 3      (1 KiB each)
  • The input is cut into chunks of 1 KiB. Each chunk is hashed on its own, 64-byte block by block, by the compression function, and comes out as a 32-byte chaining value (CV).
  • Two sibling CVs are combined into a parent node, which is compressed into a new CV. Parents pair up in turn until one node is left: the root, which produces the hash.
  • A subtree is any complete part of that tree: a power-of-two run of chunks that reduces to one CV.

The important property: every chunk is independent of every other chunk. That is what makes the rest of this possible.

If you feed the hash a little at a time (a hasher with update()), it keeps a stack of CVs for the subtrees it has finished so far, and merges them as they complete. One detail matters later: the root has to be marked as the root, so the hasher can't finish the last chunk until it knows the input has ended.

Going wide: SIMD

A modern CPU core can apply one instruction to several values at once: 4 32-bit words with SSE, 8 with AVX2, 16 with AVX-512. Those slots are lanes. BLAKE3's compression function works on 32-bit words, so the obvious way to use lanes is to hash 4, 8 or 16 chunks at once, one per lane.

  • A kernel is the compression function written for a given number of lanes. blake3pp has one source for it, templated on the width, compiled once per instruction set: SSE4.2, AVX2, AVX-512, NEON, SVE, RISC-V vectors, and a few more.
  • The data is in the wrong shape for that kind of data processing though: a chunk's words sit next to each other in memory, but a lane wants word 0 of chunk 0 next to word 0 of chunk 1. Rearranging them is the job of the transpose function, and it is a significant cost of its own.
  • Dispatch is picking the kernel at run time: the library asks the CPU what it supports and takes the widest kernel it has. A binary carrying all the kernels for its architecture (every x86-64 instruction set, say, or every ARM64 one) is a fat binary; there is still one per architecture.
  • A SIMD provider is the library under the kernel's vector types: C++26 std::simd where the compiler has it, xsimd where it doesn't, and a couple of others. The kernel is written against a thin facade, so the provider is a build option, not a rewrite.

Going parallel: senders, pools and parts

SIMD makes one core fast. The tree's independence also lets you use many of those concurrently.

  • A sender (C++26 std::execution) describes work without running it: "on this scheduler, do this, then that". You connect it to whatever receives the result and start it. An execution provider is the implementation of std::execution the build uses; in blake3pp, currently stdexec or beman.execution.
  • A scheduler is where work runs. The one that matters here is a parallel scheduler backed by a pool: a fixed set of threads waiting for work.
  • bulk is the sender algorithm that runs the same function N times in parallel. Each of those runs, on its pool thread, is an agent.
  • A part is a subtree of at least 16 chunks. A big input is split into parts, and the agents pull parts from a shared counter until none are left, so a slow thread simply takes fewer. Each part comes back as one CV, and the CVs are folded into the tree.

Reading a file: windows and engines

Hashing in memory is a problem of compute alone. Hashing a file is a problem of the whole machine: the drive, the kernel, the memory and the cores all have to keep up with each other.

  • A window is a fixed-size slice of the file (8 MiB by default in blake3pp), read into memory and hashed as one subtree. The file is processed as a sequence of windows.
  • The queue depth is how many reads are outstanding at once, so the drive always has work while the CPU hashes.
  • The arena is the block of memory the windows live in, blake3pp allocates it once.
  • Direct I/O (O_DIRECT on Linux, FILE_FLAG_NO_BUFFERING on Windows) reads straight from the device into the arena, skipping the operating system's page cache. It is faster for a big file read once, and it comes with rules: buffers, offsets and lengths must be multiples of 4 KiB.
  • An engine is the operating system's asynchronous I/O mechanism: io_uring on Linux, I/O completion ports on Windows, Grand Central Dispatch on macOS, and plain synchronous reads everywhere else. More engines join later in the series.
  • The ladder is the sequence of fallbacks when the fast path isn't available: no io_uring, no direct I/O for this file system, a length that isn't a multiple of 4 KiB. Each rung is a slower, more general way to read, and the library always says which rung a file ended up on.
  • The driver is the code (and the thread) that issues the reads, hands windows to the pool and collects them again.
  • I call the rate at which the device reads with no hashing at all raw. It is the ceiling: a run at 100% of raw is device-bound, and anything else is waiting on some piece of code I wrote.

See you in Part 1.

Comments

Loading comments…