Base64 encoding and decoding at almost the speed of a memory copy
Wojciech Muła, Daniel Lemire
cs.DC
2019-10-02
Three AVX-512 VBMI instructions encode 48 bytes to base64; five decode 64. Past L1, speed matches memcpy: about 40 GB/s from L2, 10–20× Chrome's lookup tables.
MIME email, data URIs, JSON, XML, and stores such as MongoDB, Elasticsearch and DynamoDB all have to turn binary into base64 before it can live in text. Three bytes become four ASCII characters drawn from letters, digits and +/. Chrome does the mapping with lookup tables. Earlier measurements, cited in the paper, already treated the decode cost as real CPU time.
The same authors had vectorized base64 on 256-bit AVX2, and that design later showed up in PHP and other runtimes. Doubling the register to 512 bits should about double throughput. The extra cut comes from VBMI byte permutation: the instruction count drops by more than 2×. Once the working set misses L1, encode and decode run about as fast as a memory copy.
The hot loop is sized to a full register block. Encode reads 48 bytes and writes 64 characters; decode reads 64 characters and writes 48 bytes. A scalar tail handles the leftover. The ops are AVX-512 VBMI, the subset that does byte lookup, permute and shift. The machine is a 2018 Cannon Lake part. The paper is explicit that this subset is not present on every AVX-512 chip.
Ignoring loads and stores, encoding is three instructions: two vpermb and one vpmultishiftqb. The AVX2 encoder needed 11 SIMD instructions to turn 24 bytes into base64.
Decoding cannot be encoding reversed, because bytes outside the alphabet must be rejected. Five instructions cover each 64 input bytes: vpermi2b, vpternlogd, vpmaddubsw, vpmaddwd and vpermb. One vpmovb2m runs once per stream. The AVX2 decoder used 14 SIMD instructions for 32 bytes.
vpermi2b looks the input up in a 128-byte table spread across two registers, taking the low 7 bits as the index. Valid alphabet slots hold the 6-bit value; every other slot holds 0x80. A bitwise OR with the original input sets the high bit on any illegal or non-ASCII byte. The inner loop does not branch. vpternlogd computes input OR translated OR error-so-far in one instruction and folds it into an ERROR register. vpmovb2m inspects that register at the end.
Two multiply-adds pack the bits back. Each 32-bit lane starts as four 6-bit fields with two leading zeros. vpmaddubsw multiplies by a repeated (0, 64). Multiplying by 64 is a left shift of 6, so neighboring bytes join into 12 bits. vpmaddwd multiplies by a repeated (4096, 1). Multiplying by 4096 is a left shift of 12, producing 24 bits and a zero high byte. vpermb then gathers the 48 live bytes through a fixed index list and restores standard order. The multiply-add is a shift and a concatenate in one instruction, which is why the pack fits inside those five ops.
The baselines are Chrome's lookup tables, the authors' previous AVX2 codec, and memcpy on the same box. The CPU is an Intel i3-8121U (Cannon Lake, 2.2 GHz base, 3.2 GHz max) with 32 KB of L1 data and 256 KB of L2 per core, and 4 MB of L3. Builds used GCC 9.1 at -O3 -march=cannonlake. Each size is the median of 10 runs, including the call overhead. Speeds are charged to base64 bytes. avx-turbo reported no AVX-512 downclock on this chip. Peak system memcpy bandwidth is about 20 GB/s.
Random inputs run from 1 KB to 64 KB. Around 10 KB, still inside L1, memcpy exceeds 150 GB/s. Past 16 KB, AVX-512 and memcpy both drop to about 40 GB/s, the rate the paper associates with L2. In that band AVX2 encodes at about 17 GB/s and decodes at about 15.5 GB/s. Chrome encodes at 1.5 GB/s and decodes at 2.6 GB/s. At or below 12 KB, AVX-512 is more than twice the AVX2 codec.
Decode rates on real files, in GB/s:
| Input | Size | memcpy | Chrome | AVX2 | AVX-512 |
| lena.jpg | 141020 B | 25 | 2.6 | 14 | 32 |
| mandril.jpg | 247222 B | 18 | 2.6 | 14 | 25 |
| Google logo | 2357 B | 44 | 2.6 | 14 | 42 |
| large zip | 34904444 B | 9.5 | 2.6 | 8.3 | 9.5 |
The 2357-byte logo fits in L1: memcpy 44, AVX-512 42. On lena and mandril, AVX-512 beats the copy because decoding 1 GB of base64 writes about 786 MB, while memcpy writes the full gigabyte. On the 34,904,444-byte zip, AVX-512 and memcpy both sit at 9.5 GB/s and AVX2 at 8.3. That run is limited by memory bandwidth.
Spread per input byte, the encoder issues about one seventh as many SIMD instructions as AVX2 (3 per 48 bytes versus 11 per 24 bytes). The decoder issues slightly under one fifth (5 per 64 bytes versus 14 per 32 bytes, a ratio of 5.6). The registers only got twice as wide. The rest of the cut is the new instructions.
While the bytes stay in cache, base64 time moves from table lookups to moving data. The paper's summary figure is 10 to 20 times a tuned conventional codec. Split with the rates it actually reports, about 40 GB/s from L2 is roughly 15 times Chrome's 2.6 GB/s decode and roughly 27 times its 1.5 GB/s encode. On the large zip the ratio is 9.5 to 2.6, about 3.7 times. The 10-to-20-times band describes the in-cache case, and it does not describe a file that has fallen out to memory.
The alphabet is a register constant, so base64url is two characters changed, not a second kernel. The AVX2 version had already entered PHP. The integration point named at the end is a vectorized parser such as simdjson. Large inputs should be sliced into pieces that fit in cache, or the memory round trip spends the gain.
On a machine that has VBMI, the change is a new constant plus a scalar tail. The base64 format itself is unchanged.
The only chip measured is the i3-8121U. Some Intel processors drop their clock when they execute AVX-512. avx-turbo found no such drop on this Cannon Lake part, and the paper found no Intel note of downclocking for it. Whether 40 GB/s holds on a chip that does downclock is unmeasured. ARM SVE and the announced SVE2 are named as a place the same idea could move, with no code and no speeds.
VBMI is not on every AVX-512 implementation. The paper lists Cannon Lake, Cascade Lake, Cooper Lake and Ice Lake as expected hosts, and only the first was tested.
Error detection asks one question: is this byte in the alphabet? The answer is accumulated to the end of the stream so the hot loop stays branch-free. Padding equals-signs, whitespace, truncated input and incremental streaming are all covered by the line that sends a length that is not a multiple of 64 down a conventional path. There is no separate correctness number and no timing on bad input. Very small inputs are also slowed by fixed overhead.
The conclusion states a seven-fold instruction cut for decoding and a five-fold cut for encoding. Section 3 counts the other way: about seven-fold for encoding, about five-fold for decoding (5.6 from the raw instruction ratio). The instruction lists in section 3 are the ones that add up.
Beating memcpy depends on charging speed to base64 bytes. Decoding writes about a quarter less, which is why lena shows 32 against 25. On the zip, AVX-512 ties the copy at 9.5 GB/s and is only slightly above AVX2 at 8.3.
The benchmark is single-threaded, ten runs, median. There is no multi-core contention result, and no end-to-end time inside a database or a parser.