Skip to content

Commit

Permalink
perf: port std::moving items (#2532)
Browse files Browse the repository at this point in the history
* Use move semantics in profession::items() (#55127)

* Use move semantics in inventory::add_item() (#55100)

---------

Co-authored-by: Binrui Dong <brett.browning.dong@gmail.com>
  • Loading branch information
scarf005 and BrettDong authored Apr 6, 2023
1 parent 363d3dd commit 5ca0c82
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ item &inventory::add_item( item newit, bool keep_invlet, bool assign_invlet, boo
} else {
newit.invlet = it_ref->invlet;
}
elem.push_back( newit );
elem.emplace_back( std::move( newit ) );
return elem.back();
} else if( keep_invlet && assign_invlet && it_ref->invlet == newit.invlet &&
it_ref->invlet != '\0' ) {
Expand All @@ -321,7 +321,7 @@ item &inventory::add_item( item newit, bool keep_invlet, bool assign_invlet, boo
}
update_cache_with_item( newit );

items.push_back( {newit} );
items.emplace_back( std::list<item> { std::move( newit ) } );
return items.back().back();
}

Expand Down
14 changes: 8 additions & 6 deletions src/profession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,12 +400,14 @@ std::list<item> profession::items( bool male, const std::vector<trait_id> &trait
add_legacy_items( legacy_starting_items );
add_legacy_items( male ? legacy_starting_items_male : legacy_starting_items_female );

const std::vector<item> group_both = item_group::items_from( _starting_items,
advanced_spawn_time() );
const std::vector<item> group_gender = item_group::items_from( male ? _starting_items_male :
_starting_items_female, advanced_spawn_time() );
result.insert( result.begin(), group_both.begin(), group_both.end() );
result.insert( result.begin(), group_gender.begin(), group_gender.end() );
std::vector<item> group_both = item_group::items_from( _starting_items,
advanced_spawn_time() );
std::vector<item> group_gender = item_group::items_from( male ? _starting_items_male :
_starting_items_female, advanced_spawn_time() );
result.insert( result.begin(), std::make_move_iterator( group_both.begin() ),
std::make_move_iterator( group_both.end() ) );
result.insert( result.begin(), std::make_move_iterator( group_gender.begin() ),
std::make_move_iterator( group_gender.end() ) );

std::vector<itype_id> bonus = item_substitutions.get_bonus_items( traits );
for( const itype_id &elem : bonus ) {
Expand Down

0 comments on commit 5ca0c82

Please sign in to comment.