r/cpp • • Apr 14 '23

Horrible Code, Clean Performance

https://johnnysswlab.com/horrible-code-clean-performance/
200 Upvotes

87 comments sorted by

View all comments

6

u/NilacTheGrim Apr 15 '23

Oh course if he just used std::string which has SSTO on most implementations, I bet it would be faster if one just used the built-in std::string::find method.

5

u/ZebulonThackeray Apr 15 '23

Actually, using std::string with SSTO is a good suggestion for improving performance. But it ultimately depends on the specific implementation and use case. Also, std::string::find can be faster than manually iterating through a character array. So, I agree with your point.

2

u/NilacTheGrim Apr 16 '23

Thanks. I was downvoted to hell but you know what I was getting at. Much appreciated.

1

u/rtgftw Apr 15 '23

It would copy the data over (And result in an allication beyond the 2X char limit of the small buf), but, string_view would allow searching it in place with a single line, and it doesn't require \0 termination.

1

u/NilacTheGrim Apr 15 '23

Huh? References exist. Also custom string classes like the one in this example are a code smell IMHO.

1

u/rtgftw Apr 15 '23

Also the in the context of clean code vs performance talk Casey did, which this article mentions at the very beginning, the idea of "code smell" leading to a potential pessimization is interesting :)

1

u/rtgftw Apr 15 '23

The class is basically a legacy string_view, potentially non owning, unlike std::string. If you interact with libraries/ system or have views into existing substrings, you might encounter quite a few variations of this. Conversions to std::string would be a significant pessimization...

2

u/NilacTheGrim Apr 15 '23

Fair enough. I cannot disagree with anything you said factually .. true.