Semantics of toString

dsimcha dsimcha at yahoo.com
Thu Nov 12 10:51:01 PST 2009


== Quote from Steven Schveighoffer (schveiguy at yahoo.com)'s article
> On Thu, 12 Nov 2009 12:38:00 -0500, dsimcha <dsimcha at yahoo.com> wrote:
> > == Quote from Steven Schveighoffer (schveiguy at yahoo.com)'s article
> >  By far a direct call is faster, but I was surprised at how
> >> little overhead virtual calls add in relation to the loop counter.  I
> >> had
> >> to use 10 billion loops or else the difference was undetectable.
> >> I used dmd 1.046 -release -O (the -release is needed to get rid of the
> >> class method checking the invariant every call).
> >> The relative assembly for calling a virtual method is:
> >> mov	ECX,[EBX]
> >> mov	EAX,EBX
> >> push	dword ptr -8[EBP]
> >> call	dword ptr 014h[ECX]
> >> and the assembly for calling a delegate is:
> >> push	dword ptr -8[EBP]
> >> mov	EAX,-010h[EBP]
> >> call	EBX
> >> -Steve
> >
> > Your benchmarks don't show that the direct call is much faster.  You had
> > inlining
> > disabled.  Was this intentional?  If so, it proves my point that most of
> > the
> > overhead from virtual calls comes from the fact that they can't usually
> > be
> > inlined, not because they're virtual.
> The direct call was 5 seconds faster.  Divide by 10 billion and you get a
> small but present amount.

Yes, about 0.5 nanoseconds.  In other words, if your CPU is roughly 2 GHz, about
one **clock cycle**.  This is definitely negligible IMHO.

> Inlining makes the struct member function call disappear (b/c foo does
> nothing!), so it's not really a relevant benchmark.

Right, my point is that the overhead of indirect function calls compared to direct
function calls is too small to ever be worth considering assuming the direct
function call is not inlined.  However, when the direct function call may be
inlined, this is where indirect calls really hurt because they usually can't be
inlined.

> I did the "struct" version as a baseline.  Consider that the struct
> version is the cost of doing the loop increments, pushing the 'this'
> pointer and argument, and calling the function.  Any difference from that
> is the overhead of virtual/delegate/interface calls.
> Inlining is not possible with delegates (yet), so it's not really
> important for this argument.
> -Steve




More information about the Digitalmars-d mailing list