Skip to content

Commit

Permalink
Improve performance in npc::has_potential_los (CleverRaven#76009)
Browse files Browse the repository at this point in the history
  • Loading branch information
CLIDragon authored Sep 1, 2024
1 parent f59d7e9 commit ea87d2d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
23 changes: 12 additions & 11 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7776,18 +7776,19 @@ bool map::sees( const tripoint_bub_ms &F, const tripoint_bub_ms &T, const int ra
return sees( F.raw(), T.raw(), range, dummy, with_fields );
}

point map::sees_cache_key( const tripoint_bub_ms &from, const tripoint_bub_ms &to ) const
// 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
{

// 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 a point.
return point(
min.x() << 16 | min.y() << 8 | ( min.z() + OVERMAP_DEPTH ),
max.x() << 16 | max.y() << 8 | ( max.z() + OVERMAP_DEPTH )
);
// 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 );
}

/**
Expand All @@ -7811,10 +7812,10 @@ 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 point key = sees_cache_key( F, T );
const int64_t key = sees_cache_key( F, T );
if( allow_cached ) {
char cached = skew_cache.get( key, -1 );
if( cached >= 0 ) {
if( cached != -1 ) {
return cached > 0;
}
}
Expand Down Expand Up @@ -11115,9 +11116,9 @@ 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 point key = sees_cache_key( from, to );
const int64_t key = sees_cache_key( from, to );
char cached = skew_vision_cache.get( key, -1 );
if( cached >= 0 ) {
if( cached != -1 ) {
return cached > 0;
}
return true;
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;
point sees_cache_key( const tripoint_bub_ms &from, const tripoint_bub_ms &to ) const;
int64_t 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<point, char>;
using lru_cache_t = lru_cache<int64_t, char>;
mutable lru_cache_t skew_vision_cache;
mutable lru_cache_t skew_vision_wo_fields_cache;

Expand Down

0 comments on commit ea87d2d

Please sign in to comment.