logoalt Hacker News

judofyryesterday at 4:35 PM4 repliesview on HN

Is there a specific reason to store the key + value as an `uint64_t` instead of just using a struct like this?

    struct slot {
      uint32_t key;
      uint32_t value;
    }

Replies

nitnelaveyesterday at 4:52 PM

The alignment constraint is different, which they use to be able to load both as a 64-bit integer and compare to 0 (the empty slot).

You could work around that with a union or casts with explicit alignment constraints, but this is the shortest way to express that.

show 3 replies
zimpenfishyesterday at 4:48 PM

Maybe trying to avoid struct padding? Although having done a quick test on {arm64, amd64} {gcc, clang}, they all give the same `sizeof` for a struct with 2x`uint32_t`, a struct with a single `uint64_t`, or a bare `uint64_t`.

show 1 reply
loegyesterday at 7:26 PM

No real reason. Slightly terser to compare with zero to find an empty slot.

mwkaufmayesterday at 10:09 PM

Or better, just store keys and values in separate arrays, so you can have compact cache lines of just keys when probing.