Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

RocksDB Blog 9 min read Deep dive hardMaster

BitFields API: Type-Safe Bit Packing for Lock-Free Data Structures

RocksDB's BitFields API is a template-based C++ abstraction that declares multiple logical fields packed into one atomic integer, replacing hand-written shifts and masks with typed Get/Set accessors and composable "transforms". Additions/subtractions, bitwise-and or bitwise-or updates across several fields compile to a single fetch_add/fetch_and/fetch_or without CAS, with debug-build overflow checks and compile-time errors for misuse. Refactoring HyperClockCache's 64-bit slot metadata (two 30-bit counters plus state and hit bits) showed no runtime overhead, with identical assembly in one case. Companion RelaxedAtomic/Atomic wrappers make memory ordering explicit instead of defaulting to seq_cst, and a variant has been proposed for folly.

Assumes: C++ templatesstd::atomic and memory orderinglock-free programmingbit manipulation

Related posts

  • Evolving our real-time timeseries storage again: Built in Rust for performance at scale

    Datadog Engineering hardMaster

    Datadog's sixth-generation real-time timeseries database (RTDB) is a Rust rewrite that unifies two earlier engines, one for scalar metrics and one for DDSketch distributions, after earlier generations built on Cassandra, Redis, MDBM, a Go B+ tree and RocksDB. Its storage engine, Monocle, is an LSM tree with a worker-per-shard, thread-per-core design. Kafka intake tasks route data over per-shard MPSC channels to single-threaded Tokio shards, which keeps locks off the write path. Permit-based throttling and CoDel-based cost scheduling keep the system stable under load, and snapshots provide recovery. Datadog reports 60x higher ingestion throughput and 5x faster queries at peak scale.

  • Supporting faster file load times with memory optimizations in Rust

    Figma mediumAdept

    Figma's Rust multiplayer server represented each file node as a BTreeMap from u16 property IDs to pointers, and memory profiling showed this map used over 60% of a file's memory. Because the schema has fewer than 200 fields and nodes average about 60 keys, the team swapped it for a flat sorted vector. Despite worse big-O complexity, the cache-friendly layout cut p99 deserialization time by 20% and fleet memory by about 20%. A further experiment stored the field ID in the unused top 16 bits of x86-64 pointers, saving only about 5% more RSS. That change was not shipped because of the risk of getting reference counts wrong and corrupting memory.

  • Eliminating Data Races in Firefox – A Technical Report

    Mozilla Hacks hardMaster

    Mozilla deployed ThreadSanitizer across Firefox's C/C++ and Rust code, using a compiled-in suppression list to gate new races in CI while legacy ones were triaged toward a "no data races" policy. So-called benign races are easily misclassified, yet fixing them proved cheap: most fixes were atomics conversions, suppressions or code removal. Notable finds include lost writes from adjacent bitfields (fixed with a MOZ_ATOMIC_BITFIELDS macro), a PreferencesWriter race likely corrupting prefs.js, and fragile late-validated racy reads. Rust needed -Zbuild-std hacks for instrumentation but surfaced only two pure-Rust races, in parking_lot and a WebRender shim, both in unsafe low-level code.

  • 64-Bit Bank Balances ‘Ought to be Enough for Anybody’?

    TigerBeetle mediumAdept

    TigerBeetle moved all amounts and balances from 64-bit to 128-bit unsigned integers because sub-cent precision (micro-cents, fuel prices, stock ticks, micropayments, digital currencies), long-lived accumulating balances and even hyperinflation can exceed 64-bit range. Ledgers use integer multiples of a user-defined minimal unit, never floats, and keep separate strictly positive debit and credit totals instead of signed balances. Generated x86-64 code shows 128-bit add, subtract, multiply and compare compile to a few carry/borrow instructions, so the overhead is immaterial. Only division needs a software routine. Since most client languages lack native 128-bit types, clients expose values as pairs of stack-allocated 64-bit integers, with helpers to convert to BigInteger, byte arrays or UUIDs.

  • RocksDB development finds a CPU bug

    RocksDB Blog mediumAdept

    A RocksDB unit test that generated thousands of SST file unique IDs across many threads exposed a hardware bug: the RDSEED instruction on a newer AMD CPU sometimes returned 0 while reporting success, but only on some cores and only under memory load. The failure appeared only through libstdc++'s std::random_device, not through rdrand, /dev/urandom or libc++. It was reproduced by scaling the thread count to roughly the core count, and was mitigated with a Linux kernel patch that marks RDSEED unavailable, followed by an AMD microcode update tracked as a high-severity CVE. The takeaways are to test what you depend on, combine redundant entropy sources, and add sanity checks, because even CPUs can be wrong.

The quest for software engineering knowledge.

Look:
Layout: