Templates are slow.

2016-09-07 Thread Stefan Koch via Digitalmars-d
Hi Guys, I have just hit a barrier trying to optimize the compile-time in binderoo. Roughly 90% of the compile-time is spent instantiating templates. The 10% for CTFE are small in comparison. I will write an article about why templates are slow. The gist will be however : "Templates

Re: Templates are slow.

2016-09-07 Thread Sebastien Alaiwan via Digitalmars-d
On Thursday, 8 September 2016 at 05:02:38 UTC, Stefan Koch wrote: (Don't do this preemptively, ONLY when you know that this template is a problem!) How would you measure such things? Is there such a thing like a "compilation time profiler" ? (Running oprofile on a dmd with debug info comes fir

Re: Templates are slow.

2016-09-07 Thread Stefan Koch via Digitalmars-d
On Thursday, 8 September 2016 at 06:34:58 UTC, Sebastien Alaiwan wrote: On Thursday, 8 September 2016 at 05:02:38 UTC, Stefan Koch wrote: (Don't do this preemptively, ONLY when you know that this template is a problem!) How would you measure such things? Is there such a thing like a "compilati

Re: Templates are slow.

2016-09-08 Thread Ethan Watson via Digitalmars-d
On Thursday, 8 September 2016 at 05:02:38 UTC, Stefan Koch wrote: I have just hit a barrier trying to optimize the compile-time in binderoo. I did a double take when Stefan told me the representative sample code I gave him to run with Binderoo instantiated ~20,000 templates and resulted in ~1

Re: Templates are slow.

2016-09-08 Thread Andrei Alexandrescu via Digitalmars-d
On 9/8/16 7:02 AM, Stefan Koch wrote: I will write an article about why templates are slow. The gist will be however : "Templates being slow is an inherent property of templates." (We are only talking about templates as defined by (C++ and D)). That would be a great article. Are

Re: Templates are slow.

2016-09-08 Thread Stefan Koch via Digitalmars-d
On Thursday, 8 September 2016 at 12:23:35 UTC, Andrei Alexandrescu wrote: Are there any situations that we can special-case away? Raising the roof. -- Andrei The rangefying functions in std.array come to mind. That will give a huge boost to everyone. (everyone who uses arrays anyway :))

Re: Templates are slow.

2016-09-08 Thread jmh530 via Digitalmars-d
On Thursday, 8 September 2016 at 05:02:38 UTC, Stefan Koch wrote: If you are instantiating a template inside another template think very hard about the reason, often you can "inline" the template body of the inner template and get an instant speed win right there. (Don't do this preemptively,

Re: Templates are slow.

2016-09-08 Thread Jonathan M Davis via Digitalmars-d
On Thursday, September 08, 2016 07:43:10 Ethan Watson via Digitalmars-d wrote: > On Thursday, 8 September 2016 at 05:02:38 UTC, Stefan Koch wrote: > > I have just hit a barrier trying to optimize the compile-time > > in binderoo. > > I did a double take when Stefan told me the representative sample

Re: Templates are slow.

2016-09-08 Thread Stefan Koch via Digitalmars-d
On Thursday, 8 September 2016 at 15:45:53 UTC, Jonathan M Davis wrote: It's critical that we do what we can to make templates fast. And if we can't make them fast enough, we'll definitely have to come up with techniques/guidelines for reducing their usage when they're not really needed. - J

Re: Templates are slow.

2016-09-08 Thread H. S. Teoh via Digitalmars-d
On Thu, Sep 08, 2016 at 04:37:36PM +, Stefan Koch via Digitalmars-d wrote: [...] > Also we need to special case ranges in general. > And try harder to inline calls to range functions. > Maybe even early in the frontend. [...] Yeah, dmd's inliner is really pessimistic. It gives up too easily, a

Re: Templates are slow.

2016-09-08 Thread Lewis via Digitalmars-d
I recently went through the process of optimizing the build time on one of my projects. I started at ~3.08s, and got it down to ~1.6s. The project is around 7000 non-comment-non-whitespace LOC. I timed the build in a pretty non-rigourous fashion (I just timed the python script that kicks off a

Re: Templates are slow.

2016-09-08 Thread Lewis via Digitalmars-d
It's true that templates are inherently slow, and there isn't a ton we can do about that. However, almost every time I compile the project (hundreds of times per day), the overwhelming majority of the time, the same templates are being re-instantiated in exactly the same way. I can't help but w

Re: Templates are slow.

2016-09-08 Thread Ethan Watson via Digitalmars-d
On Thursday, 8 September 2016 at 19:17:42 UTC, Lewis wrote: I can't help but wonder if there were some way to automatically cache templates instantiations between runs of dmd? I'm running with Visual D, which has a "COMPILE ALL THE THINGS" mentality as the default. As part of the rapid iterati

Re: Templates are slow.

2016-09-08 Thread Stefan Koch via Digitalmars-d
On Thursday, 8 September 2016 at 19:49:38 UTC, Ethan Watson wrote: On Thursday, 8 September 2016 at 19:17:42 UTC, Lewis wrote: I can't help but wonder if there were some way to automatically cache templates instantiations between runs of dmd? I'm running with Visual D, which has a "COMPILE AL

Re: Templates are slow.

2016-09-08 Thread Johan Engelen via Digitalmars-d
On Thursday, 8 September 2016 at 19:17:42 UTC, Lewis wrote: Am I crazy in wondering about caching template instantiations? I understand that an incremental build would kind of accomplish this goal, but that comes with its own set of problems. Not as good as what you propose, but: LDC 1.1.0 c

Re: Templates are slow.

2016-09-08 Thread Stefan Koch via Digitalmars-d
Hi Guys, I have some more data. In the binderoo example the main time is spent in the backend. generating code and writing objects files. The front-end spends most of it's time comparing strings of unique type-names :) One particular outlier in the backend code is the function ecom which elimi

Re: Templates are slow.

2016-09-08 Thread Nicholas Wilson via Digitalmars-d
On Thursday, 8 September 2016 at 22:57:07 UTC, Stefan Koch wrote: The front-end spends most of it's time comparing strings of unique type-names :) (Waits for Walter to say, "Use a pool Luke!")

Re: Templates are slow.

2016-09-08 Thread deadalnix via Digitalmars-d
On Thursday, 8 September 2016 at 20:10:01 UTC, Stefan Koch wrote: generating separate object files for each template instanciation is and then only re-generating on change will only be effective if they do not change much. From one build to the next. You'd have tens of thousands of file and

Re: Templates are slow.

2016-09-08 Thread Stefan Koch via Digitalmars-d
On Friday, 9 September 2016 at 01:38:40 UTC, deadalnix wrote: On Thursday, 8 September 2016 at 20:10:01 UTC, Stefan Koch wrote: generating separate object files for each template instanciation is and then only re-generating on change will only be effective if they do not change much. From one

Re: Templates are slow.

2016-09-09 Thread Steven Schveighoffer via Digitalmars-d
On 9/8/16 6:57 PM, Stefan Koch wrote: Hi Guys, I have some more data. In the binderoo example the main time is spent in the backend. generating code and writing objects files. If we ever get Rainer's patch to collapse repetitive templates, we may help this problem. https://github.com/dlang/dm

Re: Templates are slow.

2016-09-09 Thread pineapple via Digitalmars-d
On Friday, 9 September 2016 at 12:09:32 UTC, Steven Schveighoffer wrote: I just had a thought. If you hash the string, and then compare the length of the string and first and last character along with the hash, what are the chances of it being a false positive? Any chance of a false positive

Re: Templates are slow.

2016-09-09 Thread Stefan Koch via Digitalmars-d
On Friday, 9 September 2016 at 12:09:32 UTC, Steven Schveighoffer wrote: On 9/8/16 6:57 PM, Stefan Koch wrote: Hi Guys, I have some more data. In the binderoo example the main time is spent in the backend. generating code and writing objects files. If we ever get Rainer's patch to collapse re

Re: Templates are slow.

2016-09-09 Thread Russel Winder via Digitalmars-d
On Thu, 2016-09-08 at 14:23 +0200, Andrei Alexandrescu via Digitalmars- d wrote: > On 9/8/16 7:02 AM, Stefan Koch wrote: > > > > > > I will write an article about why templates are slow. > > > > The gist will be however : "Templates being slow is an inhere

Re: Templates are slow.

2016-09-09 Thread deadalnix via Digitalmars-d
On Friday, 9 September 2016 at 12:09:32 UTC, Steven Schveighoffer wrote: On 9/8/16 6:57 PM, Stefan Koch wrote: Hi Guys, I have some more data. In the binderoo example the main time is spent in the backend. generating code and writing objects files. If we ever get Rainer's patch to collapse re

Re: Templates are slow.

2016-09-09 Thread Stefan Koch via Digitalmars-d
On Friday, 9 September 2016 at 18:17:02 UTC, deadalnix wrote: You need to compare the string to unique them, so it doesn't change anything. It changes the frequency of comparisons.