Skip to content

Commit

Permalink
Minor optimization in vector<>::make_space_for_insert().
Browse files Browse the repository at this point in the history
  • Loading branch information
psiha committed Sep 17, 2024
1 parent cb49c28 commit 510fc09
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions include/psi/vm/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1185,11 +1185,18 @@ class vector
auto const current_size { size() };
auto const new_size { current_size + n };
storage_.expand( to_byte_sz( new_size ) );
auto const elements_to_move { static_cast<size_type>( current_size - position_index ) };
auto const elements_to_move_to_uninitialized_space{ n };
auto const elements_to_move_to_the_current_end { static_cast<size_type>( elements_to_move - elements_to_move_to_uninitialized_space ) };
std::uninitialized_move_n( nth( current_size - elements_to_move_to_uninitialized_space ), elements_to_move_to_uninitialized_space, nth( current_size ) );
std::move ( nth( position_index ), nth( position_index + elements_to_move_to_the_current_end ) , nth( position_index + n ) );
if constexpr ( is_trivially_moveable<T> )
{
std::move( nth( position_index ), nth( position_index + n ), nth( position_index + n ) );
}
else // future support for generic types
{
auto const elements_to_move { static_cast<size_type>( current_size - position_index ) };
auto const elements_to_move_to_uninitialized_space{ n };
auto const elements_to_move_to_the_current_end { static_cast<size_type>( elements_to_move - elements_to_move_to_uninitialized_space ) };
std::uninitialized_move_n( nth( current_size - elements_to_move_to_uninitialized_space ), elements_to_move_to_uninitialized_space, nth( current_size ) );
std::move ( nth( position_index ), nth( position_index + elements_to_move_to_the_current_end ) , nth( position_index + n ) );
}
return nth( position_index );
}
}; // class vector
Expand Down

0 comments on commit 510fc09

Please sign in to comment.