dmd: enum to!string slows down compilation

kdevel kdevel at vogtner.de
Tue Dec 29 22:42:16 UTC 2020


Why is the enum to!string conversion so slow?

~~~slowenumtostringconversion.d
private enum S { A, B, C, D, };

version (fast) {
    string resolve (E) (E e)
    {
       static foreach (m; __traits (allMembers, E))
          if (e == __traits (getMember, E, m))
             return m;
       assert (false);
    }
}

class Expected : Exception {
    this (S s)
    {
       version (slow) {
          import std.conv : to;
          super ("Expected " ~ s.to!string); // slows down 
compilation!
       }
       else version (fast) {
          super ("Expected " ~ resolve (s));
       }
    }
}

void main ()
{
    throw new Expected (S.C);
}
~~~

$ time dmd -version=slow slowenumtostringconversion.d

real	0m1.144s
user	0m0.820s
sys	0m0.140s

$ time dmd -version=fast slowenumtostringconversion.d

real	0m0.466s
user	0m0.290s
sys	0m0.067s




More information about the Digitalmars-d-learn mailing list