Precomputed Map
Precomputed a Map to embed in binary.
What and Why
The tool attempts to achieve the best in terms of runtime performance (include startup time and single query time), product size, and compilation speed. and avoid any heavyweight dependency libraries and unsafe code, make it possible to be used without supply chain burden.
Under the hood, we use binary search or phf for different sizes of data. For small data, the runtime performance should be faster than a typical hashmap, and there is almost no size overhead. For large data, it only requires one additional memory access at runtime, and only 1 byte of size overhead for every 3 entries.
At same time, in order to ensure the compilation speed, the tool supports packaging a large number of strings into a single blob. this will greatly improve the compilation performance and product size.
When there are 200,000 entries, the demo has the following comparison data:
| startup | single query | compile | size (stripped) | |
|---|---|---|---|---|
| naive | 6.6ms | 130ns | 6.9s | 14M |
| precomputed | _ | 156ns | 0.3s | 6.3M |
- Data from my Arch Linux (i7-13700H)
Usage
For small amounts of data, it is suitable to be executed in build.rs.
For large amounts of data, it is better to have a separate xtask stage
to generate code without affecting development performance.
let keys: & = ...;
let values: & = ...;
// compute map
let mapout = new
.set_seed
.set_ord
.set_hash
.build
.unwrap;
// generate code
let mut builder = new;
let k = builder.create_str_seq.unwrap;
let v = builder.create_str_seq.unwrap;
let pair = builder.create_pair;
mapout.create_map.unwrap;
let mut codeout = create.unwrap;
builder.write_to.unwrap;
License
This project is licensed under the MIT license.