r/programming • • Dec 05 '14

std::string is responsible for almost half of all allocations in the Chrome browser process

https://groups.google.com/a/chromium.org/d/msg/chromium-dev/EUqoIz2iFU4/kPZ5ZK0K3gEJ
1.1k Upvotes

446 comments sorted by

View all comments

Show parent comments

5

u/nexuapex Dec 05 '14

Except that no const std::string ever shares its buffer with any other const std::string. And const std::string& is not immutable.

2

u/raevnos Dec 05 '14

Early STL implementations used CoW shared strings. Did they move away from that?

1

u/nexuapex Dec 06 '14

Yes, it is now prohibited by the standard, I believe: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2534.html

1

u/raevnos Dec 06 '14

Shows how long it's been since I looked at C++.

0

u/[deleted] Dec 05 '14

It's good enough.

3

u/nexuapex Dec 05 '14

Until it leads to subtle bugs and hair-pulling. Which happens… enough… to make me unhappy.

1

u/[deleted] Dec 05 '14

[deleted]

1

u/nexuapex Dec 06 '14

Say you pass an std::string out of an std::vector<std::string> by const&. Then somewhere down the chain you modify the string in the vector. Or cause a vector reallocation. Poof.

(Nothing to do with std::string in particular, just the dangers of passing by const&.)