Compile time performance for metaprogramming is somewhat inconsistent
maik klein via Digitalmars-d
digitalmars-d at puremagic.com
Wed Mar 2 18:03:01 PST 2016
Consider the following code
void main()
{
import std.stdio;
import std.range: iota, join;
import std.algorithm.iteration: map;
import std.conv: to;
import std.meta: aliasSeqOf, staticMap, AliasSeq;
enum types = "AliasSeq!(" ~ iota(0,10000).map!(i =>
to!string(i)).join(",") ~ ")";
alias t = AliasSeq! (mixin(types));
//alias t1 = aliasSeqOf!(iota(0, 10000));
}
't' compiles on my machine in ~3.5 seconds while 't1' needs ~1
minute to compile. It seems that mixins are just way more
performant than template instantiations. Any ideas why? What
causes the slowdown and what can I improve?
Also I compared some meta stuff in C++ and D.
For example filtering
enum isEven(alias i) = i % 2 is 0;
void main()
{
import std.stdio;
import std.range: iota, join;
import std.algorithm.iteration: map;
import std.conv: to;
import std.meta: AliasSeq, Filter;
enum types = "AliasSeq!(" ~ iota(0,10000).map!(i =>
to!string(i)).join(",") ~ ")";
alias t = AliasSeq!(mixin(types));
alias evenTypes = Filter!(isEven,t);
}
Someone was also so kind to create this in C++ though it looks a
bit more crazy because he wanted to do roughly the same thing.
https://gist.github.com/ricejasonf/8c2b54c182e6038fd0ce
The D version compiles in ~14.5 seconds while the C++ version
compiles in ~4.2 seconds. This was very surprising to me.
The more Hana like code is here
https://github.com/boostorg/hana/blob/master/benchmark/filter/compile.hana.tuple.erb.cpp
*I wasn't yet able to run the Hana benchmarks yet because the
build script doesn't detect my ruby executable.
More information about the Digitalmars-d
mailing list