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.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of Rust

Rust

Photo of width bit

width bit

Related news:

News photo

SedonaDB: A new geospatial DataFrame library written in Rust

News photo

From Rust to reality: The hidden journey of fetch_max

News photo

Zig feels more practical than Rust for real-world CLI tools