4 ms·
I'm disappointed. I expected this to be a post along the lines of Ridiculous Fish's two posts on C++ features that you pay for even if you don't use: 1. http:/
by eridius 14y ago
I'm disappointed. I expected this to be a post along the lines of Ridiculous Fish's two posts on C++ features that you pay for even if you don't use:
1. http://ridiculousfish.com/blog/posts/i-didnt-order-that-so-why-is-it-on-my-bill-episode-1.html http://ridiculousfish.com/blog/posts/i-didnt-order-that-so-w...
2. http://ridiculousfish.com/blog/posts/i-didnt-order-that-so-why-is-it-on-my-bill-episode-2.html http://ridiculousfish.com/blog/posts/i-didnt-order-that-so-w...
Thankfully one of C++11's requirements was to not introduce any new functionality that had penalties even if you aren't using it. Too bad that wasn't a requirement for the original design of the language.
- JoeAltmaier 14y agoWhile that was informative, its not really C++ specific. The runtime example was library routines.
- eridius 14y agoWhat do you mean, it's not C++ specific? The runtime example is very explicitly fallout from a requirement of the C++ specification, which affects C++ extern inline functions.
- JoeAltmaier 14y agoWrong example - read the 2nd link.
- eridius 14y agoThe C++ spec includes the standard library. The COW optimization is a common one (although perhaps not anymore with C++11, see other comments). The C++ spec imposes certain requirements on the std::string interface that are the source of the problem documented in that link. In fact, the exact requirements of the C++ spec that cause the problem are talked about, along with an example of what a simple solution could have been (if it had been considered when C++ was being standardized). So saying it's not C++ because it's standard library is at best a misunderstanding, at worst it's disingenuous. The C++ spec is the cause of this fundamental problem with any attempt at doing COW optimization of std::string.
- JoeAltmaier 14y agoSorry, I guess I'm too used to using C++ without any std library in sight. I write a lot of embedded code, where the library overhead is too great.
- sltkr 14y agoI've read the first article and I'm not convinced the problem this guy describes incurs real costs in reality. After all, there is no reason to include inline function definition in dynamic objects unless they are called without being inlined or their address is taken; both of which are extremely unlikely for class-inline methods. The author fails to convince me that if C++ didn't require taking the address of an inline functions to yield a consistent value, real-world C++ code could/would run significantly faster. The second article is pretty much obsolete with C++11, which makes copy-on-write string implementations impractical.
- eridius 14y agoHow does C++11 make copy-on-write string implementations impractical? I know the short string optimization is used now, but I don't see how that's incompatible with COW (for strings that are too large to use the short string optimization). And I don't see how move constructor/assignment is incompatible either. Is there some other C++11 change that I'm overlooking?
- sltkr 14y agoI can't give you a definite answer, unfortunately, but I believe the more detailed specification of the semantics of string references/iterators in the new standard makes it very difficult to implement std::string with copy-on-write semantics without losing the advantages of such an implementation. For example, consider the following function: char f(string &s) { const string &t = s; const char &ch = t[1]; s[2] = 'x'; return ch; } This should be valid code, but with a copy-on-write implementation, writing to the string on line 3 could invalidate the reference created on line 2. As far as I know there isn't a proper solution to this problem without changing the return type of `std::string::operator[](size_type) const' in some non-standard way.
- eridius 14y agoDid you read the 2nd link? This is a problem even in C++03. The solution is to invoke the copy on the access of `t[1]`, which is what libstdc++ does. That was the whole point of the second link; that using any operator/method that returns a char reference (operator[], at(), etc) ruins COW.
- dkarl 14y agoThe second link is not a good example of C++ imposing a cost for something you don't use. It's just a bad performance trade-off in std::string. The standard library types aspire to be useful in a wide range of performance-sensitive applications, but if they don't work for you, you are free to use alternatives. Programmers at my company use a variety of C++ containers that are faster than the STL containers for particular uses. The first link is a reasonable example of paying a cost for something you might not use. However, I disagree with the article when it says there's no good reason for it. People really do compare function pointers for equality. A common example is when different functions implement different "strategies" (to use the GOF term.) A simulation might customize the behavior of simulation objects using pointers to different strategy functions, and you might compare the pointers to find all the simulation objects that follow a particular strategy, or to check whether two objects use the same strategy. Since implementations are free to inline functions that are not declared "inline," there would be no valid way to compare function pointers if an inlined function could have different addresses in different translation units. Adding performance at the expense of sanity should be voluntary, not the default behavior. (It definitely counts as insanity if comparing &my_handy_function and &my_handy_function for equality returns false in a language where function pointers are the only way to treat a function as data.) Anyone who is picky enough about performance to be concerned about link time is on intimate terms with their implementation(s) anyway, so I think it's perfectly acceptable for such an optimization to be available through non-standard flags. Although, if any language were to standardize insanity, perhaps it would be appropriate for it to be C++.