Skip to main content

rudb_vector/
fsst.rs

1//! FSST, the string encoding.
2//!
3//! Fast Static Symbol Table, from the 2020 paper by Boncz, Neumann and Leis. A table of at most 255
4//! symbols of one to eight bytes each, and compression is replacing the longest matching symbol at
5//! each position with its one byte code. A byte that no symbol covers is escaped, which costs two
6//! bytes, so the table has to be good or the output is larger than the input.
7//!
8//! ## Why this and not a general compressor
9//!
10//! `spec/06-compression.md` section 6.2 is blunt about it. FSST compresses text about 2x, which is
11//! worse than what zstd does to the same bytes, and the ratio is not why it is here. Two other
12//! properties are.
13//!
14//! The first is random access. Every string in a column is compressed independently against a
15//! shared table, so reading row 4,000,000 does not mean decompressing the four million before it. A
16//! block compressor gives up that property and gets it back by cutting the data into blocks, which
17//! means reading one string decompresses a block.
18//!
19//! The second is that a substring search can run against the compressed bytes. Compress the needle
20//! with the same symbol table and look for the compressed needle in the compressed haystack. That is
21//! what turns `URL LIKE '%google%'` from a decompress and scan into a scan, and section 6.7 says it
22//! is worth more on the ClickBench workload than any ratio improvement. It needs care, because the
23//! greedy match that compresses a needle standing alone can segment it differently from the way the
24//! same bytes were segmented inside a longer string, so a hit is a candidate and a miss is not a
25//! proof. The scan that uses it is M3 work and lives with the rest of encoded execution.
26//!
27//! ## Training
28//!
29//! The table is built from a sample rather than from the whole column, and the algorithm is the
30//! paper's: start with nothing, so every byte escapes, then repeat five times. Compress the sample
31//! with the table you have, count how often each symbol is used and how often each pair of adjacent
32//! symbols occurs, and build the next table from the best 255 of the symbols and the concatenations
33//! by gain, where gain is how many bytes of input the symbol accounts for. Five generations is what
34//! the paper found, and the shape of the thing is that the first generation learns single bytes, the
35//! second learns pairs, and the fifth is finding eight byte symbols like `https://`.
36//!
37//! ## Matching
38//!
39//! Three lookups in a fixed order, longest first. A hash table on the first three bytes for symbols
40//! of three bytes and up, a flat table indexed by the first two bytes, and a flat table indexed by
41//! the first one. The hash table probes eight slots and keeps the longest symbol that matches rather
42//! than the first, because several symbols share a three byte prefix and taking the first would make
43//! the ratio depend on insertion order.
44
45use std::cell::RefCell;
46use std::sync::OnceLock;
47
48use rudb_common::{Error, Result};
49
50/// The code that means the next byte is a literal. 255 rather than 0 so that the 255 real codes are
51/// a contiguous range starting at zero and a code is its own index into the symbol table.
52pub const ESCAPE: u8 = 255;
53
54/// How many real symbols a table can hold.
55pub const MAX_SYMBOLS: usize = 255;
56
57/// The longest a symbol can be. Eight, so that a symbol is a `u64` and a match is a mask and a
58/// compare rather than a loop over bytes.
59pub const MAX_SYMBOL_LEN: usize = 8;
60
61/// How many generations the trainer runs. The paper's number.
62const GENERATIONS: usize = 5;
63
64/// Slots in the prefix hash table. A power of two, and four times the largest number of symbols that
65/// can be in it, which keeps the eight slot probe from filling up on a full table.
66const HASH_SLOTS: usize = 1024;
67
68/// How far a lookup probes before giving up. A miss here costs ratio and not correctness.
69const PROBE: usize = 8;
70
71/// One symbol. The bytes are in the low end of `value` in the order they appear, so that a match
72/// against the next eight bytes of input is one mask and one compare.
73#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
74struct Symbol {
75    value: u64,
76    len: u8,
77}
78
79impl Symbol {
80    fn new(bytes: &[u8]) -> Self {
81        let len = bytes.len().min(MAX_SYMBOL_LEN);
82        let mut value = 0u64;
83        for (index, byte) in bytes[..len].iter().enumerate() {
84            value |= u64::from(*byte) << (8 * index);
85        }
86        Self { value, len: len as u8 }
87    }
88
89    fn single(byte: u8) -> Self {
90        Self { value: u64::from(byte), len: 1 }
91    }
92
93    fn len(self) -> usize {
94        self.len as usize
95    }
96
97    fn mask(self) -> u64 {
98        mask_of(self.len())
99    }
100
101    fn bytes(self) -> Vec<u8> {
102        (0..self.len()).map(|index| (self.value >> (8 * index)) as u8).collect()
103    }
104
105    /// The two symbols end to end, cut off at eight bytes.
106    fn concat(self, other: Self) -> Self {
107        if self.len() >= MAX_SYMBOL_LEN {
108            return self;
109        }
110        let len = (self.len() + other.len()).min(MAX_SYMBOL_LEN);
111        let value = self.value | (other.value << (8 * self.len()));
112        Self { value: value & mask_of(len), len: len as u8 }
113    }
114}
115
116fn mask_of(len: usize) -> u64 {
117    if len >= 8 { u64::MAX } else { (1u64 << (8 * len)) - 1 }
118}
119
120/// A trained symbol table, and everything needed to compress and decompress against it.
121pub struct SymbolTable {
122    /// Code to symbol. At most [`MAX_SYMBOLS`] long.
123    symbols: Vec<Symbol>,
124    /// What compressing looks symbols up in, built the first time something is compressed.
125    ///
126    /// Decompressing only ever reads `symbols`, and a table read back from a file is almost always
127    /// read back to decompress. Building these eagerly filled a hundred and thirty kilobytes of
128    /// pair slots and sorted the symbols for every block of a text column a scan decoded, which on
129    /// `SELECT COUNT(*) FROM hits WHERE URL LIKE '%google%'` was six percent of the query.
130    lookup: OnceLock<Lookup>,
131}
132
133/// The three tables the matcher reads, all of them built from the symbols and holding nothing else.
134struct Lookup {
135    /// First byte to code, or [`ESCAPE`] when no one byte symbol covers it.
136    single: Vec<u8>,
137    /// First two bytes to code, or `u16::MAX` when there is no two byte symbol for them.
138    pair: Vec<u16>,
139    /// Open addressed, keyed on the first three bytes, holding every symbol of three bytes or more.
140    hash: Vec<Option<(Symbol, u8)>>,
141}
142
143impl std::fmt::Debug for SymbolTable {
144    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
145        // The lookup tables are 64k entries and printing them is never what anybody wanted.
146        formatter
147            .debug_struct("SymbolTable")
148            .field("symbols", &self.symbols.len())
149            .field("bytes", &self.serialized_len())
150            .finish()
151    }
152}
153
154/// Two tables are equal when they hold the same symbols in the same order.
155///
156/// The three lookup tables are built from the symbols when a table is built and hold nothing the
157/// symbols do not, so comparing them would be comparing the same information a second time over
158/// sixty five thousand entries. A vector in FSST form carries a table, and a vector is compared for
159/// equality all over the tests, so this is on a path that gets walked.
160impl PartialEq for SymbolTable {
161    fn eq(&self, other: &Self) -> bool {
162        self.symbols == other.symbols
163    }
164}
165
166impl Eq for SymbolTable {}
167
168impl SymbolTable {
169    /// How many bytes of memory this table is holding.
170    ///
171    /// Mostly the hash table, which is sixty five thousand slots however few symbols are in it. That
172    /// is the number a vector in FSST form reports, and it is why the form is a decision about a
173    /// page rather than about a chunk: one table over a hundred chunks is nothing per chunk and one
174    /// table per chunk is a megabyte.
175    #[must_use]
176    pub fn footprint(&self) -> usize {
177        size_of::<Self>()
178            + self.symbols.capacity() * size_of::<Symbol>()
179            + self.lookup.get().map_or(0, |lookup| {
180                lookup.single.capacity()
181                    + lookup.pair.capacity() * size_of::<u16>()
182                    + lookup.hash.capacity() * size_of::<Option<(Symbol, u8)>>()
183            })
184    }
185
186    /// A table with no symbols, which escapes everything and doubles its input. The starting point
187    /// of training, and what a column of nothing but unique bytes ends up with.
188    #[must_use]
189    pub fn empty() -> Self {
190        Self::build(Vec::new())
191    }
192
193    /// Trains a table on a sample.
194    ///
195    /// The caller picks the sample. Section 6.3 says a systematic sample across the chunk rather
196    /// than the first N rows, because column data is frequently clustered, and that decision belongs
197    /// to whoever knows what the chunk is rather than to this function.
198    #[must_use]
199    pub fn train(samples: &[&[u8]]) -> Self {
200        // The counts are a megabyte of pair slots, and a load trains a table for every block of
201        // every text column it writes. So each thread keeps one and clears the slots it used, rather
202        // than asking for a fresh megabyte of zeroes and faulting it in on every block.
203        thread_local! {
204            static COUNTS: RefCell<Option<Counts>> = const { RefCell::new(None) };
205        }
206        COUNTS.with(|held| match held.try_borrow_mut() {
207            Ok(mut held) => Self::train_with(samples, held.get_or_insert_with(Counts::new)),
208            Err(_) => Self::train_with(samples, &mut Counts::new()),
209        })
210    }
211
212    fn train_with(samples: &[&[u8]], counts: &mut Counts) -> Self {
213        let mut table = Self::empty();
214        for _ in 0..GENERATIONS {
215            counts.clear();
216            for sample in samples {
217                table.count(sample, counts);
218            }
219            let next = counts.best(&table);
220            if next.is_empty() {
221                break;
222            }
223            table = Self::build(next);
224        }
225        table
226    }
227
228    /// How many symbols are in the table.
229    #[must_use]
230    pub fn len(&self) -> usize {
231        self.symbols.len()
232    }
233
234    /// Whether the table has no symbols, in which case every byte of every string escapes.
235    #[must_use]
236    pub fn is_empty(&self) -> bool {
237        self.symbols.is_empty()
238    }
239
240    /// How many bytes [`serialize`](Self::serialize) writes. At most 2049 for a full table, and
241    /// that is the number section 6.4 is weighing when it says a shared symbol table is cheaper
242    /// than a shared dictionary.
243    #[must_use]
244    pub fn serialized_len(&self) -> usize {
245        1 + self.symbols.iter().map(|symbol| 1 + symbol.len()).sum::<usize>()
246    }
247
248    /// Writes the table itself, which has to travel with the data it compressed.
249    pub fn serialize(&self, out: &mut Vec<u8>) {
250        out.push(self.symbols.len() as u8);
251        for symbol in &self.symbols {
252            out.push(symbol.len);
253            out.extend_from_slice(&symbol.bytes());
254        }
255    }
256
257    /// Reads back what [`serialize`](Self::serialize) wrote, and says how many bytes it consumed.
258    ///
259    /// # Errors
260    ///
261    /// If the bytes are truncated or describe a symbol of zero or more than eight bytes.
262    pub fn deserialize(bytes: &[u8]) -> Result<(Self, usize)> {
263        let count = *bytes.first().ok_or_else(|| truncated("a symbol table header"))? as usize;
264        let mut at = 1;
265        let mut symbols = Vec::with_capacity(count);
266        for _ in 0..count {
267            let len = *bytes.get(at).ok_or_else(|| truncated("a symbol length"))? as usize;
268            if len == 0 || len > MAX_SYMBOL_LEN {
269                return Err(Error::internal(format!("a symbol of {len} bytes is not a symbol")));
270            }
271            at += 1;
272            let end = at + len;
273            if end > bytes.len() {
274                return Err(truncated("a symbol"));
275            }
276            symbols.push(Symbol::new(&bytes[at..end]));
277            at = end;
278        }
279        Ok((Self::build(symbols), at))
280    }
281
282    /// Compresses one string, appending to `out`.
283    ///
284    /// Strings are compressed one at a time against a shared table rather than as one stream,
285    /// because that is what keeps random access, which is the first of the two reasons this encoding
286    /// was chosen at all.
287    pub fn compress(&self, input: &[u8], out: &mut Vec<u8>) {
288        let mut at = 0;
289        while at < input.len() {
290            let (code, len) = self.match_at(input, at);
291            if code == ESCAPE {
292                out.push(ESCAPE);
293                out.push(input[at]);
294            } else {
295                out.push(code);
296            }
297            at += len;
298        }
299    }
300
301    /// Decompresses one string, appending to `out`.
302    ///
303    /// A symbol goes out as all eight of the bytes its `u64` holds, and then the cursor steps back
304    /// over the ones that were not part of it. Eight is a length the compiler knows, so that is one
305    /// store. The length a symbol really has is only known at run time, so copying exactly that
306    /// many bytes is a call into `memcpy` for one to eight of them, and building a `Vec` to copy
307    /// them out of, which is what this used to do, is a heap allocation and a free on top.
308    ///
309    /// That mattered more than anything else in the engine. `SELECT COUNT(*) FROM hits WHERE URL
310    /// LIKE '%google%'` over ClickBench spends almost all of its time here, because the search
311    /// itself runs once per distinct URL and finding those means decompressing the column, and the
312    /// allocation, the free and the copy together were 41% of the query.
313    ///
314    /// # Errors
315    ///
316    /// If the input ends on an escape byte, or holds a code the table does not have.
317    pub fn decompress(&self, input: &[u8], out: &mut Vec<u8>) -> Result<()> {
318        // What the table is trained to reach, plus room for the tail of the last symbol, so the
319        // loop below mostly finds the space already there. Nothing here depends on the guess being
320        // right: too small and the growth happens where it always did.
321        out.reserve(input.len().saturating_mul(2).saturating_add(MAX_SYMBOL_LEN));
322        let mut at = 0;
323        while at < input.len() {
324            let code = input[at];
325            at += 1;
326            if code == ESCAPE {
327                let literal = *input.get(at).ok_or_else(|| truncated("an escaped byte"))?;
328                out.push(literal);
329                at += 1;
330            } else {
331                let symbol = *self
332                    .symbols
333                    .get(code as usize)
334                    .ok_or_else(|| Error::internal(format!("code {code} is not in the table")))?;
335                out.extend_from_slice(&symbol.value.to_le_bytes());
336                out.truncate(out.len() - (MAX_SYMBOL_LEN - symbol.len()));
337            }
338        }
339        Ok(())
340    }
341
342    /// Decompresses one string into `out` from `at`, handing back where it ended.
343    ///
344    /// [`Self::decompress`] for a caller that has already made the buffer as long as the output
345    /// is going to be, so a symbol is one eight byte store and a step of the cursor with no length
346    /// to keep and no capacity to check. The store is eight bytes whatever the symbol's length,
347    /// which is why `out` needs [`MAX_SYMBOL_LEN`] bytes of room past the end of the string.
348    ///
349    /// # Errors
350    ///
351    /// As [`Self::decompress`], and if `out` runs out of room, which a string that really
352    /// decompresses to the length the caller sized for never does.
353    pub fn decompress_at(&self, input: &[u8], out: &mut [u8], mut at: usize) -> Result<usize> {
354        let mut read = 0;
355        while read < input.len() {
356            let code = input[read];
357            read += 1;
358            if code == ESCAPE {
359                let literal = *input.get(read).ok_or_else(|| truncated("an escaped byte"))?;
360                read += 1;
361                *out.get_mut(at).ok_or_else(out_of_room)? = literal;
362                at += 1;
363            } else {
364                let symbol = *self
365                    .symbols
366                    .get(code as usize)
367                    .ok_or_else(|| Error::internal(format!("code {code} is not in the table")))?;
368                out.get_mut(at..at + MAX_SYMBOL_LEN)
369                    .ok_or_else(out_of_room)?
370                    .copy_from_slice(&symbol.value.to_le_bytes());
371                at += symbol.len();
372            }
373        }
374        Ok(at)
375    }
376
377    /// The code and how many input bytes it covers. [`ESCAPE`] and 1 when nothing matches.
378    fn match_at(&self, input: &[u8], at: usize) -> (u8, usize) {
379        let remaining = input.len() - at;
380        let word = load(input, at);
381        // Written as a nested `if` rather than as a chained `if let` because the minimum supported
382        // Rust version is 1.85 and let chains landed in 1.88.
383        if remaining >= 3 {
384            if let Some((symbol, code)) = self.probe(word, remaining) {
385                return (code, symbol.len());
386            }
387        }
388        if remaining >= 2 {
389            let code = self.lookup().pair[(word & 0xffff) as usize];
390            if code != u16::MAX {
391                return (code as u8, 2);
392            }
393        }
394        let code = self.lookup().single[(word & 0xff) as usize];
395        if code == ESCAPE { (ESCAPE, 1) } else { (code, 1) }
396    }
397
398    /// The longest symbol of three bytes or more matching here, if any.
399    ///
400    /// Longest rather than first, because several symbols share a three byte prefix and taking
401    /// whichever the probe reached first would make the compression ratio depend on the order the
402    /// table was built in.
403    fn probe(&self, word: u64, remaining: usize) -> Option<(Symbol, u8)> {
404        let hash = &self.lookup().hash;
405        let mut slot = hash_of(word);
406        let mut best: Option<(Symbol, u8)> = None;
407        for _ in 0..PROBE {
408            match hash[slot] {
409                None => break,
410                Some((symbol, code)) => {
411                    if symbol.len() <= remaining
412                        && word & symbol.mask() == symbol.value
413                        && best.is_none_or(|(found, _)| symbol.len() > found.len())
414                    {
415                        best = Some((symbol, code));
416                    }
417                }
418            }
419            slot = (slot + 1) & (HASH_SLOTS - 1);
420        }
421        best
422    }
423
424    /// Runs the matcher over a sample without producing output, recording what it used. This is the
425    /// counting half of a training generation.
426    fn count(&self, input: &[u8], counts: &mut Counts) {
427        let mut at = 0;
428        let mut previous: Option<u16> = None;
429        while at < input.len() {
430            let (code, len) = self.match_at(input, at);
431            let id = if code == ESCAPE { 256 + u16::from(input[at]) } else { u16::from(code) };
432            counts.one(id);
433            if let Some(previous) = previous {
434                counts.two(previous, id);
435            }
436            previous = Some(id);
437            at += len;
438        }
439    }
440
441    fn build(symbols: Vec<Symbol>) -> Self {
442        Self { symbols, lookup: OnceLock::new() }
443    }
444
445    fn lookup(&self) -> &Lookup {
446        self.lookup.get_or_init(|| Lookup::of(&self.symbols))
447    }
448}
449
450impl Lookup {
451    fn of(symbols: &[Symbol]) -> Self {
452        let mut table = Self {
453            single: vec![ESCAPE; 256],
454            pair: vec![u16::MAX; 65536],
455            hash: vec![None; HASH_SLOTS],
456        };
457        // Longest first, so that a short symbol never displaces a long one out of the probe window
458        // and the flat tables get the lowest code for a duplicate.
459        let mut order: Vec<(Symbol, u8)> =
460            symbols.iter().enumerate().map(|(code, symbol)| (*symbol, code as u8)).collect();
461        order.sort_by_key(|(symbol, code)| (std::cmp::Reverse(symbol.len()), *code));
462        for (symbol, code) in order {
463            match symbol.len() {
464                1 => {
465                    let index = (symbol.value & 0xff) as usize;
466                    if table.single[index] == ESCAPE {
467                        table.single[index] = code;
468                    }
469                }
470                2 => {
471                    let index = (symbol.value & 0xffff) as usize;
472                    if table.pair[index] == u16::MAX {
473                        table.pair[index] = u16::from(code);
474                    }
475                }
476                _ => {
477                    let mut slot = hash_of(symbol.value);
478                    for _ in 0..PROBE {
479                        if table.hash[slot].is_none() {
480                            table.hash[slot] = Some((symbol, code));
481                            break;
482                        }
483                        slot = (slot + 1) & (HASH_SLOTS - 1);
484                    }
485                }
486            }
487        }
488        table
489    }
490}
491
492/// The next eight bytes as a little endian word, zero padded at the end of the input.
493///
494/// The padding is why every match checks the remaining length as well as the mask. Without that
495/// check a two byte symbol ending in a zero byte would match the last byte of a string.
496fn load(input: &[u8], at: usize) -> u64 {
497    if at + 8 <= input.len() {
498        let bytes: [u8; 8] = input[at..at + 8].try_into().expect("eight bytes were checked");
499        u64::from_le_bytes(bytes)
500    } else {
501        let mut word = 0u64;
502        for (index, byte) in input[at..].iter().enumerate() {
503            word |= u64::from(*byte) << (8 * index);
504        }
505        word
506    }
507}
508
509/// Hashes the first three bytes. The multiply and shift is the standard Fibonacci hash, which
510/// spreads a three byte key across the whole slot range where a mask of the low bits would put every
511/// symbol starting with the same letter in the same neighbourhood.
512fn hash_of(word: u64) -> usize {
513    let key = word & 0xff_ffff;
514    ((key.wrapping_mul(0x9e37_79b9_7f4a_7c15)) >> (64 - HASH_SLOTS.trailing_zeros())) as usize
515}
516
517/// What one training generation counts. Symbol ids below 256 are codes in the current table and ids
518/// from 256 up are escaped literal bytes, which is how a generation learns single bytes it does not
519/// have yet.
520struct Counts {
521    single: Vec<u32>,
522    /// Indexed by `first * IDS + second`. Flat rather than hashed because every id is under 512, so
523    /// every pair fits in a quarter million slots, and counting pairs is most of what training does.
524    pairs: Vec<u32>,
525    /// The pair slots that are not zero, so that reading and clearing them costs the pairs seen
526    /// rather than the whole array.
527    seen: Vec<u32>,
528    /// The candidates of the generation being ranked, kept so a thread sizes them once.
529    gains: Gains,
530}
531
532/// Every candidate symbol once with its summed gain, which [`Counts::best`] ranks.
533#[derive(Default)]
534struct Gains {
535    gains: Vec<(Symbol, u64)>,
536    /// Where each symbol sits in `gains`, open addressed on the symbol, `u32::MAX` where empty.
537    places: Vec<u32>,
538    /// The slots of `places` in use, so that emptying it costs the symbols rather than the table.
539    taken: Vec<u32>,
540}
541
542impl Gains {
543    /// Empties the candidates, with room for `most` of them.
544    fn clear(&mut self, most: usize) {
545        for place in self.taken.drain(..) {
546            self.places[place as usize] = u32::MAX;
547        }
548        let wanted = (most * 2).next_power_of_two();
549        if self.places.len() < wanted {
550            self.places = vec![u32::MAX; wanted];
551        }
552        self.gains.clear();
553    }
554
555    /// Adds `count` uses of `symbol` to its gain, making it a candidate if it is not one yet.
556    fn add(&mut self, symbol: Symbol, count: u64) {
557        let gain = count * symbol.len() as u64;
558        let mask = self.places.len() - 1;
559        let mut place = gain_hash(symbol) & mask;
560        loop {
561            let at = self.places[place];
562            if at == u32::MAX {
563                self.places[place] = self.gains.len() as u32;
564                self.taken.push(place as u32);
565                self.gains.push((symbol, gain));
566                return;
567            }
568            if self.gains[at as usize].0 == symbol {
569                self.gains[at as usize].1 += gain;
570                return;
571            }
572            place = (place + 1) & mask;
573        }
574    }
575}
576
577/// How many symbol ids there are: 256 codes and 256 escaped bytes.
578const IDS: usize = 512;
579
580impl Counts {
581    fn new() -> Self {
582        Self {
583            single: vec![0; IDS],
584            pairs: vec![0; IDS * IDS],
585            seen: Vec::new(),
586            gains: Gains::default(),
587        }
588    }
589
590    fn clear(&mut self) {
591        self.single.fill(0);
592        for slot in self.seen.drain(..) {
593            self.pairs[slot as usize] = 0;
594        }
595    }
596
597    fn one(&mut self, id: u16) {
598        self.single[id as usize] += 1;
599    }
600
601    fn two(&mut self, first: u16, second: u16) {
602        let slot = first as usize * IDS + second as usize;
603        if self.pairs[slot] == 0 {
604            self.seen.push(slot as u32);
605        }
606        self.pairs[slot] += 1;
607    }
608
609    /// The 255 best symbols for the next generation.
610    ///
611    /// Gain is how many bytes of input a symbol accounts for, which is its length times how often it
612    /// was used. A concatenation is scored on the length it would have, so a pair of four byte
613    /// symbols scores as eight and a pair of six byte ones also scores as eight, because that is
614    /// what it would be cut down to.
615    fn best(&mut self, table: &SymbolTable) -> Vec<Symbol> {
616        // Different ids can spell the same symbol, a code and the pair it was learned from for one,
617        // and two pairs whose concatenation runs past eight bytes for another, so the gains are
618        // summed per symbol before anything is ranked. This was a sort of every candidate by symbol
619        // followed by a dedup, and on a ClickBench load that sort was a quarter of training.
620        self.gains.clear(IDS + self.seen.len());
621        for (id, count) in self.single.iter().enumerate() {
622            if *count == 0 {
623                continue;
624            }
625            let symbol = symbol_of(table, id as u16);
626            self.gains.add(symbol, u64::from(*count));
627        }
628        for slot in &self.seen {
629            let slot = *slot as usize;
630            let (first, second) = ((slot / IDS) as u16, (slot % IDS) as u16);
631            let symbol = symbol_of(table, first).concat(symbol_of(table, second));
632            self.gains.add(symbol, u64::from(self.pairs[slot]));
633        }
634        let gains = &mut self.gains.gains;
635        // Gain first, then the symbol itself, so that two symbols with the same gain come out in the
636        // same order on every host and the table is a function of the sample and nothing else. The
637        // symbols are distinct by now, so the order is total and an unstable sort gives one answer.
638        let order = |left: &(Symbol, u64), right: &(Symbol, u64)| {
639            right.1.cmp(&left.1).then(left.0.cmp(&right.0))
640        };
641        if gains.len() > MAX_SYMBOLS {
642            gains.select_nth_unstable_by(MAX_SYMBOLS - 1, order);
643            gains.truncate(MAX_SYMBOLS);
644        }
645        gains.sort_unstable_by(order);
646        gains.iter().map(|(symbol, _)| *symbol).collect()
647    }
648}
649
650/// Where a symbol's search for its place in [`Gains::places`] starts.
651fn gain_hash(symbol: Symbol) -> usize {
652    ((symbol.value ^ u64::from(symbol.len)).wrapping_mul(0x9e37_79b9_7f4a_7c15) >> 32) as usize
653}
654
655fn symbol_of(table: &SymbolTable, id: u16) -> Symbol {
656    if (id as usize) < table.symbols.len() {
657        table.symbols[id as usize]
658    } else {
659        Symbol::single((id.saturating_sub(256)) as u8)
660    }
661}
662
663fn out_of_room() -> Error {
664    Error::internal("a string decompresses to more than its length says")
665}
666
667fn truncated(what: &str) -> Error {
668    Error::internal(format!("the input ended in the middle of {what}"))
669}
670
671#[cfg(test)]
672mod tests {
673    use super::*;
674
675    #[test]
676    fn a_table_read_back_to_decompress_builds_no_lookup_tables() {
677        let urls = urls();
678        let samples: Vec<&[u8]> = urls.iter().map(Vec::as_slice).collect();
679        let trained = SymbolTable::train(&samples);
680        let mut compressed = Vec::new();
681        trained.compress(&urls[7], &mut compressed);
682        let mut stored = Vec::new();
683        trained.serialize(&mut stored);
684        let (read, _) = SymbolTable::deserialize(&stored).expect("a table it wrote");
685        let mut out = Vec::new();
686        read.decompress(&compressed, &mut out).expect("a string it compressed");
687        assert_eq!(out, urls[7]);
688        assert!(read.lookup.get().is_none(), "decompressing reads the symbols alone");
689        assert!(read.footprint() < trained.footprint());
690    }
691
692    /// A few hundred URLs in the shape ClickBench `hits` has them, which is the workload this
693    /// encoding was chosen for. Repetitive in the way real URLs are: a handful of hosts, a handful
694    /// of path shapes, and query strings that differ in a number.
695    fn urls() -> Vec<Vec<u8>> {
696        let hosts = ["www.example.com", "shop.example.com", "news.other.example.org"];
697        let paths = ["/index.html", "/catalog/item", "/search", "/user/profile/settings"];
698        let mut out = Vec::new();
699        for index in 0..600 {
700            let host = hosts[index % hosts.len()];
701            let path = paths[(index / 3) % paths.len()];
702            out.push(
703                format!("http://{host}{path}?session={}&ref=google&page={}", index * 7, index % 20)
704                    .into_bytes(),
705            );
706        }
707        out
708    }
709
710    fn borrow(strings: &[Vec<u8>]) -> Vec<&[u8]> {
711        strings.iter().map(Vec::as_slice).collect()
712    }
713
714    fn round_trip(table: &SymbolTable, strings: &[Vec<u8>]) -> (usize, usize) {
715        let mut raw = 0;
716        let mut compressed = 0;
717        for string in strings {
718            let mut bytes = Vec::new();
719            table.compress(string, &mut bytes);
720            let mut back = Vec::new();
721            table.decompress(&bytes, &mut back).unwrap();
722            assert_eq!(back, *string, "{}", String::from_utf8_lossy(string));
723            raw += string.len();
724            compressed += bytes.len();
725        }
726        (raw, compressed)
727    }
728
729    #[test]
730    fn urls_compress_by_more_than_half_and_come_back_unchanged() {
731        // The number the paper reports on text is around 2x, and URLs are more repetitive than
732        // text. Anything under 2x here means the trainer is not finding the long symbols.
733        let strings = urls();
734        let table = SymbolTable::train(&borrow(&strings));
735        let (raw, compressed) = round_trip(&table, &strings);
736        let ratio = raw as f64 / compressed as f64;
737        assert!(ratio > 2.5, "{ratio:.2}x, {raw} to {compressed}");
738        assert!(table.len() > 100, "{} symbols", table.len());
739    }
740
741    #[test]
742    fn the_trainer_finds_the_long_repeated_pieces() {
743        let strings = urls();
744        let table = SymbolTable::train(&borrow(&strings));
745        let found: Vec<String> = (0..table.len())
746            .map(|code| String::from_utf8_lossy(&table.symbols[code].bytes()).into_owned())
747            .collect();
748        // Not a specific symbol, since which eight bytes win is a property of the sample, but the
749        // table has to be mostly long symbols or it has not learned anything.
750        let long = found.iter().filter(|symbol| symbol.len() >= 6).count();
751        assert!(long > 60, "only {long} symbols of six bytes or more: {found:?}");
752    }
753
754    #[test]
755    fn english_text_round_trips_and_shrinks() {
756        let text: Vec<Vec<u8>> = "the quick brown fox jumps over the lazy dog while the other dog \
757             watches the fox and the dog and the fox go over the hill together"
758            .split(' ')
759            .map(|word| word.as_bytes().to_vec())
760            .collect();
761        let table = SymbolTable::train(&borrow(&text));
762        let (raw, compressed) = round_trip(&table, &text);
763        assert!(compressed < raw, "{raw} to {compressed}");
764    }
765
766    #[test]
767    fn incompressible_bytes_round_trip_and_cost_what_escaping_costs() {
768        // The worst case, and it has to be a correct worst case. Every byte escapes at two bytes
769        // each unless the trainer finds single byte symbols, which it will for the 255 most common
770        // of the 256 values.
771        let mut state = 0x1234_5678_9abc_def0u64;
772        let strings: Vec<Vec<u8>> = (0..100)
773            .map(|_| {
774                (0..64)
775                    .map(|_| {
776                        state ^= state << 13;
777                        state ^= state >> 7;
778                        state ^= state << 17;
779                        state as u8
780                    })
781                    .collect()
782            })
783            .collect();
784        let table = SymbolTable::train(&borrow(&strings));
785        let (raw, compressed) = round_trip(&table, &strings);
786        assert!(compressed < raw * 2, "{raw} to {compressed}");
787    }
788
789    #[test]
790    fn an_empty_table_escapes_everything_and_still_round_trips() {
791        let table = SymbolTable::empty();
792        let strings = vec![b"hello".to_vec(), Vec::new(), b"x".to_vec()];
793        let (raw, compressed) = round_trip(&table, &strings);
794        assert_eq!(compressed, raw * 2);
795    }
796
797    #[test]
798    fn an_empty_string_compresses_to_nothing() {
799        let table = SymbolTable::train(&[b"abcabcabc"]);
800        let mut out = Vec::new();
801        table.compress(b"", &mut out);
802        assert!(out.is_empty());
803        let mut back = Vec::new();
804        table.decompress(&out, &mut back).unwrap();
805        assert!(back.is_empty());
806    }
807
808    #[test]
809    fn a_string_shorter_than_the_symbols_does_not_read_past_its_end() {
810        // The load pads with zeros, so without the length check a two byte symbol whose second byte
811        // is zero would match the last byte of a string and swallow a byte that is not there.
812        let table = SymbolTable::train(&[b"ab\0ab\0ab\0ab\0", b"abcdefgh"]);
813        for string in [b"a".to_vec(), b"ab".to_vec(), b"abc".to_vec()] {
814            let mut bytes = Vec::new();
815            table.compress(&string, &mut bytes);
816            let mut back = Vec::new();
817            table.decompress(&bytes, &mut back).unwrap();
818            assert_eq!(back, string);
819        }
820    }
821
822    #[test]
823    fn a_table_survives_being_written_and_read_back() {
824        let strings = urls();
825        let table = SymbolTable::train(&borrow(&strings));
826        let mut bytes = Vec::new();
827        table.serialize(&mut bytes);
828        assert_eq!(bytes.len(), table.serialized_len());
829        let (read, consumed) = SymbolTable::deserialize(&bytes).unwrap();
830        assert_eq!(consumed, bytes.len());
831        assert_eq!(read.symbols, table.symbols);
832
833        // And the read back table compresses to the same bytes, which is the property that matters,
834        // since the lookup structures are rebuilt rather than stored.
835        let mut first = Vec::new();
836        let mut second = Vec::new();
837        table.compress(&strings[7], &mut first);
838        read.compress(&strings[7], &mut second);
839        assert_eq!(first, second);
840    }
841
842    #[test]
843    fn a_full_table_is_two_kilobytes_at_the_very_most() {
844        let strings = urls();
845        let table = SymbolTable::train(&borrow(&strings));
846        assert!(table.serialized_len() <= 1 + MAX_SYMBOLS * (1 + MAX_SYMBOL_LEN));
847        assert!(table.serialized_len() <= 2049);
848    }
849
850    #[test]
851    fn a_truncated_symbol_table_is_an_error() {
852        let strings = urls();
853        let table = SymbolTable::train(&borrow(&strings));
854        let mut bytes = Vec::new();
855        table.serialize(&mut bytes);
856        for len in 1..bytes.len().min(40) {
857            let error = SymbolTable::deserialize(&bytes[..len]).unwrap_err();
858            assert!(error.message().contains("ended in the middle"), "{error}");
859        }
860    }
861
862    #[test]
863    fn a_symbol_of_zero_bytes_is_an_error() {
864        let error = SymbolTable::deserialize(&[1, 0]).unwrap_err();
865        assert!(error.message().contains("is not a symbol"), "{error}");
866    }
867
868    #[test]
869    fn a_short_symbol_does_not_drag_the_rest_of_its_word_out_with_it() {
870        // Decompression writes all eight bytes of a symbol and steps back over the ones that were
871        // not part of it, so a table of short symbols is where that would show. `ab` and `cd` are
872        // two bytes each and sit in a `u64` with six zero bytes above them, and if the step back
873        // were wrong those zeros would be in the answer. Appending twice checks it again at an
874        // offset, since the second write lands where the first one left the cursor.
875        let table = SymbolTable::train(&[b"abcdabcdabcdabcd"]);
876        let mut compressed = Vec::new();
877        table.compress(b"abcdabcd", &mut compressed);
878        let mut out = Vec::new();
879        table.decompress(&compressed, &mut out).expect("decompresses");
880        table.decompress(&compressed, &mut out).expect("decompresses");
881        assert_eq!(out, b"abcdabcdabcdabcd");
882    }
883
884    #[test]
885    fn a_dangling_escape_is_an_error_and_not_a_panic() {
886        let table = SymbolTable::train(&[b"abcabcabc"]);
887        let error = table.decompress(&[ESCAPE], &mut Vec::new()).unwrap_err();
888        assert!(error.message().contains("escaped byte"), "{error}");
889    }
890
891    #[test]
892    fn a_code_the_table_does_not_have_is_an_error() {
893        let table = SymbolTable::train(&[b"abcabcabc"]);
894        let code = table.len() as u8;
895        let error = table.decompress(&[code], &mut Vec::new()).unwrap_err();
896        assert!(error.message().contains("not in the table"), "{error}");
897    }
898
899    #[test]
900    fn training_twice_on_the_same_sample_gives_the_same_table() {
901        // A table that differs run to run would make every size in the M1 report unreproducible.
902        let strings = urls();
903        let first = SymbolTable::train(&borrow(&strings));
904        let second = SymbolTable::train(&borrow(&strings));
905        assert_eq!(first.symbols, second.symbols);
906    }
907
908    #[test]
909    fn the_longest_match_wins_rather_than_the_first_one_found() {
910        let table = SymbolTable::build(vec![
911            Symbol::new(b"abc"),
912            Symbol::new(b"abcdef"),
913            Symbol::new(b"abcd"),
914        ]);
915        let mut out = Vec::new();
916        table.compress(b"abcdef", &mut out);
917        assert_eq!(out, vec![1]);
918    }
919
920    #[test]
921    fn a_symbol_longer_than_what_is_left_is_not_used() {
922        let table = SymbolTable::build(vec![Symbol::new(b"abcdef"), Symbol::new(b"ab")]);
923        let mut out = Vec::new();
924        table.compress(b"abcd", &mut out);
925        // "ab" then two escapes, rather than a six byte symbol over four bytes of input.
926        assert_eq!(out, vec![1, ESCAPE, b'c', ESCAPE, b'd']);
927    }
928
929    #[test]
930    fn concatenation_stops_at_eight_bytes() {
931        let long = Symbol::new(b"abcdef");
932        assert_eq!(long.concat(Symbol::new(b"ghijkl")).bytes(), b"abcdefgh");
933        assert_eq!(Symbol::new(b"ab").concat(Symbol::new(b"cd")).bytes(), b"abcd");
934    }
935
936    /// The trainer the way it was written first, with the pairs and the gains in hash maps and one
937    /// stable sort over everything, kept here to check the flat counts pick the same symbols.
938    fn train_with_maps(samples: &[&[u8]]) -> SymbolTable {
939        use std::collections::HashMap;
940        let mut table = SymbolTable::empty();
941        for _ in 0..GENERATIONS {
942            let mut single = [0u32; IDS];
943            let mut pairs: HashMap<(u16, u16), u32> = HashMap::new();
944            for sample in samples {
945                let mut at = 0;
946                let mut previous: Option<u16> = None;
947                while at < sample.len() {
948                    let (code, len) = table.match_at(sample, at);
949                    let id =
950                        if code == ESCAPE { 256 + u16::from(sample[at]) } else { u16::from(code) };
951                    single[id as usize] += 1;
952                    if let Some(previous) = previous {
953                        *pairs.entry((previous, id)).or_insert(0) += 1;
954                    }
955                    previous = Some(id);
956                    at += len;
957                }
958            }
959            let mut gains: HashMap<Symbol, u64> = HashMap::new();
960            for (id, count) in single.iter().enumerate().filter(|(_, count)| **count > 0) {
961                let symbol = symbol_of(&table, id as u16);
962                *gains.entry(symbol).or_insert(0) += u64::from(*count) * symbol.len() as u64;
963            }
964            for ((first, second), count) in &pairs {
965                let symbol = symbol_of(&table, *first).concat(symbol_of(&table, *second));
966                *gains.entry(symbol).or_insert(0) += u64::from(*count) * symbol.len() as u64;
967            }
968            let mut ranked: Vec<(Symbol, u64)> = gains.into_iter().collect();
969            ranked.sort_by(|left, right| right.1.cmp(&left.1).then(left.0.cmp(&right.0)));
970            ranked.truncate(MAX_SYMBOLS);
971            if ranked.is_empty() {
972                break;
973            }
974            table = SymbolTable::build(ranked.into_iter().map(|(symbol, _)| symbol).collect());
975        }
976        table
977    }
978
979    #[test]
980    fn flat_counts_train_the_same_table_as_hash_maps() {
981        let mut state = 0x9e37_79b9_7f4a_7c15u64;
982        let mut next = move || {
983            state ^= state << 13;
984            state ^= state >> 7;
985            state ^= state << 17;
986            state
987        };
988        let mut shapes: Vec<Vec<Vec<u8>>> = vec![urls(), Vec::new(), vec![Vec::new(); 3]];
989        // Few letters, so that many pairs tie on gain and the tie break decides the table.
990        shapes.push(
991            (0..400).map(|_| (0..12).map(|_| b"abc"[(next() % 3) as usize]).collect()).collect(),
992        );
993        // Every byte value, so that escapes of all 256 bytes are counted.
994        shapes.push((0..300).map(|_| (0..40).map(|_| next() as u8).collect()).collect());
995        // Long repeats, so that concatenations reach eight bytes and get cut.
996        shapes.push(
997            (0..200)
998                .map(|index| format!("prefix-{}-suffix-{}", index % 7, index % 5).into_bytes())
999                .collect(),
1000        );
1001        // One after another on one thread, so every table after the first is trained on the counts
1002        // the one before it left behind, which is what a load does block after block.
1003        for strings in &shapes {
1004            let samples = borrow(strings);
1005            assert_eq!(SymbolTable::train(&samples).symbols, train_with_maps(&samples).symbols);
1006        }
1007    }
1008}