www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Virtual Methods hurting performance in the DMD frontend

reply Stefan Koch <uplink.coder googlemail.com> writes:
Hi,

As you may know I am currently optimizing template-related code 
inside of DMD.
Inside DMD code quality is quite high, there is little low 
hanging fruit.

However there is one thing that suspiciously often shows up the 
on profilers display.
Those are indirect class which have a high number of L2(!) 
i-cache misses.

These calls don't do a lot of work. They are needed either for 
downcasts or to verify the dynamic type of an AST-Node.
Even without the i-cache related stalls the call overhead alone 
is something to think about.

For template-heavy code matching template parameters is one of 
the most frequently operations.
Since Template Parameters can be types expressions or symbols the 
dynamic-types are heavily queried.

First experiments suggest that a speedup of around 12% is 
possible if the types where accessible directly.

Since dmd uses visitors for many things now the benefit of 
virtual methods is highly reduced.

Please share your thoughts.

Cheers,
Stefan
Sep 11 2016
next sibling parent Jacob Carlborg <doob me.com> writes:
On 2016-09-11 23:48, Stefan Koch wrote:
 Hi,

 As you may know I am currently optimizing template-related code inside
 of DMD.
 Inside DMD code quality is quite high, there is little low hanging fruit.

 However there is one thing that suspiciously often shows up the on
 profilers display.
 Those are indirect class which have a high number of L2(!) i-cache misses.

 These calls don't do a lot of work. They are needed either for downcasts
 or to verify the dynamic type of an AST-Node.
 Even without the i-cache related stalls the call overhead alone is
 something to think about.

 For template-heavy code matching template parameters is one of the most
 frequently operations.
 Since Template Parameters can be types expressions or symbols the
 dynamic-types are heavily queried.

 First experiments suggest that a speedup of around 12% is possible if
 the types where accessible directly.

 Since dmd uses visitors for many things now the benefit of virtual
 methods is highly reduced.

 Please share your thoughts.
What about using an enum in the base class that indicates the runtime type? The Expression class is already using this technique. Add it to Declaration and Statement as well, or even to RootObject. -- /Jacob Carlborg
Sep 12 2016
prev sibling parent reply Stefan Koch <uplink.coder googlemail.com> writes:
On Sunday, 11 September 2016 at 21:48:56 UTC, Stefan Koch wrote:
 Those are indirect class
I meant indirect calls! Jacob Yes that is my indented solution. Having a type-field in root-object.
Sep 12 2016
parent reply Stefan Koch <uplink.coder googlemail.com> writes:
On Monday, 12 September 2016 at 08:03:45 UTC, Stefan Koch wrote:
 On Sunday, 11 September 2016 at 21:48:56 UTC, Stefan Koch wrote:
 Those are indirect class
I meant indirect calls! Jacob Yes that is my indented solution. Having a type-field in root-object.
A Small update on this. When dmd is complied with ldc this problem seems to lessen. However much of dmd's code especially in dtemplate could be simpified if it could just switch on a value. instead of doing method calls and null checks.
Sep 13 2016
next sibling parent Jacob Carlborg <doob me.com> writes:
On 2016-09-13 23:01, Stefan Koch wrote:

 However much of dmd's code especially in dtemplate could be simpified if
 it could just switch on a value. instead of doing method calls and null
 checks.
At least to me, it would be very useful for debugging purpose as well. -- /Jacob Carlborg
Sep 14 2016
prev sibling parent Johan Engelen <j j.nl> writes:
On Tuesday, 13 September 2016 at 21:01:34 UTC, Stefan Koch wrote:
 On Monday, 12 September 2016 at 08:03:45 UTC, Stefan Koch wrote:
 On Sunday, 11 September 2016 at 21:48:56 UTC, Stefan Koch 
 wrote:
 Those are indirect class
I meant indirect calls! Jacob Yes that is my indented solution. Having a type-field in root-object.
A Small update on this. When dmd is complied with ldc this problem seems to lessen.
Try PGO with LDC 1.1.0-beta. https://johanengelen.github.io/ldc/2016/04/13/PGO-in-LDC-virtual-calls.html
Sep 14 2016