I have this very simple POD struct with 3 shorts in it, and I'm planning to use it hundreds of millions of times in a unordered_set
.
Here's is what I'm doing so far:
struct ps3 {
short v1;
short v2;
short v3;
bool operator==(const ps3& other) const {
return v1 == other.v1
&& v2 == other.v2
&& v3 == other.v3;
}
}
// Hash function:
size_t operator()(const ps3& p) const {
return (static_cast<size_t>(*reinterpret_cast<const int*>(&p)) << 16) | p.v3;
}
The hash function simply returns the value of ps3
, as it can fit inside 8 bytes.
(I saw here that for the basic types the standard hash function just returns itself, so if it was a int
with a value of 30, it would return a hash of 30)
I return the value of ps3
by getting the first four bytes, shifting by 16, and then OR
ing the last short.
I was wondering if this method is good, and if there is anything I can do to improve performance (as it is used hundreds of millions of times, potentially billions)
Benchmarks
I did some benchmarking, and heres what I found out:
- Current method: 1076ms
- memcpy: 1500ms
- Combining each short using
hash<short>()
as @rturrado suggested: 876ms