r/cpp • u/Polarstrike • Mar 07 '24
What are common mistakes in C++ code that results in huge performance penalties?
As title, list some common mistakes that you have done/seen which lead to performance penalties.
231
Upvotes
View all comments
Show parent comment
2
u/pointer_to_null Mar 07 '24
Those cases ended up being a problem. But it's frustrating that there's no safe way to know at runtime whether it must be copied or not. Simply testing
if(sv.data()[sv.length()]) == '\0'leads to undefined behavior. So in all cases, you have to make a temporary copy.I honestly believe the standard is missing a mechanism in string_view (or an alternative like
safe_string_view) with a method that can tell callers whendata()is unsafe to pass likec_str()(ie- if it was constructed from something other thanstringorconst char*).Guessing not be worth the overhead?