bhc-data-structures
Common data structures for the Basel Haskell Compiler.
Overview
This crate provides type aliases and wrappers around commonly used data structures, ensuring consistent hashing performance across the compiler. It standardizes on FxHash for fast, non-cryptographic hashing.
Key Types
Hash Collections
| Type | Description |
|---|---|
FxHashMap<K, V> |
Hash map using FxHasher (fast, deterministic) |
FxHashSet<T> |
Hash set using FxHasher |
FxIndexMap<K, V> |
Insertion-ordered hash map |
FxIndexSet<T> |
Insertion-ordered hash set |
Small Vectors
| Type | Description |
|---|---|
TinyVec<T> |
SmallVec optimized for 0-4 elements inline |
SmallVec8<T> |
SmallVec optimized for 0-8 elements inline |
Utilities
| Type | Description |
|---|---|
WorkQueue<T> |
Work queue for graph traversals (dedup built-in) |
FrozenMap<K, V> |
Immutable map for read-only access |
UnionFind |
Disjoint set data structure |
Usage
use ;
// Create collections using the extension traits
let mut map: = new;
map.insert;
let mut set: = with_capacity;
set.insert;
// Work queue automatically deduplicates
let mut wq: = new;
assert!; // true: first time
assert!; // false: already seen
assert!; // true: new item
while let Some = wq.pop
Union-Find
use UnionFind;
let mut uf = new;
uf.union;
uf.union;
uf.union;
assert!; // All connected
assert!; // 4 is separate
Why FxHash?
FxHash is optimized for small keys (integers, short strings) which are common in compilers:
- Fast: Simple multiply-xor algorithm
- Deterministic: Same output across runs (unlike RandomState)
- Good enough: Not cryptographic, but suitable for compiler workloads
Re-exports
This crate also re-exports commonly used types:
IndexMap,IndexSetfromindexmapMutex,RwLockfromparking_lotSmallVecfromsmallvec
Design Notes
- Prefer
FxHashMap/FxHashSetoverstd::collections::HashMap/HashSet - Use
FxIndexMap/FxIndexSetwhen iteration order matters - Use
TinyVecfor fields that are usually 0-4 elements - Use
WorkQueuefor BFS/worklist algorithms
Related Crates
bhc-typeck- Uses UnionFind for type unificationbhc-core- Uses FxHashMap for variable environmentsbhc-query- Uses WorkQueue for incremental computation