Expand description
Dictionary key encoder for low-cardinality GROUP BY keys. Dictionary key encoder for low-cardinality GROUP BY keys.
Maps variable-length byte keys (e.g., ticker symbols like "AAPL", region
names like "us-east-1") to compact fixed-size u32 codes. This reduces
hash map key sizes from variable-length to 4 bytes, improving:
- Hash throughput: hashing 4 bytes is ~5x faster than hashing 20+ byte strings
- Cache utilization: smaller keys mean more entries per cache line
- Comparison cost:
u32 == u32is a single instruction vsmemcmp
§Architecture
- Ring 2 (setup): Call
DictionaryKeyEncoder::encode_or_insertduring query registration to populate the dictionary with known keys. - Ring 0 (hot path): Call
DictionaryKeyEncoder::encodefor O(1) lookup of pre-populated keys. Unknown keys fall through to the raw-byte path.
§Example
use laminar_core::state::DictionaryKeyEncoder;
let mut encoder = DictionaryKeyEncoder::new();
// Ring 2: populate during query setup
let code_aapl = encoder.encode_or_insert(b"AAPL").unwrap();
let code_goog = encoder.encode_or_insert(b"GOOG").unwrap();
assert_ne!(code_aapl, code_goog);
// Ring 0: fast lookup on hot path
assert_eq!(encoder.encode(b"AAPL"), Some(code_aapl));
assert_eq!(encoder.encode(b"UNKNOWN"), None);
// Decode back to original key
assert_eq!(encoder.decode(code_aapl), Some(b"AAPL".as_slice()));Structs§
- Dictionary
KeyEncoder - Compact dictionary encoder that maps variable-length byte keys to
u32codes.