Skip to content

Commit 51f5871

Browse files
committed
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
1 parent 1a6062a commit 51f5871

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

include/cpp2util.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,14 +1224,14 @@ constexpr auto assert_not_zero(auto&& arg CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAUL
12241224
if constexpr (std::is_signed_v<CPP2_TYPEOF(arg)>) { return std::ssize(x); } \
12251225
else { return std::size(x); } \
12261226
}; \
1227-
auto msg = "out of bounds access attempt detected - attempted access at index " + std::to_string(arg) + ", "; \
1228-
if (max() > 0 ) { \
1229-
msg += "[min,max] range is [0," + std::to_string(max()-1) + "]"; \
1230-
} \
1231-
else { \
1232-
msg += "but container is empty"; \
1233-
} \
12341227
if (!(0 <= arg && arg < max())) { \
1228+
auto msg = "out of bounds access attempt detected - attempted access at index " + std::to_string(arg) + ", "; \
1229+
if (max() > 0 ) { \
1230+
msg += "[min,max] range is [0," + std::to_string(max()-1) + "]"; \
1231+
} \
1232+
else { \
1233+
msg += "but container is empty"; \
1234+
} \
12351235
bounds_safety.report_violation(msg.c_str() CPP2_SOURCE_LOCATION_ARG); \
12361236
} \
12371237
return CPP2_FORWARD(x) [ arg ]; \

0 commit comments

Comments
 (0)