From edb20cca0f4698033c6fb302f402c9729e2e8759 Mon Sep 17 00:00:00 2001 From: Frederick Roy Date: Tue, 17 Mar 2026 10:58:52 +0900 Subject: [PATCH] avoid convert if the given types are the same --- Sofa/framework/Type/src/sofa/type/Vec.h | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/Sofa/framework/Type/src/sofa/type/Vec.h b/Sofa/framework/Type/src/sofa/type/Vec.h index db5f39260cb..315c9d0ed90 100644 --- a/Sofa/framework/Type/src/sofa/type/Vec.h +++ b/Sofa/framework/Type/src/sofa/type/Vec.h @@ -817,15 +817,25 @@ template ) constexpr auto toVecN(const sofa::type::Vec& in, const OutReal filler = static_cast(0)) -> sofa::type::Vec { - sofa::type::Vec out(type::NOINIT); - std::copy(in.begin(), in.begin() + std::min(InSize, OutSize), out.begin()); - - if constexpr(OutSize > InSize) + // first, check if in and out types are the same -> nothing to be done + // with reasonable optimization, it should be treated like no-op + // it is possible that the compiler would do it without this test, but this is more explicit + if constexpr (std::is_same_v, sofa::type::Vec>) { - std::fill_n(out.begin() + InSize, OutSize-InSize, filler); + return in; + } + else + { + sofa::type::Vec out(type::NOINIT); + std::copy(in.begin(), in.begin() + std::min(InSize, OutSize), out.begin()); + + if constexpr(OutSize > InSize) + { + std::fill_n(out.begin() + InSize, OutSize-InSize, filler); + } + + return out; } - - return out; } // Convenient function calling previous toVecN with OutVec directly