Preston L. Bannister tries to build a better C++ string class than provided by the C++ standard libraries (found through Christopher Baus). However, I think he will fail, since he’s too much involved in low level things, rather then design.
Preston states that
I seldom use the C++ string classes supplied with the standard libraries, out of a belief that (for my purposes) that I can easily do better, building on bits of history and experience. Considerations that go into building a simple and highly efficient C++ string class include the following.
- The standard <string .h> supplied C string functions are highly optimized (as described in a previous post).
- Heap operations are best avoided (where practical) in long running applications. Use of a free list is very effective when the application has a cyclic workload (as most do).
- Use of C++ inline class methods allows both readable code and most effective use of the optimizer.
I actually wonder why he’s so negative about the standard C++ string class, and what makes him belief the standard implementation isn’t highly efficient. Let’s take a look at the Visual C++ implementation, for example. Doesn’t it fullfill, what Preston considers?
- It uses calls to the supplied C string functions, which turn out to perform very well on Windows machines, as he himself figured out.
- It avoids unnecessary heap allocation by implementing copy-on-write, allocating in blocks and similiar tricks.
- Since it is a template class, all functions are inlined.
Remarkably, the string class gains perfomance by changing the point of view from low level operations like memory copying to the actual usage. Take for example the copy-on-write strategy (Scott Meyers wrote about it in “More Effective C++”, Rule 29). This is a true performance boost, since you avoid memory beeing copied on each assignment or copy construction operation. And have in mind that each function returning a string class implicitly invokes the copy constructor. In other words: This is a very common use case for a string class.
Figuring out the best way to copy an array of chars is simple. But designing a string class is much more than this: It is about avoiding copying whereever possible, too. And I doubt if the string class Preston is to come up with, will be look too much different from the current standard implementation in the end.