Templates are slow.

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Thu Sep 8 08:45:53 PDT 2016


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
> code I gave him to run with Binderoo instantiated ~20,000
> templates and resulted in ~10,000,000 hash map look ups inside
> the compiler.
>
> I can certainly write it to be more optimised, but one of the
> goals I have is to make the codebase human readable so that it's
> not just Manu and myself that can understand the code. As a
> result, I figure this could be representative of how an ordinary
> user would write templated code.

IIRC, Don posted at one point about how the std.algorithm unit tests ended
up with over a million template instantiations. All of the eponymous
templates that we use for template constraints really add up, and having
heavily range-based code is going to rack up the number of template
instantiations as well. 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.

Improvements to CTFE have really helped though. std.metastrings was dog slow
in comparison to using CTFE, and at this point, we don't need
std.metastrings anymore, and so it's gone. That's definitely not going to
work in all cases though - just in cases where something is being done with
templates that could be done with a function that could run at runtime now
that CTFE can do most things that can be done at runtime. And we've probably
gotten most everything we can out of that transition already - at least in
Phobos.

- Jonathan M Davis



More information about the Digitalmars-d mailing list