From 09f8468acffd281d566f9c38a08c46f9d9345e8b Mon Sep 17 00:00:00 2001 From: Andrew Duffy Date: Tue, 20 Aug 2024 12:48:44 -0400 Subject: [PATCH] doc --- src/lib.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 31c5482..82ed0d7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -332,13 +332,16 @@ pub struct Compressor { impl Default for Compressor { fn default() -> Self { + // NOTE: `vec!` has a specialization for building a new vector of `0u64`. Because Symbol and u64 + // have the same bit pattern, we can allocate as u64 and transmute. If we do `vec![Symbol::EMPTY; N]`, + // that will create a new Vec and call `Symbol::EMPTY.clone()` `N` times which is considerably slower. let symbols = vec![0u64; 511]; // SAFETY: transmute safety assured by the compiler. let symbols: Vec = unsafe { std::mem::transmute(symbols) }; let mut table = Self { symbols, n_symbols: 0, - codes_twobyte: [CodeMeta::EMPTY].repeat(65_536), + codes_twobyte: vec![CodeMeta::EMPTY; 65_536], lossy_pht: LossyPHT::new(), };