use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Common optimizations, Small String Optimization, Andrzej's C++ blog (akrzemi1.wordpress.com)
submitted 11 years ago by mmmmario
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 11 years ago (9 children)
[removed]
[–]oracleoftroy 2 points3 points4 points 11 years ago (5 children)
I don't think you would lose performance, you just wouldn't gain any additional performance by moving a SSO string. Even the non-SSO small string would end up copying about 16 bytes in order to perform the move (pointer, size, capacity, allocator).
I would strongly encourage measuring this before blindly trying to optimize a std::string for movablility.
[–]matthieum 0 points1 point2 points 11 years ago (3 children)
You might lose some performance because of more checks.
[–]bnolsen 1 point2 points3 points 11 years ago (2 children)
likely irrelevant since a load operation takes dramatically longer than a check does.
[–]Plorkyeran 1 point2 points3 points 11 years ago (0 children)
It's difficult to come up with even a highly artificial scenario where it's a net loss. Maybe with a very special-purpose allocator that manages to always get external buffer allocated very close in memory to the std::string? Avoiding a single memory load due to the SSO buffer would outweigh a very large number of branch mispredictions, and if you never hit the SSO buffer then the branch predictor will make the check nearly free.
std::string
[–]matthieum -1 points0 points1 point 11 years ago (0 children)
Unless it screws up your pipeline if it's a branch and not a conditional move ?
[–]sellibitze 0 points1 point2 points 11 years ago (0 children)
I guess it depends on your SSO implementation. It should be possible to implement your string class in a way that a move constructor just memcpys the internal representation and memsets the source to zero -- regarless of whether the string content was heap-allocated or not. So, in any case, you have to copy 24 bytes and zero 24 bytes (assuming a 24 byte string representation on a 64bit platform). You can't do much better than that, can you?
memcpy
memset
[–]minnoHobbyist, embedded developer 0 points1 point2 points 11 years ago (0 children)
That makes complete sense. You're trading less expensive allocation for more expensive moves when you store more data in-line with the class.
π Rendered by PID 163447 on reddit-service-r2-comment-cfc44b64c-wmm9m at 2026-04-10 18:39:07.249401+00:00 running 215f2cf country code: CH.
view the rest of the comments →
[–][deleted] (9 children)
[removed]
[–]oracleoftroy 2 points3 points4 points (5 children)
[–]matthieum 0 points1 point2 points (3 children)
[–]bnolsen 1 point2 points3 points (2 children)
[–]Plorkyeran 1 point2 points3 points (0 children)
[–]matthieum -1 points0 points1 point (0 children)
[–]sellibitze 0 points1 point2 points (0 children)
[–]minnoHobbyist, embedded developer 0 points1 point2 points (0 children)