Expand description
Frozen version of Rust standard library’s hashbrown.
§What is this about
- Allow you to dump the memory of a
std::collections::HashMapinto a blob - Load the blob and re-construct the hashmap
- Such that we can iterate through the data!
§What is this for
- It’s used in FireDBG to allow us to capture and render
HashMap - It could also probably allow us to extract
HashMapfromcoredump
§How it works
Online
- Construct
TableLayoutfor(K, V) - Extract
ctrlandbucket_mask calculate_layout_for(buckets)and calculate the addressNonNull<u8>andLayout- Dump the memory into a blob
Offline
- Load the blob into memory
- Re-construct
hashbrown::map::HashMapfor(K, V) - Ready to serve
§Why does it work
- The
HashMapin Rust’s standard library is a flat hashmap. Meaning it’s only backed by a single contiguous piece of memory. - It’s dense for small maps and is very memory efficient
- It’s more like a glorified
Vec<(K, V)>with an index to assist hash key lookup
§How to use
use frozen_hashbrown::FrozenHashMap;
use std::collections::HashMap;
let map: HashMap<char, i32> = [('a', 1), ('b', 2), ('c', 3), ('d', 4)]
.into_iter()
.collect();
let snapshot = format!("{map:?}");
let frozen = FrozenHashMap::construct(&map);
std::mem::drop(map);
let frozen: Vec<u8> = frozen.store();
let mut unfrozen = FrozenHashMap::load(&frozen).expect("Failed to load");
let unfrozen = unfrozen
.reconstruct::<char, i32>()
.expect("Failed to reconstruct");
let unfrozen_snapshot = format!("{unfrozen:?}");
// even the "random" iteration order holds
assert_eq!(snapshot, unfrozen_snapshot);More examples under https://github.com/tyt2y3/frozen-hashbrown/blob/main/tests/unfreeze.rs
§
Structs§
- Frozen
Hash Map - Group
- HashMap
- Random
State - RawBucket
Iter - An iterator that yields raw pointers to buckets
- RawTable
- RawTable
Inner - Table
Layout