www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - dmd: enum to!string slows down compilation

reply kdevel <kdevel vogtner.de> writes:
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
Dec 29 2020
parent Basile B. <b2.temp gmx.com> writes:
On Tuesday, 29 December 2020 at 22:42:16 UTC, kdevel wrote:
 Why is the enum to!string conversion so slow?

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

 [...]
one factor is all the template constraints that are evaluated until the right std.conv.to overload gets selected.
Dec 29 2020