From 3ab8dbfde61fb2172264845c52f5132b2330fd99 Mon Sep 17 00:00:00 2001 From: Harry Denholm Date: Wed, 22 Oct 2025 19:53:57 +0100 Subject: [PATCH] Move the construction of the OOB violation string to after the OOB check fails so that the code is not allocating, concatenating and freeing strings that are never used in regular operation - this can produce a severe performance penalty (cherry picked from commit 51f58714eb8fb72252c7622c7069009e4648d213) --- include/cpp2util.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/include/cpp2util.h b/include/cpp2util.h index 57ed3c651..06206def2 100644 --- a/include/cpp2util.h +++ b/include/cpp2util.h @@ -1224,14 +1224,14 @@ constexpr auto assert_not_zero(auto&& arg CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAUL if constexpr (std::is_signed_v) { return std::ssize(x); } \ else { return std::size(x); } \ }; \ - auto msg = "out of bounds access attempt detected - attempted access at index " + std::to_string(arg) + ", "; \ - if (max() > 0 ) { \ - msg += "[min,max] range is [0," + std::to_string(max()-1) + "]"; \ - } \ - else { \ - msg += "but container is empty"; \ - } \ if (!(0 <= arg && arg < max())) { \ + auto msg = "out of bounds access attempt detected - attempted access at index " + std::to_string(arg) + ", "; \ + if (max() > 0 ) { \ + msg += "[min,max] range is [0," + std::to_string(max()-1) + "]"; \ + } \ + else { \ + msg += "but container is empty"; \ + } \ bounds_safety.report_violation(msg.c_str() CPP2_SOURCE_LOCATION_ARG); \ } \ return CPP2_FORWARD(x) [ arg ]; \