Skip to content

Commit

Permalink
Minor performance optiomizations to improve sleep speed (#44759)
Browse files Browse the repository at this point in the history
* Avoid useless bound checks
  • Loading branch information
Farseer2 authored Oct 18, 2020
1 parent 85e8538 commit a6b7ead
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 70 deletions.
14 changes: 9 additions & 5 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6726,15 +6726,19 @@ bool item::is_comestible() const

bool item::is_food() const
{
const std::string comest_type = get_comestible() ? get_comestible()->comesttype : "";
return is_comestible() && ( comest_type == "FOOD" ||
comest_type == "DRINK" );
if( !is_comestible() ) {
return false;
}
const std::string &comest_type = get_comestible()->comesttype;
return comest_type == "FOOD" || comest_type == "DRINK";
}

bool item::is_medication() const
{
const std::string comest_type = get_comestible() ? get_comestible()->comesttype : "";
return is_comestible() && comest_type == "MED";
if( !is_comestible() ) {
return false;
}
return get_comestible()->comesttype == "MED";
}

bool item::is_brewable() const
Expand Down
92 changes: 46 additions & 46 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1329,7 +1329,7 @@ furn_id map::furn( const tripoint &p ) const
}

point l;
submap *const current_submap = get_submap_at( p, l );
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried process furniture at (%d,%d) but the submap is not loaded", l.x, l.y );
return f_null;
Expand All @@ -1345,7 +1345,7 @@ void map::furn_set( const tripoint &p, const furn_id &new_furniture )
}

point l;
submap *const current_submap = get_submap_at( p, l );
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried to set furniture at (%d,%d) but the submap is not loaded", l.x, l.y );
return;
Expand Down Expand Up @@ -1471,7 +1471,7 @@ ter_id map::ter( const tripoint &p ) const
}

point l;
submap *const current_submap = get_submap_at( p, l );
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried to process terrain at (%d,%d) but the submap is not loaded", l.x, l.y );
return t_null;
Expand Down Expand Up @@ -1665,7 +1665,7 @@ bool map::ter_set( const tripoint &p, const ter_id &new_terrain )
}

point l;
submap *const current_submap = get_submap_at( p, l );
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried to set terrain at (%d,%d) but the submap is not loaded", l.x, l.y );
return true;
Expand Down Expand Up @@ -1799,13 +1799,20 @@ bool map::is_wall_adjacent( const tripoint &center ) const

int map::move_cost( const tripoint &p, const vehicle *ignored_vehicle ) const
{
// To save all of the bound checks and submaps fetching, we extract it
// here instead of using furn(), field_at() and ter().
if( !inbounds( p ) ) {
return 0;
}
point l;
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
return 0;
}

const furn_t &furniture = furn( p ).obj();
const ter_t &terrain = ter( p ).obj();
const field &field = field_at( p );
const furn_t &furniture = current_submap->get_furn( l ).obj();
const ter_t &terrain = current_submap->get_ter( l ).obj();
const field &field = current_submap->get_field( l );
const optional_vpart_position vp = veh_at( p );
vehicle *const veh = ( !vp || &vp->vehicle() == ignored_vehicle ) ? nullptr : &vp->vehicle();
const int part = veh ? vp->part_index() : -1;
Expand All @@ -1830,7 +1837,7 @@ int map::move_cost_ter_furn( const tripoint &p ) const
}

point l;
submap *const current_submap = get_submap_at( p, l );
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried process terrain at (%d,%d) but the submap is not loaded", l.x, l.y );
return 0;
Expand Down Expand Up @@ -2353,7 +2360,7 @@ bool map::has_flag_ter_or_furn( const std::string &flag, const tripoint &p ) con
}

point l;
submap *const current_submap = get_submap_at( p, l );
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried to process terrain at (%d,%d) but the submap is not loaded", l.x, l.y );
return false;
Expand Down Expand Up @@ -2385,7 +2392,7 @@ bool map::has_flag_ter_or_furn( const ter_bitflags flag, const tripoint &p ) con
}

point l;
submap *const current_submap = get_submap_at( p, l );
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried to process terrain at (%d,%d) but the submap is not loaded", l.x, l.y );
return false;
Expand Down Expand Up @@ -3619,7 +3626,7 @@ void map::shoot( const tripoint &p, projectile &proj, const bool hit_items )
{
// TODO: Make bashing count fully, but other types much less
float initial_damage = 0.0f;
for( damage_unit dam : proj.impact ) {
for( const damage_unit &dam : proj.impact ) {
initial_damage += dam.amount;
initial_damage += dam.res_pen;
}
Expand Down Expand Up @@ -4056,7 +4063,7 @@ std::string map::get_signage( const tripoint &p ) const
}

point l;
submap *const current_submap = get_submap_at( p, l );
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried to get signage at (%d,%d) but the submap is not loaded", l.x, l.y );
return std::string();
Expand All @@ -4071,7 +4078,7 @@ void map::set_signage( const tripoint &p, const std::string &message ) const
}

point l;
submap *const current_submap = get_submap_at( p, l );
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried set signage at (%d,%d) but the submap is not loaded", l.x, l.y );
return;
Expand All @@ -4086,7 +4093,7 @@ void map::delete_signage( const tripoint &p ) const
}

point l;
submap *const current_submap = get_submap_at( p, l );
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried to delete signage at (%d,%d) but the submap is not loaded", l.x, l.y );
return;
Expand All @@ -4102,7 +4109,7 @@ int map::get_radiation( const tripoint &p ) const
}

point l;
submap *const current_submap = get_submap_at( p, l );
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried to get radiation at (%d,%d) but the submap is not loaded", l.x, l.y );
return 0;
Expand All @@ -4118,7 +4125,7 @@ void map::set_radiation( const tripoint &p, const int value )
}

point l;
submap *const current_submap = get_submap_at( p, l );
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried to set radiation at (%d,%d,%d) but the submap is not loaded", l.x, l.y );
return;
Expand All @@ -4134,7 +4141,7 @@ void map::adjust_radiation( const tripoint &p, const int delta )
}

point l;
submap *const current_submap = get_submap_at( p, l );
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried to adjust radiation at (%d,%d) but the submap is not loaded", l.x, l.y );
return;
Expand All @@ -4150,7 +4157,7 @@ int map::get_temperature( const tripoint &p ) const
return 0;
}

submap *const current_submap = get_submap_at( p );
submap *const current_submap = unsafe_get_submap_at( p );
if( current_submap == nullptr ) {
debugmsg( "Tried to get temperature at (%d,%d,%d) but the submap is not loaded", p.x, p.y, p.z );
return 0;
Expand All @@ -4164,7 +4171,7 @@ void map::set_temperature( const tripoint &p, int new_temperature )
if( !inbounds( p ) ) {
return;
}
submap *const current_submap = get_submap_at( p );
submap *const current_submap = unsafe_get_submap_at( p );
if( current_submap == nullptr ) {
debugmsg( "Tried to set temperature at (%d,%d,%d) but the submap is not loaded", p.x, p.y, p.z );
return;
Expand All @@ -4182,7 +4189,7 @@ map_stack map::i_at( const tripoint &p )
}

point l;
submap *const current_submap = get_submap_at( p, l );
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried to get items at (%d,%d) but the submap is not loaded", l.x, l.y );
nulitems.clear();
Expand Down Expand Up @@ -4443,7 +4450,7 @@ item &map::add_item( const tripoint &p, item new_item )
return null_item_reference();
}
point l;
submap *const current_submap = get_submap_at( p, l );
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried to add items at (%d,%d) but the submap is not loaded", l.x, l.y );
return null_item_reference();
Expand Down Expand Up @@ -4928,7 +4935,7 @@ bool map::has_items( const tripoint &p ) const
}

point l;
submap *const current_submap = get_submap_at( p, l );
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried to check items at (%d,%d) but the submap is not loaded", l.x, l.y );
return false;
Expand Down Expand Up @@ -5142,7 +5149,7 @@ const trap &map::tr_at( const tripoint &p ) const
}

point l;
submap *const current_submap = get_submap_at( p, l );
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried to get trap at (%d,%d) but the submap is not loaded", l.x, l.y );
return tr_null.obj();
Expand All @@ -5161,7 +5168,7 @@ partial_con *map::partial_con_at( const tripoint &p )
return nullptr;
}
point l;
submap *const current_submap = get_submap_at( p, l );
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried to get construction at (%d,%d) but the submap is not loaded", l.x, l.y );
return nullptr;
Expand All @@ -5179,7 +5186,7 @@ void map::partial_con_remove( const tripoint &p )
return;
}
point l;
submap *const current_submap = get_submap_at( p, l );
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried to remove construction at (%d,%d) but the submap is not loaded", l.x, l.y );
return;
Expand All @@ -5193,7 +5200,7 @@ void map::partial_con_set( const tripoint &p, const partial_con &con )
return;
}
point l;
submap *const current_submap = get_submap_at( p, l );
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried to set construction at (%d,%d) but the submap is not loaded", l.x, l.y );
return;
Expand All @@ -5210,7 +5217,7 @@ void map::trap_set( const tripoint &p, const trap_id &type )
}

point l;
submap *const current_submap = get_submap_at( p, l );
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried to set trap at (%d,%d) but the submap is not loaded", l.x, l.y );
return;
Expand Down Expand Up @@ -5240,7 +5247,7 @@ void map::remove_trap( const tripoint &p )
}

point l;
submap *const current_submap = get_submap_at( p, l );
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried to remove trap at (%d,%d) but the submap is not loaded", l.x, l.y );
return;
Expand Down Expand Up @@ -5271,7 +5278,7 @@ const field &map::field_at( const tripoint &p ) const
}

point l;
submap *const current_submap = get_submap_at( p, l );
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried to get field at (%d,%d) but the submap is not loaded", l.x, l.y );
nulfield = field();
Expand All @@ -5292,7 +5299,7 @@ field &map::field_at( const tripoint &p )
}

point l;
submap *const current_submap = get_submap_at( p, l );
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried to get field at (%d,%d,%d) but the submap is not loaded", p.x, p.y, p.z );
nulfield = field();
Expand Down Expand Up @@ -5366,7 +5373,7 @@ field_entry *map::get_field( const tripoint &p, const field_type_id &type )
}

point l;
submap *const current_submap = get_submap_at( p, l );
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried to get field at (%d,%d) but the submap is not loaded", l.x, l.y );
return nullptr;
Expand Down Expand Up @@ -5404,7 +5411,7 @@ bool map::add_field( const tripoint &p, const field_type_id &type_id, int intens
}

point l;
submap *const current_submap = get_submap_at( p, l );
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried to add field at (%d,%d) but the submap is not loaded", l.x, l.y );
return false;
Expand Down Expand Up @@ -5453,7 +5460,7 @@ void map::remove_field( const tripoint &p, const field_type_id &field_to_remove
}

point l;
submap *const current_submap = get_submap_at( p, l );
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried to remove field at (%d,%d) but the submap is not loaded", l.x, l.y );
return;
Expand Down Expand Up @@ -5534,7 +5541,7 @@ computer *map::computer_at( const tripoint &p )
}

point l;
submap *const sm = get_submap_at( p, l );
submap *const sm = unsafe_get_submap_at( p, l );
if( sm == nullptr ) {
debugmsg( "Tried to get computer at (%d,%d) but the submap is not loaded", l.x, l.y );
return nullptr;
Expand Down Expand Up @@ -7677,7 +7684,7 @@ void map::set_graffiti( const tripoint &p, const std::string &contents )
return;
}
point l;
submap *const current_submap = get_submap_at( p, l );
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried to set graffiti at (%d,%d) but the submap is not loaded", l.x, l.y );
return;
Expand All @@ -7691,7 +7698,7 @@ void map::delete_graffiti( const tripoint &p )
return;
}
point l;
submap *const current_submap = get_submap_at( p, l );
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried to delete graffiti at (%d,%d) but the submap is not loaded", l.x, l.y );
return;
Expand All @@ -7706,7 +7713,7 @@ const std::string &map::graffiti_at( const tripoint &p ) const
return empty_string;
}
point l;
submap *const current_submap = get_submap_at( p, l );
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried to get graffiti at (%d,%d) but the submap is not loaded", l.x, l.y );
static const std::string empty_string;
Expand All @@ -7721,7 +7728,7 @@ bool map::has_graffiti_at( const tripoint &p ) const
return false;
}
point l;
submap *const current_submap = get_submap_at( p, l );
submap *const current_submap = unsafe_get_submap_at( p, l );
if( current_submap == nullptr ) {
debugmsg( "Tried to get graffiti at (%d,%d) but the submap is not loaded", l.x, l.y );
return false;
Expand Down Expand Up @@ -8140,14 +8147,7 @@ submap *map::get_submap_at( const tripoint &p ) const
debugmsg( "Tried to access invalid map position (%d, %d, %d)", p.x, p.y, p.z );
return nullptr;
}
return get_submap_at_grid( { p.x / SEEX, p.y / SEEY, p.z } );
}

submap *map::get_submap_at( const tripoint &p, point &offset_p ) const
{
offset_p.x = p.x % SEEX;
offset_p.y = p.y % SEEY;
return get_submap_at( p );
return unsafe_get_submap_at( p );
}

submap *map::get_submap_at_grid( const tripoint &gridp ) const
Expand Down
Loading

0 comments on commit a6b7ead

Please sign in to comment.