Can `Hash` be implemented in a way to maximize chances for locality?

⚓ Rust    📅 2026-03-02    👤 surdeus    👁️ 1      

surdeus

Hello, I want to try to avoid writing a custom data structure by abusing HashMap and Hash trait. My problem is, I need to store data hierarchically: DeviceId -> PropertyId -> Value. I would prefer it all to be in a single allocation, so HashMap<(DeviceId, PropertyId), Value> seems nice. But the hash table could be a couple megabytes then - and hashes of different properties of the same device would get spreaded totally different pages.

My idea is to make a type for a key that will preserve random behavior in respect to device id, be be linear I'm respect to properly id, so that, in most cases, those entries get partitioned together in memory.

Is it compatible with the swiss table and how hashbrown implements it? And could you advise on the Hash implementation, if it is possible? Thank you!

EDIT: device IDs are extremely random to begin with

3 posts - 3 participants

Read full topic

🏷️ Rust_feed