Unescape HTML · Zig 🤝 GLM-5.3 · #3 Fastest
SHA-256
f3efa921634d88080009812e8332dfb24b5b1ee3ffee08e3aff94d4e9e1021b6
View source (50.9 KiB of Zig)
// unescape_html in Zig with Wasm SIMD: decodes every HTML character reference in UTF-8 text
// the way Python's html.unescape does, and copies all other bytes unchanged.
//
// The design goal is a short path per reference with few unpredictable branches:
// - 64 zero bytes are written after the input, so no load or loop needs a bounds check and
// nothing stale from an earlier input can be mistaken for part of a reference.
// - Two stages per 2 KiB window. Stage 1 turns each 64-byte block into a bitmask of its `&`
// and appends their positions to a list, without branching on what it finds. Stage 2
// walks the list: copy the text since the last reference (32 bytes unconditionally, more
// in a loop), then decode. The next position is known before the current reference
// is decoded, so consecutive references do not wait on each other.
// - Inputs without `&` are returned in place; nothing is copied.
// - A name of up to 7 bytes ending in `;` is looked up without measuring it: the first `;`
// in the next 8 bytes is found with a SWAR zero-byte test, the bytes up to it are the key,
// and one multiply indexes an open-addressed table of 16-byte entries (key, value, length).
// A hit proves the name; legacy and common names sit in their home slot.
// - Longer names go through a second table and one masked 32-byte vector compare.
// - Without `;` only the 106 legacy names match. A filter on the first two bytes gives the
// longest legacy name that could start there, usually none.
// - Numeric references of up to 7 digits, decimal or hexadecimal, share one branch-free
// SWAR path, and the code point is encoded as UTF-8 without branching on its length.
// - Output and input share one buffer (as in the leading C and Nim entries): the output is
// written from the buffer's start and the input sits IN_OFF bytes in. A reference grows
// by at most a fifth (`≫⃒` is 5 bytes in, 6 out), so the write position always stays
// at least 64 bytes behind the next unread byte and decoding in place is safe. The
// buffer holds INPUT_CAP bytes of input, the most the official suite and benchmark send.
// The output region is larger than the output needs; before the first render it is the
// unpacker's working memory (see `init`).
// - The lookup tables are built on the first call by range-decoding a 6.5 KB packed entity
// list (see `sym` and `init`, and tools/gen_entities.py, the matching packer): entities
// in code-point order, each name coded character by character with an adaptive binary
// range coder whose per-bit probability is the logistic mix of an order-1, an order-2
// and a match model, as in the PAQ compressors. The model lives in the buffer's start,
// which is still zero on the first call.
//
// Build (or just run `zig build` / `zig build submit` in this repo):
// zig build-exe this-file.zig -target wasm32-freestanding
// -mcpu=generic+simd128+bulk_memory -O ReleaseSmall -fno-entry -rdynamic -fstrip
// --stack 2048 --initial-memory=327680 --max-memory=327680
// then: wasm-opt -O3 --enable-simd --enable-bulk-memory
const std = @import("std");
const V = @Vector(16, u8);
// The most the official suite and benchmark send; the player refuses larger inputs.
const INPUT_CAP: usize = 104_000;
// One buffer: the output is written from its start and the input sits IN_OFF bytes in, so
// the output follows the input through the same memory (see the header comment). Before
// the first render the buffer's start is the unpacker's working memory, laid out as:
// model (2^MODEL_BITS u16), squash (4095 u16), stretch (4096 i16), hist (HIST_SIZE u8).
const MODEL_BYTES = (1 << MODEL_BITS) * 2;
const SQUASH_OFF = MODEL_BYTES;
const STRETCH_OFF = SQUASH_OFF + 4095 * 2;
const HIST_OFF = STRETCH_OFF + 4096 * 2;
// Where the input starts: past the unpacker's memory (the input is written before the
// first render unpacks) and past the largest growth (a fifth plus 64 bytes), rounded up
// to a multiple of 64.
const IN_OFF: usize = (@max(HIST_OFF + HIST_SIZE, INPUT_CAP / 5 + 64) + 63) / 64 * 64;
const OUTPUT_CAP: usize = INPUT_CAP / 5 * 6 + 8;
const PAD = 128;
comptime {
if (IN_OFF % 64 != 0) @compileError("IN_OFF must be a multiple of 64");
if (IN_OFF < INPUT_CAP / 5 + 64) @compileError("the output must stay behind the input");
}
const FAST_BITS = 12;
const FAST_TAIL = 32;
// Names of at most this many bytes (plus their ';') live in the fast table; longer
// names go to the long-name table. All legacy (semicolon-less) names fit here too.
const FAST_MAXLEN = 7;
const LEG_BITS = 8;
const LEG_TAIL = 16;
const LONG_BITS = 11;
const LONG_MASK = (1 << LONG_BITS) - 1;
const EMPTY: u64 = ~@as(u64, 0);
// Bytes of input per stage-1 window; a multiple of 64. The window's position list is also
// the unpacker's `last` table on the first call (as in the 4-page C entry).
const WINDOW = 2048;
const ONES: u64 = 0x0101010101010101;
const HIGH: u64 = 0x8080808080808080;
// The multiplier was searched offline (see the repo's tools/) so that the common
// entity names sit in their home slot even at the smaller table's 72% load.
const FAST_MUL: u64 = 0xa59d054b12f5b905;
const LEG_MUL: u64 = 0xf880d97d1854e551;
const LONG_MUL: u64 = 0xa0c2995f40498cb3;
const ENTITY_COUNT = 2125;
const LONG_COUNT = 506;
const packed_list = [6699]u8{
222,225,76,127,180,18,170,54,193,110,91,248,93,113,69,179,239,54,43,111,203,206,237,70,
76,133,106,113,86,208,4,143,50,11,77,134,144,94,70,141,120,113,243,229,192,238,161,251,
123,238,10,65,68,73,4,7,224,3,94,225,107,13,36,186,230,234,35,205,158,90,193,42,
160,246,118,232,66,83,107,223,176,186,194,55,247,169,239,169,125,224,237,2,141,189,171,103,
108,141,120,224,7,153,245,108,129,248,80,238,208,72,44,235,208,74,82,77,51,15,226,255,
155,252,118,42,188,217,41,19,156,136,127,67,215,248,149,166,133,198,216,145,130,152,92,65,
114,18,87,41,95,33,159,163,211,124,148,114,115,121,26,31,200,219,94,241,128,46,57,189,
117,227,113,28,5,255,61,46,63,201,188,62,26,137,48,236,162,205,234,155,106,82,22,226,
176,0,81,27,190,65,118,1,15,80,34,231,133,38,21,36,21,243,4,28,253,27,209,84,
73,128,86,69,196,165,205,121,162,11,229,73,40,3,17,134,176,14,179,71,236,191,147,146,
157,189,163,252,10,190,14,191,14,26,172,179,237,4,8,21,133,41,196,99,236,89,67,255,
213,218,79,163,25,195,128,3,231,17,182,111,16,42,178,135,39,200,150,137,106,56,171,44,
137,87,47,75,29,79,239,114,127,131,29,68,136,185,67,193,1,62,201,136,188,192,67,16,
65,88,24,102,49,177,157,200,98,95,203,96,11,252,85,177,51,210,212,238,178,116,159,200,
13,67,175,98,91,248,202,48,19,24,116,126,117,222,250,51,31,54,113,80,113,32,249,120,
79,77,173,222,87,188,130,22,227,229,77,0,203,93,185,253,70,181,206,221,84,197,228,137,
248,210,19,90,42,78,166,105,244,122,21,149,174,84,4,188,191,3,35,66,92,33,119,158,
157,29,99,205,237,223,155,124,113,206,1,164,39,182,103,24,94,98,139,88,203,1,5,6,
245,132,18,82,186,229,142,184,218,158,52,47,150,168,152,122,223,144,114,194,43,6,126,87,
40,112,133,111,38,101,167,9,134,8,182,254,2,15,35,95,223,8,38,160,148,24,169,60,
224,98,88,27,73,188,8,236,83,191,60,217,252,180,72,136,215,17,222,22,19,182,61,170,
7,184,2,80,18,54,24,117,127,169,105,202,125,168,55,112,225,43,249,113,25,27,116,226,
224,99,206,118,174,154,109,132,73,76,191,213,104,154,200,113,234,25,47,161,186,95,154,205,
230,157,92,171,34,136,176,178,196,12,1,84,37,247,214,237,78,156,203,37,156,89,224,29,
224,10,38,122,14,239,232,76,137,88,107,233,41,31,117,87,182,75,12,9,245,187,4,121,
4,196,190,198,27,213,122,48,172,38,222,59,242,142,8,183,255,237,141,105,160,38,229,244,
1,13,66,183,202,255,87,106,157,247,147,138,207,74,211,53,88,118,232,69,52,179,83,174,
218,33,36,213,41,34,48,155,59,16,41,133,134,43,134,127,152,120,150,34,87,202,46,175,
82,62,153,230,200,113,237,134,181,190,23,172,41,181,76,3,212,235,85,189,187,88,130,49,
202,182,232,79,189,40,224,6,203,247,251,184,4,177,227,28,113,117,139,108,83,160,88,134,
164,199,119,75,172,218,138,79,252,252,164,176,171,59,44,244,140,126,173,170,127,185,108,30,
196,143,209,234,60,101,14,200,10,198,138,180,117,111,137,209,210,34,250,217,42,94,85,53,
116,88,245,41,150,196,99,94,102,195,5,18,240,79,56,51,160,207,228,197,243,103,227,126,
59,112,79,63,248,159,10,169,183,104,70,56,132,144,16,118,95,57,149,147,144,67,189,231,
131,91,20,218,39,94,145,172,130,170,249,185,43,102,33,67,61,73,195,135,242,29,4,96,
228,212,78,244,37,233,226,117,66,112,58,37,151,31,19,4,88,81,170,106,60,22,2,191,
190,177,205,36,32,13,154,100,26,193,76,143,175,167,34,236,162,17,130,130,250,25,202,211,
36,142,143,185,170,233,123,68,174,27,224,126,106,36,1,100,186,68,208,61,10,215,176,98,
141,96,61,73,201,56,214,146,197,226,215,213,195,148,105,233,53,237,63,79,59,80,8,245,
178,207,109,205,249,237,67,77,240,20,111,110,117,160,118,142,181,77,135,161,68,40,248,248,
178,171,224,223,181,188,67,156,249,204,192,74,20,113,195,28,189,105,24,212,39,122,126,250,
74,25,115,240,155,172,114,162,210,170,156,126,127,94,192,71,54,152,26,118,94,194,150,99,
1,199,185,236,228,178,62,188,159,68,55,193,255,225,72,62,214,160,252,176,131,176,80,255,
240,160,99,253,70,229,6,160,216,190,201,195,69,154,30,19,234,14,62,137,17,86,32,246,
93,74,153,244,11,35,149,53,164,146,10,199,8,25,13,90,78,245,14,239,130,137,145,248,
91,139,182,230,130,124,216,122,16,157,66,93,159,112,55,33,192,83,145,93,238,213,154,218,
134,43,41,157,218,24,58,52,213,82,178,232,160,33,30,165,179,148,136,53,38,24,75,87,
211,33,65,89,77,10,190,57,111,57,203,78,47,26,224,43,197,92,178,252,85,103,232,192,
240,39,149,61,155,12,130,141,115,71,192,252,73,228,185,158,123,164,155,214,163,190,109,109,
99,191,17,211,225,214,245,57,188,84,126,72,118,208,250,8,118,188,197,4,21,53,148,188,
125,242,122,22,218,37,252,236,232,82,68,78,237,40,235,124,116,21,106,194,214,142,69,77,
99,179,155,20,27,74,240,246,1,116,252,150,136,92,152,147,72,110,186,131,120,89,96,254,
229,57,77,245,124,98,74,109,232,126,199,174,214,120,36,134,18,206,23,92,142,253,98,202,
14,97,239,190,244,216,62,13,72,188,247,242,219,80,35,5,185,131,87,46,131,191,71,227,
161,110,41,169,206,74,111,4,250,201,171,132,186,64,135,145,217,150,25,158,160,241,120,52,
37,154,144,215,59,62,223,59,103,50,114,78,255,154,125,6,16,38,102,200,28,146,204,31,
179,3,248,70,210,155,118,145,153,229,194,154,222,65,101,217,141,22,202,74,217,113,73,66,
106,172,13,189,202,166,214,164,76,121,26,246,9,178,184,94,59,199,192,152,170,143,114,111,
193,253,184,41,98,100,161,222,102,76,28,231,202,226,119,91,130,163,163,178,188,216,131,199,
223,69,38,8,206,253,19,29,204,252,210,25,113,44,147,127,85,92,61,27,116,228,1,94,
55,151,58,42,123,62,81,253,234,154,59,239,67,167,227,87,245,246,137,183,58,213,13,73,
50,35,58,217,51,153,192,80,129,75,130,9,242,130,214,134,139,100,178,232,68,78,195,40,
15,158,166,254,160,33,103,52,195,150,57,83,136,139,34,194,220,108,81,206,217,140,22,208,
69,145,37,168,247,28,65,28,158,206,210,255,164,192,36,192,228,55,186,244,249,121,52,222,
134,232,133,1,122,132,14,253,4,117,7,75,109,201,70,16,118,209,214,82,52,175,174,176,
235,245,71,223,191,199,239,205,168,43,108,11,80,81,202,118,68,89,119,28,35,95,174,38,
192,44,211,19,163,223,37,90,165,162,199,98,156,104,64,188,251,27,229,238,161,170,104,1,
87,122,228,17,51,93,105,137,118,144,10,245,241,140,240,227,69,144,27,220,76,121,69,151,
109,53,198,216,193,11,52,28,113,191,233,63,88,20,196,245,254,126,140,185,49,80,153,138,
148,186,133,171,122,226,175,5,209,196,229,22,156,97,233,161,127,122,239,62,34,49,143,148,
99,182,84,231,86,4,26,145,90,117,8,234,36,179,5,123,73,110,65,251,26,46,56,213,
164,74,246,81,222,43,144,2,138,216,235,217,222,130,167,80,206,94,63,144,152,12,128,189,
183,243,5,193,24,237,134,21,237,117,240,208,227,202,102,19,121,143,108,126,31,143,22,84,
79,90,216,153,71,63,231,13,13,201,246,164,144,65,135,66,66,167,149,170,224,229,240,85,
42,33,175,135,254,189,87,162,17,135,170,156,245,164,29,254,127,207,65,89,147,153,51,246,
185,214,194,13,113,45,195,233,250,149,136,36,249,95,242,210,62,168,214,143,183,17,139,23,
101,90,91,192,40,204,142,34,110,85,146,251,241,102,146,122,45,170,76,64,190,111,47,12,
196,206,235,119,77,117,139,63,195,29,78,200,100,252,100,26,164,177,100,255,57,185,144,213,
175,75,174,17,232,5,6,59,7,68,5,2,103,231,52,187,27,103,244,239,122,242,199,38,
19,236,48,200,185,48,141,49,73,56,187,26,41,14,229,170,55,234,29,165,0,68,97,127,
91,109,157,26,87,131,76,206,112,171,228,64,233,126,38,146,240,76,186,85,39,146,28,144,
107,127,238,65,67,181,165,89,76,4,214,232,155,208,109,170,0,7,66,127,158,87,98,76,
249,234,141,253,83,150,182,61,211,124,64,116,87,3,142,234,6,139,165,8,201,63,110,44,
110,68,228,81,162,211,63,238,157,129,204,173,11,74,167,1,227,109,189,222,160,255,249,255,
120,226,187,206,136,63,211,80,251,241,185,126,21,51,186,70,231,191,113,20,173,47,11,27,
42,45,31,13,241,108,67,200,75,91,112,87,121,211,239,204,67,152,30,13,164,226,200,129,
8,75,208,202,233,232,159,231,112,235,116,37,156,42,216,64,81,254,112,18,160,13,88,228,
160,194,147,232,195,201,168,229,164,36,157,97,89,30,243,193,85,55,125,153,223,155,138,187,
169,152,186,103,236,231,186,85,61,235,87,19,91,178,197,119,103,200,124,56,98,156,110,101,
183,76,163,105,252,35,46,236,153,135,136,185,134,178,97,49,238,24,218,123,32,63,209,70,
81,60,126,19,77,150,193,236,232,176,249,50,15,5,151,150,47,218,71,7,56,95,90,207,
180,197,61,59,158,26,24,31,39,173,72,75,116,241,239,29,105,161,53,72,241,214,125,108,
81,14,4,121,35,74,51,195,29,86,209,21,144,166,74,67,190,102,216,154,211,197,125,253,
11,239,216,25,95,240,146,22,26,142,155,193,38,241,74,191,145,14,95,22,96,107,205,235,
234,56,233,93,209,80,102,138,123,109,33,70,68,26,98,67,6,189,63,111,81,120,237,135,
13,62,165,173,50,173,62,9,84,86,219,221,213,203,139,11,139,46,236,95,32,202,57,18,
37,36,20,233,252,23,7,33,174,200,9,242,100,72,231,97,161,156,90,82,143,127,185,35,
122,79,133,6,30,80,113,52,235,90,46,255,93,124,234,180,97,227,101,208,214,169,182,91,
136,136,18,12,196,36,0,59,109,43,36,242,73,165,214,93,12,1,214,95,134,240,148,173,
125,28,40,133,69,101,37,230,31,146,172,81,153,49,115,196,125,201,160,120,244,26,229,204,
180,156,181,77,149,148,165,17,85,151,132,89,128,11,237,226,168,76,228,212,210,140,248,105,
231,144,6,156,138,250,87,27,57,70,20,96,113,128,194,207,105,87,238,28,61,147,158,140,
90,93,219,65,2,118,112,246,182,117,181,169,210,151,144,213,207,217,180,235,33,126,58,102,
137,56,86,14,103,29,131,158,208,13,58,223,146,33,243,198,116,236,29,70,9,229,106,15,
190,238,227,229,196,172,2,55,254,18,252,216,37,45,192,204,125,137,222,59,88,8,246,10,
136,110,154,22,221,34,24,56,155,254,13,63,8,105,93,1,156,178,45,219,36,59,208,73,
241,14,84,75,180,52,83,109,35,83,177,112,63,9,224,197,192,122,220,93,70,224,219,5,
61,131,28,99,65,249,85,121,213,85,83,180,198,85,21,0,209,103,125,99,88,106,97,244,
40,115,252,230,252,149,107,211,2,218,88,76,160,72,10,51,15,63,28,116,40,201,15,103,
60,83,252,219,245,105,172,243,13,200,177,213,40,65,65,188,205,231,226,241,59,170,9,113,
217,177,169,170,169,201,138,104,94,79,87,114,44,253,87,5,99,224,10,102,156,91,102,241,
63,231,68,184,4,34,149,173,82,19,232,140,223,30,105,200,253,188,94,197,23,121,217,245,
70,64,141,17,76,189,174,250,9,115,109,161,23,43,171,41,99,10,37,148,5,150,118,176,
139,200,5,163,96,204,55,79,70,181,43,32,113,119,103,41,138,185,79,23,95,223,169,233,
136,122,111,129,30,130,111,190,24,241,8,186,86,19,209,252,196,168,138,177,204,60,216,181,
12,254,3,114,57,81,117,125,21,213,51,91,137,127,111,66,2,254,98,33,46,133,129,192,
83,219,28,253,112,247,218,45,176,208,18,221,220,201,179,136,145,34,166,199,135,24,146,59,
136,77,95,45,19,170,136,210,212,253,109,28,68,202,30,161,130,98,208,123,149,35,55,231,
246,48,98,33,112,85,85,74,209,111,136,63,193,112,99,238,28,220,94,189,37,53,20,98,
154,152,75,3,168,131,145,85,47,155,147,75,61,81,188,254,138,13,241,233,46,234,254,193,
229,119,102,195,145,227,122,59,171,213,78,141,214,127,132,66,240,57,47,47,117,55,88,206,
243,220,150,184,106,94,194,3,75,40,46,86,0,233,85,204,138,33,252,103,64,138,141,231,
131,50,179,171,101,218,223,32,95,158,100,232,158,106,53,80,192,120,44,226,132,213,245,160,
158,18,167,125,227,225,84,95,47,163,110,43,79,24,245,92,51,123,202,69,207,92,240,199,
107,253,67,125,191,215,138,115,175,187,227,229,131,160,202,31,60,37,48,72,209,64,192,151,
49,34,153,162,29,59,115,213,223,59,100,244,191,106,51,199,87,230,78,71,103,173,228,97,
44,113,130,253,234,128,3,19,48,2,12,32,191,39,58,213,165,6,39,210,201,24,133,215,
110,154,177,32,174,131,55,117,162,24,89,36,148,8,85,39,137,208,73,51,85,246,154,252,
36,6,250,138,137,176,212,42,39,212,247,230,113,17,95,41,79,89,27,204,95,59,251,201,
23,136,5,117,105,79,185,110,112,71,182,12,163,178,53,16,194,180,166,56,79,92,250,126,
18,177,230,175,143,110,143,251,194,184,232,1,67,129,179,81,39,50,9,64,254,175,162,68,
44,66,193,189,193,36,206,109,192,2,97,119,153,244,101,31,105,65,162,4,235,10,71,144,
246,80,169,0,193,214,77,82,205,36,106,73,57,54,137,109,184,10,246,239,186,243,188,122,
253,182,30,211,189,252,103,215,99,238,211,106,118,78,21,128,179,4,255,87,182,165,136,40,
229,64,48,81,174,145,231,150,118,108,27,37,101,117,4,157,105,151,180,115,110,13,101,4,
95,181,138,16,186,195,119,88,216,16,56,243,8,132,169,234,191,81,44,51,2,143,224,111,
216,218,68,234,29,89,203,232,44,194,53,135,199,221,251,74,165,176,12,190,100,181,186,36,
132,5,78,138,116,57,88,62,15,28,102,180,87,197,247,115,248,172,138,23,61,131,58,198,
95,171,107,55,19,0,48,136,246,90,177,218,227,102,67,23,255,124,100,58,145,248,34,214,
29,67,94,14,175,9,24,166,190,179,236,201,199,101,249,74,149,202,5,42,139,44,36,170,
127,28,5,103,79,36,81,62,197,60,194,196,2,148,113,213,83,1,89,22,128,181,68,121,
109,29,192,186,3,60,87,230,190,112,162,106,16,148,50,153,141,73,189,149,235,41,86,66,
71,81,226,6,8,20,174,74,188,198,38,131,215,180,242,101,159,40,193,34,194,191,248,215,
73,32,238,203,82,242,238,116,242,36,134,188,138,87,216,194,151,249,88,161,237,79,231,198,
192,127,30,211,164,18,117,190,208,2,40,29,132,147,116,95,43,28,103,152,86,105,150,62,
1,110,2,56,245,210,241,19,38,90,219,126,47,195,189,164,234,139,218,237,21,122,146,175,
188,10,167,224,130,11,93,149,144,111,41,109,138,133,160,233,153,114,148,20,49,151,182,41,
34,233,66,153,1,159,230,249,223,14,213,14,68,224,148,140,115,10,95,4,179,133,173,78,
21,81,53,3,220,68,183,72,13,92,182,50,215,93,178,176,192,238,45,103,111,247,33,16,
182,76,119,141,231,21,2,22,146,167,190,245,85,35,133,102,52,98,37,95,185,194,65,97,
148,140,119,126,131,138,71,159,223,41,175,46,230,36,204,22,13,218,248,38,187,251,255,215,
16,254,60,124,102,59,230,110,254,84,51,253,180,108,78,225,116,193,15,131,199,141,153,126,
130,57,20,30,117,102,104,42,50,200,73,63,9,151,198,27,244,196,89,58,202,184,139,62,
211,164,68,91,211,230,194,38,173,118,7,38,103,192,57,0,224,241,222,2,94,113,58,13,
101,170,19,175,253,157,93,204,89,169,142,229,190,174,175,158,47,240,155,111,17,110,101,82,
138,108,178,188,95,230,124,209,247,73,221,116,81,5,131,198,54,45,25,162,142,168,64,66,
139,167,60,196,241,48,146,44,34,58,195,242,50,220,190,174,190,46,4,135,67,11,15,224,
191,218,197,58,14,134,2,241,233,122,6,127,188,77,154,84,22,106,55,252,239,0,189,51,
190,216,252,222,190,71,171,55,51,233,40,180,158,236,252,221,104,47,71,114,45,255,91,39,
39,255,94,79,94,204,149,8,139,210,26,225,124,128,247,194,13,218,72,56,91,237,96,40,
225,15,48,141,102,221,38,159,100,25,233,218,208,236,180,225,147,155,245,96,185,95,157,22,
38,189,241,113,187,124,11,237,7,29,165,219,116,152,92,79,113,131,116,56,251,170,20,236,
35,22,149,111,116,226,79,99,6,127,125,221,149,55,143,79,165,100,57,155,226,220,141,120,
234,138,50,175,32,202,133,91,71,54,135,106,176,235,57,244,107,71,45,165,36,238,72,222,
134,98,109,43,203,189,126,209,2,179,76,212,77,137,85,64,184,70,226,203,10,63,227,249,
204,41,206,104,218,22,228,214,66,236,79,81,76,46,240,122,1,200,153,220,39,207,40,204,
9,152,130,146,133,164,235,46,223,147,99,107,44,246,152,190,219,185,240,107,31,93,254,245,
10,39,61,27,29,57,217,244,94,178,55,113,145,109,253,194,166,178,105,5,20,130,240,170,
88,174,178,239,192,80,180,59,147,65,198,212,217,111,86,108,174,138,47,125,101,113,54,193,
123,166,250,118,29,253,105,11,86,197,42,179,42,215,87,234,56,203,212,35,36,122,2,201,
59,147,54,189,93,208,175,203,113,226,39,197,104,173,46,28,183,201,24,109,184,164,240,126,
85,170,192,223,132,78,241,75,142,221,242,63,145,229,60,108,137,53,253,99,36,178,41,204,
121,195,128,153,178,237,106,80,15,126,165,136,241,34,98,129,205,225,85,111,255,69,28,199,
103,173,24,59,217,130,54,144,4,177,10,84,186,120,231,72,126,245,196,173,230,112,245,41,
134,231,64,167,129,117,107,125,206,33,130,51,197,98,191,174,36,125,191,118,121,48,126,49,
208,39,19,204,21,192,140,101,78,173,3,108,1,80,185,202,134,176,102,201,194,22,22,184,
231,79,215,21,251,164,70,237,137,119,173,240,193,82,157,27,237,34,46,11,239,190,0,204,
251,193,250,196,36,128,53,11,236,212,92,208,128,84,8,126,83,148,130,67,177,186,127,21,
51,38,238,211,90,84,191,170,253,183,169,87,146,14,60,236,43,80,48,111,83,32,93,149,
198,204,155,169,133,81,184,226,11,128,169,64,49,129,198,81,146,9,215,140,141,216,70,125,
114,130,191,197,130,150,28,114,27,55,81,200,240,194,105,203,188,61,75,74,231,18,176,54,
178,215,7,133,73,23,126,224,7,88,117,79,189,104,47,167,244,220,113,239,128,178,108,204,
140,255,77,79,56,129,190,95,10,9,16,26,11,69,128,59,60,40,45,162,48,57,247,45,
179,61,192,83,136,102,78,147,28,218,107,225,38,228,230,25,138,141,227,204,3,128,76,7,
253,92,113,20,89,182,158,165,215,48,208,121,206,220,217,228,221,120,50,84,81,183,118,115,
50,8,160,84,2,184,163,207,179,200,202,52,13,216,109,103,94,215,166,146,57,173,28,191,
136,92,225,223,241,230,126,83,118,171,243,46,51,169,177,181,105,96,35,54,253,231,86,72,
64,33,31,169,63,7,4,49,102,244,34,146,88,166,192,211,134,185,83,181,219,186,167,153,
141,6,152,130,119,203,77,221,244,238,41,74,130,180,20,173,105,130,92,224,136,171,215,139,
207,123,94,156,248,197,202,100,15,156,185,54,116,64,142,154,184,31,214,221,73,44,106,223,
168,63,20,171,11,140,132,239,214,43,150,42,91,132,237,188,70,222,93,72,19,230,47,72,
231,76,119,120,160,239,124,101,228,7,92,31,149,237,2,15,136,208,106,32,17,114,76,54,
177,29,86,80,62,152,202,69,120,207,184,88,71,57,112,81,241,20,120,228,45,11,86,106,
176,104,15,22,188,167,119,76,80,186,123,84,219,68,16,55,157,145,46,47,193,59,123,189,
184,173,156,108,122,244,103,99,55,8,61,106,93,47,35,23,182,68,146,67,211,180,60,107,
109,110,6,165,230,59,231,201,219,75,53,28,170,197,113,151,160,179,90,107,241,107,179,151,
106,102,180,25,234,32,214,130,18,169,99,124,64,6,24,74,159,81,192,7,3,72,13,168,
201,68,39,194,237,127,50,141,178,215,66,240,157,3,54,16,160,75,46,212,106,63,246,79,
113,13,165,129,254,51,110,190,180,229,195,45,222,167,19,246,143,47,76,238,194,105,60,58,
62,74,174,31,23,226,177,203,144,22,198,192,133,185,36,185,78,206,116,243,9,149,108,125,
252,193,36,65,175,78,212,115,192,92,61,247,158,146,115,69,182,88,54,191,209,110,218,44,
202,29,216,174,100,243,176,5,182,55,33,172,220,27,97,53,138,69,84,62,177,242,168,227,
72,132,164,9,180,155,254,36,126,74,133,190,200,7,33,237,140,140,43,211,158,245,233,102,
180,171,199,78,118,101,129,32,85,28,23,103,182,2,219,153,21,122,137,6,148,69,62,77,
31,143,163,209,21,162,113,215,148,115,165,169,249,172,43,121,30,234,56,51,146,66,44,76,
50,17,214,32,141,255,203,93,188,64,172,249,160,116,16,222,189,0,43,83,21,232,117,21,
5,95,19,53,76,205,164,117,212,104,54,36,177,67,52,57,98,205,240,120,248,108,173,151,
209,240,101,131,10,64,169,146,142,212,66,174,79,11,126,222,223,187,239,140,179,66,191,26,
197,184,121,143,41,238,219,19,56,132,43,48,177,241,196,148,254,48,196,183,66,58,167,82,
63,54,62,216,70,184,56,216,94,65,226,236,84,29,126,218,170,70,42,129,61,114,22,42,
129,208,209,124,198,36,29,44,78,167,116,62,81,240,39,203,129,240,145,149,147,228,75,192,
212,104,166,24,122,148,240,205,128,65,83,236,54,131,236,81,238,162,136,190,104,46,147,226,
174,173,23,104,220,120,17,169,172,84,149,210,168,80,103,125,208,49,250,196,84,211,66,196,
219,229,121,11,124,250,228,226,56,218,251,142,228,206,43,130,224,31,53,152,169,53,130,101,
50,77,76,170,106,94,173,237,71,98,82,179,37,227,36,69,248,52,168,13,97,136,195,63,
196,73,8,10,137,26,154,148,155,54,131,140,249,251,201,107,29,191,179,49,33,3,214,16,
9,232,78,106,250,204,189,161,235,173,37,168,227,136,56,250,183,210,247,252,116,143,121,90,
248,225,27,187,5,164,71,112,163,74,114,134,169,159,47,94,159,240,57,123,138,29,166,225,
73,227,220,97,110,16,98,176,179,58,84,114,10,134,181,210,250,66,156,29,128,80,249,37,
194,45,158,75,15,171,32,200,145,129,24,137,216,190,87,178,91,29,2,125,93,241,169,213,
83,34,40,180,182,105,163,80,100,93,141,163,130,43,70,177,229,176,100,164,98,28,138,97,
173,17,128,53,77,90,49,130,124,172,71,127,74,43,236,25,252,30,22,151,229,166,102,76,
131,69,138,118,243,29,155,12,161,10,64,95,209,170,151,228,107,91,11,137,8,19,161,141,
252,251,65,7,36,215,20,82,241,112,153,240,162,175,169,49,61,5,255,223,57,157,78,68,
98,187,13,137,14,149,55,250,78,62,14,103,220,244,1,191,167,174,238,176,159,232,102,137,
254,5,249,126,152,41,232,67,19,128,177,230,199,232,66,253,197,16,54,135,105,47,145,36,
232,196,53,180,198,14,250,7,43,64,178,135,136,141,105,190,102,50,163,126,148,109,127,39,
105,230,163,222,67,3,137,62,146,232,125,193,87,241,186,197,15,24,201,218,42,136,59,171,
63,51,192,75,107,117,143,24,151,110,135,206,255,68,179,155,245,251,187,30,173,195,158,199,
189,76,67,162,141,11,89,48,134,31,236,236,43,225,131,142,120,73,78,99,78,132,156,123,
31,157,50,249,74,36,15,242,228,127,221,99,59,82,159,75,239,132,15,59,13,136,242,46,
131,141,77,149,248,159,74,97,66,174,237,50,66,107,67,20,121,169,116,188,0,238,182,45,
110,224,103,35,78,59,255,187,76,232,54,236,39,110,111,208,6,109,159,230,103,192,199,131,
88,155,30,86,184,145,136,129,129,3,75,6,105,200,6,85,220,53,235,151,29,163,145,118,
50,90,209,192,49,58,27,81,233,216,252,242,244,151,208,57,93,6,85,9,70,83,113,177,
217,126,131,246,45,111,58,184,90,119,195,195,177,29,36,102,201,198,226,103,37,207,42,87,
65,170,127,71,234,252,144,169,233,213,73,43,22,74,101,219,168,45,160,40,53,24,119,164,
134,217,66,178,133,139,63,222,35,62,54,98,164,49,130,238,42,137,48,165,254,215,213,241,
103,113,120,172,155,191,119,94,193,104,217,12,6,75,96,8,156,89,153,235,251,47,137,144,
71,41,178,22,245,251,33,114,236,190,58,232,166,151,233,191,10,250,40,80,0,228,78,247,
149,213,146,146,197,199,131,210,164,145,80,218,144,105,82,169,219,174,191,195,138,37,103,8,
105,14,40,114,114,0,129,35,20,118,47,39,68,67,63,93,253,103,80,102,170,26,159,213,
173,167,138,118,180,32,77,193,128,108,3,204,218,108,146,240,162,253,190,15,168,159,97,71,
182,38,235,103,175,178,17,28,236,165,41,36,37,162,51,54,121,109,168,138,38,150,195,112,
139,157,66,146,238,134,129,118,240,242,96,196,72,179,226,60,84,54,242,73,80,57,167,110,
66,199,4,241,208,228,113,216,177,38,242,241,166,18,9,125,30,13,37,167,144,37,162,106,
207,85,4,78,208,143,85,41,123,183,246,243,233,234,48,25,12,64,234,1,141,160,75,214,
186,252,52,223,242,220,86,241,134,15,139,140,111,34,251,244,233,39,70,43,114,157,100,225,
60,184,30,105,76,141,78,186,135,220,71,94,229,107,1,45,50,62,112,79,124,55,69,61,
222,183,132,45,32,251,108,41,231,79,50,119,193,113,166,132,155,205,143,79,245,81,50,193,
118,51,117,108,198,98,220,128,136,114,240,204,99,112,197,108,42,147,18,179,84,241,2,171,
84,182,39,114,26,79,2,226,123,9,167,68,104,194,156,49,46,27,224,105,100,23,226,8,
114,207,185,175,128,161,174,75,222,16,253,78,67,127,118,155,240,162,188,71,111,134,3,139,
207,89,155,179,118,164,23,110,148,162,34,47,141,103,190,113,193,119,91,58,220,20,77,18,
233,254,209,37,136,100,178,30,66,234,123,245,65,132,196,209,132,173,33,178,192,47,42,23,
230,138,143,140,219,200,63,181,9,176,197,189,100,16,77,137,158,173,145,21,179,203,116,111,
132,193,166,143,138,104,24,215,228,45,155,0,181,167,14,197,166,112,86,169,113,115,112,247,
5,43,213,104,19,73,107,30,224,95,106,158,214,83,148,207,189,187,21,246,78,24,223,28,
163,251,107,18,159,162,25,171,162,201,12,93,206,39,70,69,207,17,52,127,225,121,176,227,
118,212,24,182,72,183,116,66,57,178,196,245,134,70,12,54,250,60,182,23,46,5,9,245,
228,90,97,145,112,151,113,199,186,26,249,125,25,43,155,158,16,218,132,25,236,226,112,129,
146,61,48,54,44,132,127,76,35,45,126,175,81,17,249,255,217,243,96,194,16,50,16,77,
179,37,64,242,198,75,237,94,169,54,66,16,152,43,242,89,216,101,27,180,81,28,216,105,
14,89,15,97,56,64,11,80,247,84,222,254,110,3,46,179,120,206,53,136,193,4,222,42,
44,64,198,15,109,243,197,149,17,152,40,148,127,79,233,158,118,59,49,171,208,173,48,131,
82,15,186,246,162,149,103,111,103,103,101,225,137,95,60,153,244,94,173,215,144,8,19,172,
186,26,162,158,184,26,34,163,54,90,68,15,43,78,22,242,81,187,82,101,43,45,40,135,
247,155,47,156,74,132,122,180,13,8,91,190,61,164,41,80,227,94,82,32,62,28,81,166,
93,19,136,146,108,130,5,187,1,30,11,16,120,92,117,97,248,227,242,243,232,137,86,165,
28,168,42,90,252,23,112,129,157,255,250,193,81,75,229,94,117,46,77,113,218,194,45,38,
63,196,182,243,255,249,160,251,154,142,186,175,162,11,233,253,79,31,168,215,145,188,222,191,
163,25,163,202,163,126,203,44,173,180,224,204,90,57,79,58,102,219,224,138,141,10,165,89,
106,0,107,92,178,45,41,119,226,29,160,157,150,198,102,188,196,14,74,14,135,233,136,237,
149,213,132,161,233,147,224,103,229,124,105,38,18,172,9,39,14,150,30,112,83,112,124,166,
37,38,40,158,32,172,24,195,34,229,99,117,105,213,153,186,160,39,66,117,219,128,164,162,
69,75,203,94,168,180,134,47,69,166,63,255,33,132,38,84,241,229,29,180,174,79,206,148,
149,237,12,193,44,86,240,227,246,30,106,243,137,129,125,123,122,197,17,197,234,218,33,133,
126,78,65,5,6,2,178,105,10,48,39,52,245,167,206,19,98,99,25,115,1,155,94,156,
104,21,187,46,181,125,207,114,166,109,89,183,71,75,226,210,43,32,190,
0,0,0,0,0,0,0,0,
};
// The range coder's parameters; gen_entities.py must agree.
const MODEL_BITS = 14; // 16,384 16-bit entries: the most that fits before the input
const MODEL_LIMIT = 3;
const MIX_SHIFT: u5 = 11;
const MIX_W0: i32 = 16384;
const K: u32 = 1 << 20;
const HASH_A: u32 = 0x2545F491;
const HASH_B: u32 = 0x9E3779B1;
const HIST_SIZE = 16_130;
// e^(2047 / 256) and e^(-1 / 256) as the bits of a float64.
const SQUASH_E0: u64 = 0x40a732ac33de5528;
const SQUASH_STEP: u64 = 0x3fefe00ffaabffbc;
// Step toward the bit just seen, as a 16-bit fraction: 2 / (2n + 3) after n uses.
const rate = [4]i32{ 43690, 26214, 18724, 14563 };
// The value's UTF-8 bytes in the low bytes, its length in bits 56-58 and, for
// long-name entries, the name's length in bits 59-63.
const Entry = extern struct { key: u64, val: u64 };
const Long = extern struct { name: [32]u8, val: u64 };
// One buffer: the output is written from its start and the input sits IN_OFF bytes in, so
// the output follows the input through the same memory (see the header comment).
var buf align(64) = std.mem.zeroes([IN_OFF + INPUT_CAP + PAD]u8);
const input: [*]u8 = buf[IN_OFF..].ptr;
const output: [*]u8 = buf[0..].ptr;
// The unpacker's tables, inside the buffer's still-zero start on the first call.
const model: [*]u16 = @ptrCast(buf[0..].ptr);
const squash: [*]u16 = @ptrCast(buf[SQUASH_OFF..].ptr);
const stretch: [*]i16 = @ptrCast(buf[STRETCH_OFF..].ptr);
const hist: [*]u8 = buf[HIST_OFF..].ptr;
// The match model's position list, in stage 1's still-zero position list.
const last: [*]u16 = @ptrCast(&pos);
var weights = std.mem.zeroes([3]i32);
var rc_in: u32 = undefined;
var rc_range: u32 = undefined;
var rc_code: u32 = undefined;
var fast: [(1 << FAST_BITS) + FAST_TAIL]Entry = undefined;
var leg: [(1 << LEG_BITS) + LEG_TAIL]Entry = undefined;
var longs: [LONG_COUNT]Long align(16) = std.mem.zeroes([LONG_COUNT]Long);
var long_slots = std.mem.zeroes([1 << LONG_BITS]u16);
// Longest legacy name starting with these two bytes (hashed), or 0.
var leg_max = std.mem.zeroes([4096]u8);
// Stage 1's per-window position list; zeroed, because the unpacker's `last` table lives
// here on the first call.
var pos = std.mem.zeroes([WINDOW + 16]u32);
var ready: bool = false;
// Windows-1252 remapping for references to 0x80-0x9F.
const c1 = [32]u16{
0x20AC, 0x81, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0x8D, 0x017D, 0x8F,
0x90, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, 0x9D, 0x017E, 0x0178,
};
const utf8_marks = [5]u32{ 0, 0, 0x80C0, 0x8080E0, 0x808080F0 };
// Byte j of the 32 loaded at `len_mask + 32 - n` is 0xFF when j < n: 32 bytes of 0xFF, then 32 of 0.
var len_mask align(16) = std.mem.zeroes([64]u8);
inline fn ld(p: [*]const u8) V {
return p[0..16].*;
}
inline fn st(p: [*]u8, v: V) void {
p[0..16].* = v;
}
inline fn ld64(p: [*]const u8) u64 {
return @as(*align(1) const u64, @ptrCast(p)).*;
}
inline fn st64(p: [*]u8, x: u64) void {
@as(*align(1) u64, @ptrCast(p)).* = x;
}
inline fn st32(p: [*]u8, x: u32) void {
@as(*align(1) u32, @ptrCast(p)).* = x;
}
inline fn eqMask(v: V, c: u8) u32 {
return @as(u16, @bitCast(v == @as(V, @splat(c))));
}
inline fn legSlot(w: u64) usize {
return ((@as(u32, @truncate(w)) & 0xFFFF) *% 0x9E3779B1) >> 20;
}
inline fn longHash(head: u64, tail: u64, n: usize) usize {
return @intCast((((head ^ std.math.rotl(u64, tail, 29)) +% n) *% LONG_MUL) >> (64 - LONG_BITS));
}
/// A name of 7 to 31 bytes: probe the long-name table. The hash covers the name and
/// its trailing ';' (the last 7 name bytes plus it), as the unpacker's insertion does.
inline fn longName(src: [*]const u8, i: usize, w: u64, s0: V, s1: V, len: usize, dst: [*]u8, o: *usize) bool {
var h = longHash(w, ld64(src + i + len - 6), len);
const lm: [*]const u8 = &len_mask;
const m0 = ld(lm + 32 - len);
const m1 = ld(lm + 48 - len);
while (true) {
const s = long_slots[h];
if (s == 0) return false;
const l = &longs[s - 1];
const nm: [*]const u8 = &l.name;
const diff = ((s0 & m0) ^ ld(nm)) | ((s1 & m1) ^ ld(nm + 16));
if (l.val >> 59 == len and @reduce(.Or, diff) == 0) {
st64(dst + o.*, l.val);
o.* += @intCast((l.val >> 56) & 7);
return true;
}
h = (h + 1) & LONG_MASK;
}
}
// The entity list is range-coded and read once, on the first call, by `sym` below; see
// tools/gen_entities.py (the matching packer) for the format and the field sequence.
// A code point as UTF-8 bytes in a word, low byte first.
noinline fn utf8(v: u32, len: *u32) u32 {
@branchHint(.cold);
const n: u32 = 1 + @as(u32, @intFromBool(v > 0x7F)) + @intFromBool(v > 0x7FF) + @intFromBool(v > 0xFFFF);
const bits = (v >> 18) | (((v >> 12) & 0x3F) << 8) | (((v >> 6) & 0x3F) << 16) | ((v & 0x3F) << 24);
len.* = n;
return if (n == 1) v else (bits >> @intCast(32 - 8 * n)) | utf8_marks[n];
}
// squash[x + 2047] = 4096 / (1 + e^(-x / 256)) rounded, for x = -2047..2047, and stretch,
// its inverse. Only IEEE multiply, add and divide, so every machine (and the packer)
// gets the same bits.
noinline fn buildSquash() void {
@setFloatMode(.strict);
var e: f64 = @bitCast(SQUASH_E0);
var pi: u32 = 0;
for (0..4095) |x| {
const v: u32 = (@as(u32, @intFromFloat(8192.0 / (1.0 + e))) + 1) >> 1;
squash[x] = @intCast(v);
while (pi <= v) : (pi += 1) stretch[pi] = @intCast(@as(i32, @intCast(x)) - 2047);
e *= @bitCast(SQUASH_STEP);
}
}
// Decode an nbits-bit field, one bit at a time. Each bit's probability is the logistic
// mix of the model entries chosen by the N contexts (field bits use one context, name
// characters three); after the bit is known, each weight moves by error * stretch(p)
// and each entry steps toward the bit.
noinline fn symN(comptime N: usize, ctx: [N]u32, nbits: u5) u32 {
var node: u32 = 1;
const top = @as(u32, 1) << nbits;
while (true) {
var stretched: [N]i32 = undefined;
var slot: [N]u32 = undefined;
// The packer asserts |sum| < 2^31 (measured max 2.2e8), so 32-bit is exact here.
var dot: i32 = 0;
inline for (0..N) |k| {
const s: u32 = ((ctx[k] +% node *% HASH_A) *% HASH_B) >> (32 - MODEL_BITS);
slot[k] = s;
stretched[k] = stretch[(model[s] >> 4) ^ 0x800];
dot +%= stretched[k] *% (weights[k] +% MIX_W0);
}
var d: i32 = dot >> 16;
if (d < -2047) d = -2047;
if (d > 2047) d = 2047;
// 12-bit probability that the bit is 1, from 1 to 4095.
const p: u32 = squash[@intCast(d + 2047)];
const bound = (rc_range >> 12) * p;
const bit: u32 = @intFromBool(rc_code < bound);
if (bit != 0) {
rc_range = bound;
} else {
rc_code -= bound;
rc_range -= bound;
}
while (rc_range < (1 << 24)) {
rc_range <<= 8;
rc_code = (rc_code << 8) | packed_list[rc_in];
rc_in += 1;
}
const one: i32 = @as(i32, @intCast(bit)) << 12;
const err: i32 = one - @as(i32, @intCast(p));
inline for (0..N) |k| {
weights[k] +%= (stretched[k] *% err) >> MIX_SHIFT;
const e = model[slot[k]];
var q: i32 = (e >> 4) ^ 0x800;
const n = e & 15;
q += ((one - q) *% rate[n]) >> 16;
model[slot[k]] = @intCast((q ^ 0x800) << 4 | (n + @intFromBool(n < MODEL_LIMIT)));
}
node = node * 2 + bit;
if (node >= top) break;
}
return node - top;
}
inline fn sym1(c0: u32, nbits: u5) u32 {
return symN(1, .{c0}, nbits);
}
inline fn sym3(ca: u32, cb: u32, cc: u32, nbits: u5) u32 {
return symN(3, .{ ca, cb, cc }, nbits);
}
noinline fn init() void {
@branchHint(.cold);
@memset(std.mem.asBytes(&fast), 0xFF);
@memset(std.mem.asBytes(&leg), 0xFF);
@memset(len_mask[0..32], 0xFF);
buildSquash();
// The model, hist and last tables live in the buffer's zeroed start; nothing to clear.
rc_in = 4;
rc_range = 0xFFFF_FFFF;
rc_code = (@as(u32, packed_list[0]) << 24) | (@as(u32, packed_list[1]) << 16) |
(@as(u32, packed_list[2]) << 8) | packed_list[3];
var cp: u32 = 0;
var nl: usize = 0;
var hn: u32 = 1; // hist[0] is the always-zero sentinel
for (0..ENTITY_COUNT) |_| {
// Step from the previous code point: its bit length, then the bits below the top one.
const nb = sym1(K, 5);
var d = nb;
if (nb > 1) {
d = (@as(u32, 1) << @intCast(nb - 1)) + sym1(2 * K + nb, @intCast(nb - 1));
}
cp += d;
var vn: u32 = 0;
var vn2: u32 = 0;
var val: u64 = utf8(cp, &vn);
// A few values have a second code point.
if (sym1(3 * K + @intFromBool(d == 0), 1) != 0) {
val |= @as(u64, utf8(sym1(4 * K, 16), &vn2)) << @intCast(8 * vn);
}
val |= @as(u64, vn + vn2) << 56;
const legacy = sym1(5 * K + @intFromBool(cp < 256), 1) != 0;
// Names use a 6-bit alphabet: 0 ends the name, then digits, capitals, small letters.
var name = std.mem.zeroes([40]u8);
var n: u32 = 0;
var h: u32 = 0;
var mp: u32 = 0;
var ml: u32 = 0;
while (true) {
// The match model predicts the character that followed the last place the
// previous two were seen, and how many predictions in a row have been right.
const c = sym3(
8 * K + (h & 63),
9 * K + (h & 4095),
13 * K + (if (ml != 0) @as(u32, hist[mp]) else 0) * 16 + @min(ml, 3),
6,
);
if (ml != 0 and hist[mp] == c) {
ml += 1;
mp += 1;
} else {
ml = 0;
}
hist[hn] = @intCast(c);
hn += 1;
if (c == 0) break;
name[n] = @intCast(if (c < 11) c + 39;039; - 1 else if (c < 37) c + 39;A39; - 11 else c + 39;a39; - 37);
n += 1;
h = (h << 6) | c;
if (n >= 2) {
const l = &last[h & 4095];
if (ml == 0 and l.* != 0) {
mp = l.*;
ml = 1;
}
l.* = @intCast(hn);
}
}
const nm: [*]const u8 = &name;
if (n <= FAST_MAXLEN) {
var key = ld64(nm) | (@as(u64, 39;;39;) << @intCast(8 * n));
var e: [*]Entry = @ptrCast(&fast[@intCast((key *% FAST_MUL) >> (64 - FAST_BITS))]);
while (e[0].key != EMPTY) e += 1;
e[0] = .{ .key = key, .val = val };
if (legacy) {
key = ld64(nm) | (@as(u64, n) << 56);
e = @ptrCast(&leg[@intCast((key *% LEG_MUL) >> (64 - LEG_BITS))]);
while (e[0].key != EMPTY) e += 1;
e[0] = .{ .key = key, .val = val };
const s = legSlot(key); // only the first two bytes matter
if (leg_max[s] < n) leg_max[s] = @intCast(n);
}
} else {
const l = &longs[nl];
nl += 1;
// The hash covers the name and its ';', as the lookup's loads do; the
// stored name keeps zeros past its end (the compare masks them out).
name[n] = 39;;39;;
var hh = longHash(ld64(nm), ld64(nm + n - 7), n);
name[n] = 0;
l.name = name[0..32].*;
l.val = val | @as(u64, n) << 59;
while (long_slots[hh] != 0) hh = (hh + 1) & LONG_MASK;
long_slots[hh] = @intCast(nl);
}
}
ready = true;
}
export fn input_ptr() u32 {
return @intCast(@intFromPtr(input));
}
export fn input_utf8_cap() u32 {
return INPUT_CAP;
}
export fn output_ptr() u32 {
return @intCast(@intFromPtr(output));
}
export fn output_utf8_cap() u32 {
return OUTPUT_CAP;
}
export fn render(n: u32) u64 {
if (!ready) init();
const size: usize = @min(n, INPUT_CAP);
const src: [*]u8 = input;
const dst: [*]u8 = output;
const zero: V = @splat(0);
st(src + size, zero);
st(src + size + 16, zero);
st(src + size + 32, zero);
st(src + size + 48, zero);
// Stage 1 lists the position of every `&` in a window; stage 2 copies the text between
// references and decodes each one.
var e: usize = 0;
var o: usize = 0;
var base: usize = 0;
while (base < size) {
const lim = if (size - base > WINDOW) base + WINDOW else size;
var count: usize = 0;
while (true) {
const ma: u64 = eqMask(ld(src + base), 39;&39;);
const mb: u64 = eqMask(ld(src + base + 16), 39;&39;);
const mc: u64 = eqMask(ld(src + base + 32), 39;&39;);
const md: u64 = eqMask(ld(src + base + 48), 39;&39;);
var mm: u64 = ma | mb << 16 | mc << 32 | md << 48;
const cnt: usize = @popCount(mm);
const b: u32 = @intCast(base);
const out: [*]u32 = pos[0..].ptr + count;
// Entries past cnt are garbage and are overwritten or ignored.
inline for (0..4) |k| {
out[k] = b + @ctz(mm);
mm &= mm -% 1;
}
if (cnt > 4) {
@branchHint(.unlikely);
inline for (4..8) |k| {
out[k] = b + @ctz(mm);
mm &= mm -% 1;
}
var k: usize = 8;
while (mm != 0) : (k += 1) {
out[k] = b + @ctz(mm);
mm &= mm -% 1;
}
}
count += cnt;
base += 64;
if (base >= lim) break;
}
var q: usize = 0;
while (q < count) : (q += 1) {
var i: usize = pos[q];
// Output and input are one buffer, so the compiler cannot move a load across
// a store: everything this reference needs is loaded before the first store.
const w = ld64(src + i + 1);
// Copy the text since the last reference; src[i] is `&` and i < size.
{
const len = i - e;
const ca = ld(src + e);
const cb = ld(src + e + 16);
st(dst + o, ca);
st(dst + o + 16, cb);
if (len > 32) {
var j: usize = 32;
while (true) {
st(dst + o + j, ld(src + e + j));
j += 16;
if (j >= len) break;
}
}
o += len;
}
done: {
if (@as(u8, @truncate(w)) == 39;#39;) numeric: {
// Decimal and hexadecimal share one branch-free path: classify 8 bytes,
// take the digits before the first other byte, and fold them pairwise.
const hex = (@as(u8, @truncate(w >> 8)) | 0x20) == 39;x39;;
var p = src + i + 2 + @intFromBool(hex);
const d = ld64(p);
const t = d ^ 0x3030303030303030;
const digit = ~(((t & ~HIGH) +% 0x7676767676767676) | t) & HIGH;
const y = d | 0x2020202020202020;
const ty = y & ~HIGH;
const letter = (ty +% ONES * (0x80 - 39;a39;)) & ~(ty +% ONES * (0x7F - 39;f39;)) & ~y & HIGH;
const stop = ~(digit | (if (hex) letter else 0)) & HIGH;
var v: u32 = 0;
var end: usize = undefined;
if (stop != 0) {
const nb: u32 = @ctz(stop) & 0x38;
if (nb == 0) break :numeric;
var x = ((d & 0x0F0F0F0F0F0F0F0F) +% 9 * (letter >> 7)) << @intCast(64 - nb);
x = (x *% @as(u64, if (hex) 16 else 10) +% (x >> 8)) & 0x00FF00FF00FF00FF;
x = (x *% @as(u64, if (hex) 256 else 100) +% (x >> 16)) & 0x0000FFFF0000FFFF;
v = @truncate(x *% @as(u64, if (hex) 65536 else 10000) +% (x >> 32));
end = @intFromPtr(p) - @intFromPtr(src) + (nb >> 3);
} else {
// Eight or more digits: saturate past U+10FFFF.
@branchHint(.unlikely);
const radix: u32 = if (hex) 16 else 10;
while (true) {
var c: u32 = @as(u32, p[0]) -% 39;039;;
if (c > 9) {
c = (@as(u32, p[0]) | 0x20) -% 39;a39; +% 10;
if (c < 10) c = 16;
}
if (c >= radix) break;
v = v * radix + c;
if (v > 0x10FFFF) v = 0x110000;
p += 1;
}
end = @intFromPtr(p) - @intFromPtr(src);
}
i = end + @intFromBool(src[end] == 39;;39;);
if (v -% 0x80 < 0x20) {
v = c1[v - 0x80];
} else if (v == 0 or v -% 0xD800 < 0x800 or v > 0x10FFFF) {
v = 0xFFFD;
}
// Lay the bits out as four UTF-8 bytes, then drop the unused leading ones.
const len: u32 = 1 + @as(u32, @intFromBool(v > 0x7F)) + @intFromBool(v > 0x7FF) + @intFromBool(v > 0xFFFF);
const bits = v >> 18 | (v >> 12 & 0x3F) << 8 | (v >> 6 & 0x3F) << 16 | (v & 0x3F) << 24;
const word = bits >> @intCast(32 - 8 * len) | utf8_marks[len];
st32(dst + o, if (len == 1) v else word);
o += len;
break :done;
} else {
const t = w ^ (ONES * 39;;39;);
const z = (t -% ONES) & ~t & HIGH;
if (z != 0) {
// Bit index of the `;` byte; the key is the name and its `;`.
const kb: u32 = @ctz(z) & 0x38;
const key = w & ((@as(u64, 2) << @intCast(kb + 7)) -% 1);
var en: [*]const Entry = @ptrCast(&fast[@intCast((key *% FAST_MUL) >> (64 - FAST_BITS))]);
while (true) {
const k = en[0].key;
if (k == key) {
const val = en[0].val;
st64(dst + o, val);
o += @intCast(val >> 56);
i += 2 + (kb >> 3);
break :done;
}
if (k == EMPTY) break;
en += 1;
}
// A 7-byte name (its ';' is the 8th byte) lives in the long table too.
if (kb == 56 and longName(src, i, w, ld(src + i + 1), ld(src + i + 17), 7, dst, &o)) {
i += 9;
break :done;
}
} else {
// No `;` in 8 bytes: a name of 8 to 31 bytes needs its `;` in the next 24.
const s0 = ld(src + i + 1);
const s1 = ld(src + i + 17);
const sm: u32 = eqMask(s0, 39;;39;) | eqMask(s1, 39;;39;) << 16;
if (sm != 0) {
const len: usize = @ctz(sm);
if (longName(src, i, w, s0, s1, len, dst, &o)) {
i += 2 + len;
break :done;
}
}
}
// Without `;`, the longest legacy name that is a prefix.
var k: usize = leg_max[legSlot(w)];
while (k >= 2) : (k -= 1) {
// The length is part of the key, so a NUL after a shorter name cannot match.
const key = (w & ((@as(u64, 1) << @intCast(8 * k)) - 1)) | @as(u64, k) << 56;
var en: [*]const Entry = @ptrCast(&leg[@intCast((key *% LEG_MUL) >> (64 - LEG_BITS))]);
while (true) {
if (en[0].key == key) {
st64(dst + o, en[0].val);
o += @intCast(en[0].val >> 56);
i += 1 + k;
break :done;
}
if (en[0].key == EMPTY) break;
en += 1;
}
}
}
dst[o] = 39;&39;;
o += 1;
i += 1;
}
e = i;
}
}
// Without a reference the input is the output.
if (e == 0) return @as(u64, @intFromPtr(input)) << 32 | size;
var j = e;
while (j < size) : (j += 16) st(dst + o + (j - e), ld(src + j));
o += size - e;
return @as(u64, @intFromPtr(output)) << 32 | o;
}
Download source MIT licensed — build on it
Run it on the example input with qipdo (Node.js 22 or newer):
printf '%s' 'Fish & chips — café <3 😀 © 2026 ¬it;' | npx qipdo calculated.world components/f3efa921634d88080009812e8332dfb24b5b1ee3ffee08e3aff94d4e9e1021b6.wasm
Same code as my 1.68 ms entry 7caf4b41, built without the wasm-opt pass. Measured on the benchmark corpus (downloaded and replayed locally on wasmtime with in-wasm timing): wasm-opt -O1/-Oz/-O3 all cost 3-6% over Zig's own output, and every entry ahead on the fastest leaderboard is also built without one. Five pages, full 4,096-slot fast table, range-coded entity list, in-place shared buffer.
Try it
Step through this Wasm running on the input above. Focus the debugger, then press Space to run, N to step over or ? for all keys.
To debug it in your terminal with qiptui (Node.js 22 or newer):
First download the Wasm and the example input:
curl -o unescape_html.wasm https://calculated.world/components/f3efa921634d88080009812e8332dfb24b5b1ee3ffee08e3aff94d4e9e1021b6.wasm
printf '%s' 'Fish & chips — café <3 😀 © 2026 ¬it;' > input.txt
Then step through it in the qipdb debugger:
npx qiptui \
-F component=@unescape_html.wasm \
-F 'input=<input.txt' \
qip.dev/tui/qipdb.wasm
The debugger command fetches qipdb from qip.dev and keeps it in memory.
Build details
Compiler, size, memory and checksum
- Submitted
- 27 November 2026 at 17:09 UTC
- Language
- Zig
- Compiler
-
zig 0.17.1zig build-exe -target wasm32-freestanding -mcpu=generic+simd128+bulk_memory -O ReleaseSmall -fno-entry -rdynamic -fstrip --stack 2048 --initial-memory=327680 --max-memory=327680 - Size
- 12.2 KiB, gzip 9.9 KiB
- Download
- ~150 ms on Slow 4G (Lighthouse’s 1.6 Mbps, 150 ms round trips)
- Memory
- 5 pages (320 KiB)
- SIMD
- yes
- AI models
- GLM-5.3
Functions, loops, branches and other static counts
| Count | Value |
|---|---|
| Functions | 10 |
| Instructions | 2,409 |
| Loops | 23 |
| Conditional branches | 53 |
| Indirect calls | 0 |
| Memory loads | 68 |
| Memory stores | 64 |
| SIMD instructions | 67 |
| Potentially trapping instructions | 160 |
Instruction sites in the binary, not executions, counted by qip.dev’s wasm-counts.