Better way to append to array than ~= ?

Vladimirs Nordholm v at vladde.net
Tue Apr 3 19:02:25 UTC 2018


Hello people.

I currently have a function which multiple times per second takes 
in arguments, and appends the argument as my special type. The 
following code should explain what I do more properly:

     struct MySpecialType { char c; }

     auto foo(Args...)(Args args)
     {
         MySpecialType[] bar;

         foreach(ref arg; args)
         {
             static if(is(typeof(arg) == MySpecialType))
             {
                 bar ~= arg;
             }
             else
             {
                 foreach(c; to!string(arg))
                 {
                     bar ~= MySpecialType(c);
                 }
             }
         }

         // do more stuff
     }

Now, from my trace.log, some of the topmost things on the timing 
list are `std.array.Appender!(immutable(char).<more stuff>`. I 
also remember reading some years ago that ~= isn't optimal for 
speed.

So my question is: Is there a better and/or faster way of doing 
this, or is this the best approach?



More information about the Digitalmars-d-learn mailing list