user7637348
user7637348

Reputation:

C++ string class efficiency

I have an old string class I wrote years ago (before the standard library was so standard). It works OK but I suppose I should move into the 21th century on my latest project. What I was wondering was how efficient the standard library string class is. For instance if I pass a string to a function or simply assign it to another sting does it copy the data, or simply copy a pointer to the data.

In my old class I kept a reference count with the data and just copied the pointer. If I did something that modified the string in place, I would check to see if its reference was 1. If it was 1 I could use the same string data area assuming the string didn't grow past it's original size. If it was greater than 1 I would copy the string data it before modifying it.

Does the standard library class do something similar (or hopefully better), or does it copy the data every time you pass strings around. I guess this could be implementation dependent. I'm currently developing for windows, although I will probably port it later. Thanks

Upvotes: 0

Views: 153

Answers (2)

Bo Persson
Bo Persson

Reputation: 92211

With the bloated interface of std::string you get copy-on-potential-write, which is a lot less efficient.

For example in char& c = s[i]; c = 'a'; the assignment to c is not allowed to throw bad_alloc if needing to creating a separate COW item when s happens to be shared. So we must un-share the string in every non-const call to s[i], just-in-case.

In practice, this limits the sharing potential a lot, while also adding lots of if (shared) unshare(); code to many string member functions. In multi-threaded code, this is a real performance killer as the unshare() will have to lock the object while un-sharing.

Upvotes: 0

Puppy
Puppy

Reputation: 146910

Small string optimisation has proven to be more effective in most contexts than COW. It's not a guaranteed win though; it can depend a lot on exactly how it's being used. However I would argue that you can always use std::shared_ptr<std::string> to implement your COW class and save yourself a lot of manual memory management crap.

Bottom line is, std::string is probably better but the details here can swing it. Try it out but be careful.

Upvotes: 2

Related Questions