Templates are slow.

Stefan Koch via Digitalmars-d digitalmars-d at puremagic.com
Wed Sep 7 22:02:38 PDT 2016


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 being slow is an inherent 
property of templates." (We are only talking about templates as 
defined by (C++ and D)).

That said: Templates are great!
But you have to use them sanely.

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, ONLY when you know that this 
template is a problem!)

Phobos is has many templates inside templates.
In constraints for example.

I have no idea how to cut down on template-instanciations in 
phobos while still maintaining the same user-friendliness.

Of course myself and other will continue fighting on the 
compiler-front.
To give you that fastest implementation possible!

Cheers,
Stefan


More information about the Digitalmars-d mailing list