Get the latest tech news
Engineering a fixed-width bit-packed integer vector in Rust
Design and implementation of a memory-efficient, fixed-width bit-packed integer vector in Rust, with extremely fast random access.
In Rust, the contract of a Vec<T>(where T is a primitive integer type like u64 or i32) is simple: O(1) random access in exchange for a memory layout that is tied to the static size of T. This is a good trade-off, until it isn’t. Modern x86-64 CPUs handle this directly: when an unaligned load instruction is issued, the CPU’s memory controller fetches the necessary cache lines and the load/store unit reassembles the bytes into the target register. The most efficient way to implement this is to load the entire word into a register, perform all bitwise modifications locally, and then write the final result back to memory in a single store operation.
Or read this on Hacker News