Skip to content

Commit

Permalink
Enable performance-inefficient-vector-operation (#48480)
Browse files Browse the repository at this point in the history
* Add a collection of `vector::reserve` calls.

All changes done automatically by clang-tidy.

* Enable performance-inefficient-vector-operation
  • Loading branch information
jbytheway authored and ZhilkinSerg committed Jun 21, 2021
1 parent cea2a30 commit 4c8a6f7
Show file tree
Hide file tree
Showing 16 changed files with 18 additions and 1 deletion.
1 change: 0 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ readability-*,\
-modernize-return-braced-init-list,\
-modernize-use-default-member-init,\
-modernize-use-emplace,\
-performance-inefficient-vector-operation,\
-performance-implicit-conversion-in-loop,\
-performance-inefficient-string-concatenation,\
-performance-type-promotion-in-math-fn,\
Expand Down
1 change: 1 addition & 0 deletions src/activity_item_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2526,6 +2526,7 @@ static requirement_check_result generic_multi_activity_check_requirement( player
// come back here after successfully fetching your stuff
if( act_prev.coords.empty() ) {
std::vector<tripoint> local_src_set;
local_src_set.reserve( src_set.size() );
for( const tripoint &elem : src_set ) {
local_src_set.push_back( here.getlocal( elem ) );
}
Expand Down
1 change: 1 addition & 0 deletions src/cata_tiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3928,6 +3928,7 @@ std::vector<options_manager::id_and_option> cata_tiles::build_display_list()
};

int numdisplays = SDL_GetNumVideoDisplays();
display_names.reserve( numdisplays );
for( int i = 0 ; i < numdisplays ; i++ ) {
display_names.emplace_back( std::to_string( i ), no_translation( SDL_GetDisplayName( i ) ) );
}
Expand Down
2 changes: 2 additions & 0 deletions src/debug_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1956,6 +1956,7 @@ static void debug_menu_game_state()
tripoint abs_sub = here.get_abs_sub();
std::string mfus;
std::vector<std::pair<m_flag, int>> sorted;
sorted.reserve( m_flag::MF_MAX );
for( int f = 0; f < m_flag::MF_MAX; f++ ) {
sorted.push_back( {static_cast<m_flag>( f ), MonsterGenerator::generator().m_flag_usage_stats[f]} );
}
Expand Down Expand Up @@ -1984,6 +1985,7 @@ static void debug_menu_game_state()

if( !creature_counts.empty() ) {
std::vector<std::pair<std::string, int>> creature_names_sorted;
creature_names_sorted.reserve( creature_counts.size() );
for( const std::pair<const std::string, int> &it : creature_counts ) {
creature_names_sorted.emplace_back( it );
}
Expand Down
1 change: 1 addition & 0 deletions src/faction_camp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ recipe_id base_camps::select_camp_option( const std::map<recipe_id, translation>
std::vector<std::string> pos_names;
int choice = 0;

pos_names.reserve( pos_options.size() );
for( const auto &it : pos_options ) {
pos_names.push_back( it.second.translated() );
}
Expand Down
1 change: 1 addition & 0 deletions src/inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ void inventory::form_from_zone( map &m, std::unordered_set<tripoint> &zone_pts,
bool assign_invlet )
{
std::vector<tripoint> pts;
pts.reserve( zone_pts.size() );
for( const tripoint &elem : zone_pts ) {
pts.push_back( m.getlocal( elem ) );
}
Expand Down
1 change: 1 addition & 0 deletions src/item_contents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ bool pocket_favorite_callback::key( const input_context &, const input_event &ev
}

std::vector<std::string> itype_initializer;
itype_initializer.reserve( nearby_itypes.size() );
for( const std::pair<const std::string, const itype *> &name : nearby_itypes ) {
itype_initializer.emplace_back( name.first );
}
Expand Down
1 change: 1 addition & 0 deletions src/magic_spell_effect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,7 @@ void spell_effect::dash( const spell &sp, Creature &caster, const tripoint &targ
::map &here = get_map();
// uses abs() coordinates
std::vector<tripoint> trajectory;
trajectory.reserve( trajectory_local.size() );
for( const tripoint &local_point : trajectory_local ) {
trajectory.push_back( here.getabs( local_point ) );
}
Expand Down
1 change: 1 addition & 0 deletions src/mission_companion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2054,6 +2054,7 @@ npc_ptr talk_function::companion_choose_return( const tripoint_abs_omt &omt_pos,
}

std::vector<std::string> npcs;
npcs.reserve( available.size() );
for( auto &elem : available ) {
npcs.push_back( ( elem )->name );
}
Expand Down
1 change: 1 addition & 0 deletions src/newcharacter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1448,6 +1448,7 @@ tab_direction set_traits( avatar &u, points_left &points )
// Grab a list of the names of the bionics that block this trait
// So that the player know what is preventing them from taking it
std::vector<std::string> conflict_names;
conflict_names.reserve( cbms_blocking_trait.size() );
for( const bionic_id &conflict : cbms_blocking_trait ) {
conflict_names.emplace_back( conflict->name.translated() );
}
Expand Down
1 change: 1 addition & 0 deletions src/npc.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ class job_data
const std::pair<activity_id, int> &b ) {
return a.second > b.second;
} );
ret.reserve( pairs.size() );
for( const std::pair<activity_id, int> &elem : pairs ) {
ret.push_back( elem.first );
}
Expand Down
1 change: 1 addition & 0 deletions src/requirements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const requirement_data &string_id<requirement_data>::obj() const
std::vector<requirement_data> requirement_data::get_all()
{
std::vector<requirement_data> ret;
ret.reserve( requirements_all.size() );
for( const std::pair<const requirement_id, requirement_data> &pair : requirements_all ) {
ret.push_back( pair.second );
}
Expand Down
1 change: 1 addition & 0 deletions tests/encumbrance_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ static void test_encumbrance(
{
CAPTURE( clothing_types );
std::vector<item> clothing;
clothing.reserve( clothing_types.size() );
for( const std::string &type : clothing_types ) {
clothing.push_back( item( type ) );
}
Expand Down
1 change: 1 addition & 0 deletions tests/generic_factory_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ TEST_CASE( "string_and_int_ids_benchmark", "[.][generic_factory][int_id][string_
}
const int test_flags_size = test_flags.size();
std::vector<dyn_str_id > test_dyn_str_ids;
test_dyn_str_ids.reserve( test_flags.size() );
for( const auto &f : test_flags ) {
test_dyn_str_ids.push_back( dyn_str_id( f.str() ) );
}
Expand Down
1 change: 1 addition & 0 deletions tests/ranged_balance_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ static std::vector<firing_statistics> firing_test( const dispersion_sources &dis
const int range, const std::vector< Threshold > &thresholds )
{
std::vector<firing_statistics> firing_stats;
firing_stats.reserve( thresholds.size() );
for( const Threshold &pear : thresholds ) {
firing_stats.push_back( firing_test( dispersion, range, pear ) );
}
Expand Down
3 changes: 3 additions & 0 deletions tests/string_ids_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ TEST_CASE( "string_ids_intern_test", "[string_id]" )
struct test_obj {};
std::vector<string_id<test_obj>> ids;
// lots of ids to make sure that "interning" map gets expanded
ids.reserve( num_ids );
for( int i = 0; i < num_ids; ++i ) {
ids.push_back( string_id<test_obj>( "test_id" + std::to_string( i ) ) );
}
Expand Down Expand Up @@ -119,6 +120,7 @@ TEST_CASE( "string_id_sorting_test", "[string_id]" )

SECTION( "vector ids sorting" ) {
std::vector<id> vec;
vec.reserve( 10 );
for( int i = 0; i < 10; ++i ) {
vec.push_back( id( "id" + std::to_string( i ) ) );
}
Expand Down Expand Up @@ -150,6 +152,7 @@ TEST_CASE( "string_id_creation_benchmark", "[.][string_id][benchmark]" )
{
static constexpr int num_test_strings = 30;
std::vector<std::string> test_strings;
test_strings.reserve( num_test_strings );
for( int i = 0; i < num_test_strings; ++i ) {
test_strings.push_back( "some_test_string_" + std::to_string( i ) );
}
Expand Down

0 comments on commit 4c8a6f7

Please sign in to comment.