Originally posted by mmstick
View Post
Another point is how template specialization enables special-case optimizations that would be impractical to do by hand. I've certainly taken advantage of this capability, in vectorized signal processing code.
On the flip side, C++ makes it very easy to write slow code. Heap allocation is often hidden, and copies are so effortless that programmers often do them as a matter of course. A few years ago, I became keenly aware of this, while hacking on x264 (a popular video codec written in C and assembly). They never copied anything, instead preferring to pass everything through a small number of context structures. This was practically as bad as using global variables, as it often made data flow very hard to follow, further adding to the code's impenetrability. The one benefit was performance, although the size of these structures eventually resulted in some fairly hopeless issues with L1 (and I think even L2) cache thrashing. Of course, if you're careful, you can avoid copies in your C++ code. It's just a matter of knowing where copies are likely to hurt, and discipline. And C++11 did give us important new tools to help with this.
Switching sides, yet again, C programmers still use too many fixed-sized arrays. C++'s heap-allocated std::string and std::vector might be slower, but they play an important part in helping to avoid many buffer overrun bugs and exploits. And if you wanted the performance of stack-allocated buffers, but the safety of heap-allocated structures, you could write a custom string template to let you specify the expected size and have it automatically overflow to the heap (or fail in some other robust way). Last time I checked, GCC's std::string had a built-in buffer that was hard-coded to something like 6 bytes. Also, CoW optimizations.
Leave a comment: