Skip to content

Commit

Permalink
Avoid more temporary items for 10+% load speedup
Browse files Browse the repository at this point in the history
  • Loading branch information
akrieger committed Jan 22, 2024
1 parent 751aac4 commit eb6a7f3
Showing 1 changed file with 43 additions and 27 deletions.
70 changes: 43 additions & 27 deletions src/item_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,30 +187,44 @@ item Single_item_creator::create_single( const time_point &birthday, RecursionLi
item Single_item_creator::create_single_without_container( const time_point &birthday,
RecursionList &rec ) const
{
item tmp;
if( type == S_ITEM ) {
if( id == "corpse" ) {
tmp = item::make_corpse( mtype_id::NULL_ID(), birthday );
} else {
tmp = item( id, birthday );
}
} else if( type == S_ITEM_GROUP ) {
// Check direct return conditions first.
if( type == S_NONE ) {
return item( null_item_id, birthday );
}
Item_spawn_data *isd = nullptr;
if( type == S_ITEM_GROUP ) {
item_group_id group_id( id );
if( std::find( rec.begin(), rec.end(), group_id ) != rec.end() ) {
debugmsg( "recursion in item spawn list %s", id.c_str() );
return item( null_item_id, birthday );
}
rec.push_back( group_id );
Item_spawn_data *isd = item_controller->get_group( group_id );
isd = item_controller->get_group( group_id );
if( isd == nullptr ) {
debugmsg( "unknown item spawn list %s", id.c_str() );
return item( null_item_id, birthday );
}
tmp = isd->create_single( birthday, rec );
rec.erase( rec.end() - 1 );
} else if( type == S_NONE ) {
return item( null_item_id, birthday );
}

item tmp = ( [&]() -> item {
if( isd )
{
on_out_of_scope scope{
[&]{
rec.erase( rec.end() - 1 );
}} ;
return isd->create_single( birthday, rec );
} else
{
cata_assert( type == S_ITEM );
if( id == "corpse" ) {
return item::make_corpse( mtype_id::NULL_ID(), birthday );
} else {
return item( id, birthday );
}
}
} )();

if( one_in( 3 ) && tmp.has_flag( flag_VARSIZE ) ) {
tmp.set_flag( flag_FIT );
}
Expand Down Expand Up @@ -507,13 +521,14 @@ void Item_modifier::modify( item &new_item, const std::string &context ) const

{
// create container here from modifier or from default to get max charges later
item cont;
std::optional<item> cont;
if( container != nullptr ) {
cont = container->create_single( new_item.birthday() );
} else if( new_item.type->default_container.has_value() ) {
} else if( new_item.type->default_container.has_value() &&
!new_item.type->default_container->is_null() ) {
cont = item( *new_item.type->default_container, new_item.birthday() );
if( new_item.type->default_container_variant.has_value() ) {
cont.set_itype_variant( *new_item.type->default_container_variant );
cont->set_itype_variant( *new_item.type->default_container_variant );
}
}

Expand All @@ -539,13 +554,14 @@ void Item_modifier::modify( item &new_item, const std::string &context ) const
}
}

if( max_capacity == -1 && !cont.is_null() && ( new_item.made_of( phase_id::LIQUID ) ||
( !new_item.is_tool() && !new_item.is_gun() && !new_item.is_magazine() ) ) ) {
if( max_capacity == -1 && cont.has_value() && !cont->is_null() &&
( new_item.made_of( phase_id::LIQUID ) ||
( !new_item.is_tool() && !new_item.is_gun() && !new_item.is_magazine() ) ) ) {
if( new_item.type->weight == 0_gram ) {
max_capacity = new_item.charges_per_volume( cont.get_total_capacity() );
max_capacity = new_item.charges_per_volume( cont->get_total_capacity() );
} else {
max_capacity = std::min( new_item.charges_per_volume( cont.get_total_capacity() ),
new_item.charges_per_weight( cont.get_total_weight_capacity() ) );
max_capacity = std::min( new_item.charges_per_volume( cont->get_total_capacity() ),
new_item.charges_per_weight( cont->get_total_weight_capacity() ) );
}
}

Expand All @@ -570,7 +586,7 @@ void Item_modifier::modify( item &new_item, const std::string &context ) const

ch = charges_min == charges_max ? charges_min : rng( charges_min,
charges_max );
} else if( !cont.is_null() && new_item.made_of( phase_id::LIQUID ) ) {
} else if( cont.has_value() && !cont->is_null() && new_item.made_of( phase_id::LIQUID ) ) {
new_item.charges = std::max( 1, max_capacity );
}

Expand Down Expand Up @@ -647,11 +663,11 @@ void Item_modifier::modify( item &new_item, const std::string &context ) const
}
}

if( !cont.is_null() ) {
const pocket_type pk_type = guess_pocket_for( cont, new_item );
cont.put_in( new_item, pk_type );
cont.add_automatic_whitelist();
new_item = std::move( cont );
if( cont.has_value() && !cont->is_null() ) {
const pocket_type pk_type = guess_pocket_for( *cont, new_item );
cont->put_in( new_item, pk_type );
cont->add_automatic_whitelist();
new_item = std::move( *cont );
if( sealed ) {
new_item.seal();
}
Expand Down

0 comments on commit eb6a7f3

Please sign in to comment.