Skip to main content

Module dict_encoder

Module dict_encoder 

Source
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 == u32 is a single instruction vs memcmp

§Architecture

§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§

DictionaryKeyEncoder
Compact dictionary encoder that maps variable-length byte keys to u32 codes.