r/programming • • Aug 02 '26

Every byte matters

https://fzakaria.com/2026/06/01/every-byte-matters
204 Upvotes

43 comments sorted by

View all comments

Show parent comment

2

u/cdb_11 Aug 03 '26

Yes, they are set-associative caches.

struct Line { u64 tag; u512 data; };
struct Set { Line lines[8]; }; // 8-way associative
Set sets[64];

u512 get(u64 addr) {
  addr /= 64; // ignore data offset
  int idx = addr % 64;
  u64 tag = addr / 64;

  Set* set = &sets[idx];
  for (int i = 0; i < 8; ++i)
    if (set->lines[i].tag == tag) // all slots checked in parallel
      return set->lines[i].data;

  // cache miss
}

Notice it's all power-of-twos. This means that none of those muls and divs actually happen, it's just a representation of extracting bit ranges in a high level language. The hardware can just access the relevant range of bits directly.

This is basically like a hash table, but it's fixed size, the slot count in a bucket (set) is fixed, and the hash function is just taking some range of bits in the address. This means every address has only one possible bucket it can go in. You still have multiple slots in the bucket where it can be placed arbitrarily, but I believe this will usually be something like 4, 8 or 16 slots.