On 05/12/2017 12:29 PM, Tom Ritter wrote:
On Fri, May 12, 2017 at 1:27 AM, Ehsan Akhgari <ehsan.akhg...@gmail.com> wrote:
I realized we haven't had a performance mini-story for a while -- I sort of
dropped the ball on that.  Running over this bug made me want to talk about
a pretty well known sort of slowness in C++ code, virtual functions.  The
cost of virtual functions comes from several different aspects, firstly they
effectively prevent the compiler from doing inlining the function which
enables a host of compiler optimizations, essentially by enabling the
compiler to see more of the code and optimize more effectively based on
that.  But then there is the runtime cost of the function, which mostly
comes from the indirect call.  The majority of the performance penalty here
on modern hardware is due to branch midpredictions when different
implementations of a virtual function get called at a call site.  You should
remember that on modern desktop processors, the cost of a branch
misprediction can be around 15-20 cycles (depending on the processor) so if
what your function does is very trivial and it has many overrides that can
be called in hot code chances are that you are spending a considerable
amount of time waiting for the instruction cache misses on the calls to the
virtual function in question.  Of course, finding which virtual functions in
your program are these expensive ones requires profiling the workloads you
care about improving, but always keep an eye for this problem as
unfortunately the object-oriented programming model in C++ really encourages
writing code like this.  This is the kind of issue that a native profiler is
probably more suitable for discovering, for example if you are using a
simple native sampling profiler these issues typically show up as a long
amount of time being spent on the first instruction of the virtual function
being called (which is typically an inexpensive instruction otherwise.)
This reminded me of
https://bugzilla.mozilla.org/show_bug.cgi?id=1332680 (and
https://bugzilla.mozilla.org/show_bug.cgi?id=1332682 )

Adding -Wsuggest-final-types and -Wsuggest-final-methods and looking
at the output seems pretty low-effort to find a lot of more
opportunities for this. (Unless I'm misunderstanding things).

Plus, it benefits security!
Yes, this is indeed a good point. Even though this will really only have a measurable impact on performance if the functions are called in hot code, it seems like a shame to not listen to the compiler when it tells you I could make your code faster if you added this keyword in a bunch of places. :-)
_______________________________________________
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform

Reply via email to