Skip to content

Commit

Permalink
Fix point hash to be well distributed.
Browse files Browse the repository at this point in the history
  • Loading branch information
akrieger committed Sep 5, 2024
1 parent 9c09701 commit bcac8ac
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
12 changes: 6 additions & 6 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7778,17 +7778,17 @@ bool map::sees( const tripoint_bub_ms &F, const tripoint_bub_ms &T, const int ra

// TODO: Change this to a hash function on the map implementation. This will also allow us to
// account for the complete lack of entropy in the top 16 bits.
int64_t map::sees_cache_key( const tripoint_bub_ms &from, const tripoint_bub_ms &to ) const
point map::sees_cache_key( const tripoint_bub_ms &from, const tripoint_bub_ms &to ) const
{
// Canonicalize the order of the tripoints so the cache is reflexive.
const tripoint_bub_ms &min = from < to ? from : to;
const tripoint_bub_ms &max = !( from < to ) ? from : to;

// A little gross, just pack the values into an integer.
return
static_cast<int64_t>( min.x() ) << 50 | static_cast<int64_t>( min.y() ) << 40 |
( static_cast<int64_t>( min.z() ) + OVERMAP_DEPTH ) << 30 | max.x() << 20 | max.y() << 10 |
( max.z() + OVERMAP_DEPTH );
point( static_cast<int64_t>( min.x() ) << 50 | static_cast<int64_t>( min.y() ) << 40 |
( static_cast<int64_t>( min.z() ) + OVERMAP_DEPTH ) << 30, max.x() << 20 | max.y() << 10 |
( max.z() + OVERMAP_DEPTH ) );
}

/**
Expand All @@ -7812,7 +7812,7 @@ bool map::sees( const tripoint_bub_ms &F, const tripoint_bub_ms &T, const int ra
bresenham_slope = 0;
return false; // Out of range!
}
const int64_t key = sees_cache_key( F, T );
const point key = sees_cache_key( F, T );
if( allow_cached ) {
char cached = skew_cache.get( key, -1 );
if( cached != -1 ) {
Expand Down Expand Up @@ -11116,7 +11116,7 @@ void map::invalidate_max_populated_zlev( int zlev )

bool map::has_potential_los( const tripoint_bub_ms &from, const tripoint_bub_ms &to ) const
{
const int64_t key = sees_cache_key( from, to );
const point key = sees_cache_key( from, to );
char cached = skew_vision_cache.get( key, -1 );
if( cached != -1 ) {
return cached > 0;
Expand Down
4 changes: 2 additions & 2 deletions src/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ class map
bool with_fields = true ) const;
bool sees( const tripoint_bub_ms &F, const tripoint_bub_ms &T, int range, int &bresenham_slope,
bool with_fields = true, bool allow_cached = true ) const;
int64_t sees_cache_key( const tripoint_bub_ms &from, const tripoint_bub_ms &to ) const;
point sees_cache_key( const tripoint_bub_ms &from, const tripoint_bub_ms &to ) const;
public:
/**
* Returns coverage of target in relation to the observer. Target is loc2, observer is loc1.
Expand Down Expand Up @@ -2602,7 +2602,7 @@ class map
/**
* Cache of coordinate pairs recently checked for visibility.
*/
using lru_cache_t = lru_cache<int64_t, char>;
using lru_cache_t = lru_cache<point, char>;
mutable lru_cache_t skew_vision_cache;
mutable lru_cache_t skew_vision_wo_fields_cache;

Expand Down
41 changes: 29 additions & 12 deletions src/point.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,20 @@ namespace std
template <>
struct hash<point> {
std::size_t operator()( const point &k ) const noexcept {
constexpr uint64_t a = 2862933555777941757;
size_t result = k.y;
result *= a;
result += k.x;
return result;
// We cast k.y to uint32_t because otherwise when promoting to uint64_t for binary `or` it
// will sign extend and turn the upper 32 bits into all 1s.
uint64_t x = static_cast<uint64_t>( k.x ) << 32 | static_cast<uint32_t>( k.y );

// Found through https://nullprogram.com/blog/2018/07/31/
// Public domain source from https://xoshiro.di.unimi.it/splitmix64.c
x ^= x >> 30;
x *= 0xbf58476d1ce4e5b9U;
x ^= x >> 27;
x *= 0x94d049bb133111ebU;
x ^= x >> 31;
return x;

return x;
}
};
} // namespace std
Expand All @@ -319,13 +328,21 @@ namespace std
template <>
struct hash<tripoint> {
std::size_t operator()( const tripoint &k ) const noexcept {
constexpr uint64_t a = 2862933555777941757;
size_t result = k.z;
result *= a;
result += k.y;
result *= a;
result += k.x;
return result;
// We cast k.y to uint32_t because otherwise when promoting to uint64_t for binary `or` it
// will sign extend and turn the upper 32 bits into all 1s.
uint64_t x = static_cast<uint64_t>( k.x ) << 32 | static_cast<uint32_t>( k.y );

// Found through https://nullprogram.com/blog/2018/07/31/
// Public domain source from https://xoshiro.di.unimi.it/splitmix64.c
x ^= x >> 30;
x *= 0xbf58476d1ce4e5b9U;
x ^= x >> 27;

// Sprinkle in z now.
x ^= static_cast<uint64_t>( k.z );
x *= 0x94d049bb133111ebU;
x ^= x >> 31;
return x;
}
};
} // namespace std
Expand Down

0 comments on commit bcac8ac

Please sign in to comment.