Semantics of toString

Steven Schveighoffer schveiguy at yahoo.com
Thu Nov 12 10:14:51 PST 2009


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.

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

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