From 510fc099ceba7596560769e68e8ae675a4eaaca2 Mon Sep 17 00:00:00 2001 From: psiha Date: Tue, 17 Sep 2024 14:12:48 +0200 Subject: [PATCH] Minor optimization in vector<>::make_space_for_insert(). --- include/psi/vm/vector.hpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/include/psi/vm/vector.hpp b/include/psi/vm/vector.hpp index 7006291..84e72b5 100644 --- a/include/psi/vm/vector.hpp +++ b/include/psi/vm/vector.hpp @@ -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( current_size - position_index ) }; - auto const elements_to_move_to_uninitialized_space{ n }; - auto const elements_to_move_to_the_current_end { static_cast( 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 ) + { + 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( current_size - position_index ) }; + auto const elements_to_move_to_uninitialized_space{ n }; + auto const elements_to_move_to_the_current_end { static_cast( 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