
struct Player {
\\ ....
}
// Deserialization - one line!
Player load_player(std::string& json_str) {
return simdjson::from(json_str); // That's it!
}
// Serialization - one line!
void save_player(const Player& p) {
std::string json = simdjson::to_json(p); // That's it!
// Save json to file...
}
,:[, ], {, }H(c).H1and H2.H1 and H2 such as the bitwise AND of the look classify the characters: H1(low(& 0xf) & H2(c >> 4)low_nibble_mask = {16, 0, 0, 0, 0, 0, 0, 0, 0, 8, 12, 1, 2, 9, 0, 0};
high_nibble_mask = {8, 0, 18, 4, 0, 1, 0, 1, 0, 0, 0, 3, 2, 1, 0, 0};
Five instructions:
nib_lo = input & 0xf;
nib_hi = input >> 4;
shuf_lo = lookup(low_nibble_mask, nib_lo);
shuf_hi = lookup(high_nibble_mask, nib_hi);
return shuf_lo & shuf_hi;
The Insight: JSON field names are known at compile time!
Traditional (Runtime):
// Every serialization call:
write_string("\"username\""); // Quote & escape at runtime
write_string("\"level\""); // Quote & escape again!
With Consteval (Compile-Time):
constexpr auto username_key = "\"username\":"; // Pre-computed!
b.append_literal(username_key); // Just memcpy!
The Problem: JSON requires escaping ", \, and control chars
Traditional (1 byte at a time):
for (char c : str) {
if (c == '"' || c == '\\' || c < 0x20)
return true;
}
SIMD (16 bytes at once):
auto chunk = load_16_bytes(str);
auto needs_escape = check_all_conditions_parallel(chunk);
if (!needs_escape)
return false; // Fast path!
[U+D800 to U+DBFF] followed by U+DC00 to U+DFFFPROCEDURE validate_utf16(code_units)
i ← 0
WHILE i < |code_units|
unit ← code_units[i]
IF unit ≤ 0xD7FF OR unit ≥ 0xE000 THEN
INCREMENT i
CONTINUE
IF unit ≥ 0xD800 AND unit ≤ 0xDBFF THEN
IF i + 1 ≥ |code_units| THEN
RETURN false
next_unit ← code_units[i + 1]
IF next_unit < 0xDC00 OR next_unit > 0xDFFF THEN
RETURN false
i ← i + 2 // Valid surrogate pair
CONTINUE
RETURN false
RETURN true
const str = "ab\uD800";
console.log(str.toWellFormed());
// "ab�"
| scalar | ARM NEON | |
|---|---|---|
| GB/s | 2.2 | 18.9 |
| ins/byte | 12.0 | 0.9 |
text = "Hello, World!"SGVsbG8sIFdvcmxkIQ==
const b64 = Uint8Array.toBase64(bytes); // string
const recovered = Uint8Array.fromBase64(b64); // Uint8Array, matches original 'bytes'
| function | speed |
|---|---|
Uint8Array.fromBase64() |
11 GiB/s |
Uint8Array.toBase64() |
20 GiB/s |
Test in your browser at https://simdutf.github.io/browserbase64/
vpermb (twice) and vpmultishiftqb.


---
https://lemire.me/blog/2023/04/27/hotspot-performance-engineering-fails/
https://lemire.me/blog/2023/04/27/hotspot-performance-engineering-fails/