4 ms·
I have definitely observed penalties much greater than 1% (closer to 15%). This paper is old, but measures the direct cost of virtual table lookups (not taking
by haberman 12y ago
I have definitely observed penalties much greater than 1% (closer to 15%).
This paper is old, but measures the direct cost of virtual table lookups (not taking into account indirect costs arising from the inability to inline) as 5% in real C++ programs. When they converted the C++ programs to use all virtual functions, the overhead rose to 13.7%: http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf http://www.cs.ucsb.edu/~urs/oocsb/papers/oopsla96.pdf
- jasode 12y agoI didn't make it clear but I wasn't comparing inline to virtual.[1] That type of drastic difference can be greater than 10% which is understandable. I was comparing non-inline function to virtual function and I haven't been able to recreate a 15% slowdown even with zero compiler optimization switches. The stackoverflow observation and mine were on more modern cpus. Maybe that has something to do with the conflicting anecdotes. If someone has a small snippet of code that shows non-inlined function calls running 15% faster than vtable calls, I'd like to study it. [1]the context in grandparent post was already constrained to (non-inlined) normal function calls: ", but nowadays their cost over a normal function call is basically negligible." https://news.ycombinator.com/item?id=8476208 https://news.ycombinator.com/item?id=8476208
- haberman 12y agoI had this benchmark that used to show a 10% difference between non-inlined and virtual: https://gist.github.com/haberman/5621682 https://gist.github.com/haberman/5621682 But since I wrote that, compilers have gotten too smart. When I tried it just now, the compiler devirtualized the function call (which I verified by reading the assembly language output). I should mention though, the the inability to inline virtual functions is part of what makes them less efficient in some cases. Isolating the inlining factor removes one of the benefits that makes non-virtual functions perform better.
- srean 12y ago> I was comparing non-inline function to virtual function Then you are fighting a strawman. The decision to use virtuals or not has to be taken in the context of all the benfits, and inlining is a prominent one among them. Another is vectorization. Inlining a single function sometimes trigger an avalanche of other optimizations, so the cost of using virtuals can be quite high in such scenarios. It is a n unrepresentative to rule out some of the main motivators for choosing non virtual over virtual. In my experience people who harp on the line that virtual functions are free, are those who do not write number crunching code, where the benefits are most apparent. That said, vtable is really a neat performance optimization that targets runtime polymorphism. The problem lies in the fact that many people believe that runtime polymorphism is the only path to polymorphism. Java programmers certainly believe so, for a reason of course. Many uses of runtime polymorphism can be replaced by compile time polymorphism without any loss in flexibility, and frequent gains in performance. In many parts of code I know for certain that the types wont change, and in such cases runtime polymorphism is un-necessary. Many a game engine, array processing code has been written with zero or very sparse use of that feature. There is this raging debate about whether C++ / D functions should default to virtuals, just like in Java. I certainly am in the camp that believes that they should not because runtime polymorphism is not as uniform a necessity as it is made out to be, as long as the language offers compile time polymorphism. Java is out of luck here, its designers did not include compile time polymorphism features or syntactic sugars (if I am not mistaken) but for C++ and D their defaults make sense. @Jasode Indeed and in fact I had upvoted your comment even before writing my comment above.
- jasode 12y ago>you are fighting a strawman. The decision to use virtuals or not has to be taken in the context of all the benfits, and inlining is a prominent one among them. Agree that the decision to use vtable must consider all the disadvantages including loss of inlining. The previous poster also already mentioned that as well. My question about comparison was not about the decision of yes-or-no to virtual calls. It was about understanding the 15% penalty of vtable calls compared to normal non-inlined calls. If someone had a snippet of code showing that large of a penalty on a modern cpu, I thought it would be interesting to disassemble the compiler's output and study it. When the previous poster (coherentpony) was complaining about 15%, I thought he was specifically talking about normal function calls because the poster he was responding to was restricting the word "negligible" to normal function calls. My questions were a continuation of that narrow and focused benchmark. Yes, I think most people understand vtables are not "free". They have a cost. When Alexander Stepanov introduced the STL in the 1990s, one of the factors leading to fast adoption was that it used templates with extensive inlining and the performance blew away the previous attempts of algorithms+containers designed with inheritance & vtables. Heck, a C++ std::sort() could be even faster than C qsort(). Hope that clears up what my curiosity was about.