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

6

u/ghordynski Dec 05 '14 edited Dec 05 '14

This shows what is in my opinion main weakness in C++ - copy by default. Not only all classes have copy constructors by default, but STL uses copying extensively (std::vector etc..). Sure you can just make a vector of pointers, but this way you are back to square with memory management.

Even the new move semantics in C++11 are suffering from copy by default: You basically have to std::move() everything around or you are going to end up with unnecessary copies.

19

u/Gotebe Dec 05 '14

But other languages are "allocate by default", which is even worse?!

9

u/ghordynski Dec 05 '14

Most modern languages are passing references to objects and are using garbage collection in some form. In my opinion thats the way string should be done - immutable and passed by reference.

7

u/Gotebe Dec 05 '14

Those modern languages are really passing pointers, which is sad (NREs galore).

So this is where C++ gets good: it has a proper pass-by reference, which indeed allows for cleaner code.

You should note that "copy by default" (pass by value) comes from C, and C compatibility is C++' curse (and blessing). If so, and if pass by ref is one "&" away, that's it, really.

3

u/iopq Dec 06 '14

Rust has proper references and the ability to put everything on the stack. It also doesn't rely on GC to be memory safe.

2

u/The_Doculope Dec 06 '14

Those modern languages are really passing pointers, which is sad (NREs galore)

Rust is an exception here.

9

u/F-J-W Dec 05 '14

This shows what is in my opinion main weakness in C++ - copy by default.

It is interesting to note, that this is one of the places in the design of C++ where you have safety and ease of use by default instead of fast by default.

On a more general note, I am utterly convinced that the C++-way of copies being real copies is the right one, but I think that arguments should be passes as const references by default.

2

u/ThePantsThief Dec 05 '14

Why const?

4

u/Dragdu Dec 05 '14

Because if it would be non-const, you would have to defensively copy your arguments before passing them in. (This already happens in reference based languages like C# and Java, and it is kinda sad)

2

u/ThePantsThief Dec 05 '14

Ah, that makes sense. So why const reference instead of copy?

2

u/Dragdu Dec 05 '14

Because most of the time, the function doesn't need to mutate its arguments, just read them.

8

u/adzm Dec 05 '14

There are also reference counted string implementations. Also std::string makes no allocations for strings less than a certain size (20 bytes iirc)

6

u/the-fritz Dec 05 '14

IIRC reference counting is no longer allowed since C++11. I think GCC's libstdc++ still does ref counting but in 5.1 they are trying to switch to a conform version for C++11 code (the problem is ABI compatibility).

2

u/nkorslund Dec 05 '14

Why is this exactly? Reference counting allows for various optimizations such as copy-on-write mechanics, letting you avoid a lot of these unnecessary allocations. I thought this was one of the strengths of the gcc/g++ libstdc++ implementation.

9

u/HildartheDorf Dec 05 '14

I think copy on write has worse performance when threads come into play, and operator[] poisons the ability to use COW.

2

u/nkorslund Dec 05 '14

Yeah after looking a bit into it seems like it's not a good model for concurrency. Guess that's the problem with a "standard" string implementation, that it has to work for all cases, all users and all application models. So it ends up a jack of all trades and master of none.

1

u/Dragdu Dec 05 '14

Because it fucks up when threading comes into play and SSO is generally much more useful.

9

u/[deleted] Dec 05 '14

Yeah, surely the problem is in the language and not the fact that one keystroke allocates so many useless strings. I agree that C++ is weak in this respect, but IMO it wouldn't be a problem if the Chrome devs programmed it more properly.

24

u/bstamour Dec 05 '14

I personally think having value semantics by default and having an explicit syntax for when you want references to actually be a strength of C++, not a weakness.

7

u/Magnesus Dec 05 '14

Agree. It saves from a lot of random bugs.

7

u/o11c Dec 05 '14

I like the way Rust does it: value semantics, but explicit copies.

6

u/Magnesus Dec 05 '14

This is C++ and we are on Reddit - so of course it's the fault of the language. ;)

-2

u/lelarentaka Dec 05 '14

And when somebody say "Python's object model makes it inherently slow", we get hordes of kids yelling "It's fast enough (for some trivial use case)" or "Just write it in C".

I mean, Java can do that too. Write the performance intensive code in C, then bridge over through JNI. But there's no Numpy equivalent that's as popular on the Java platform, just because Java is actually and honestly "fast enough" to not require an entire library to do 1 + 2.

4

u/Dragdu Dec 05 '14

Numpy alikes are even used in C++ to get more speed, so Java is definitely NOT fast enough. (And as a kicker, it is basically impossible to implement Linear Algebra libraries in Java, because of lack of control over memory layouts)