jai-like CTFE string formating

Sönke Ludwig via Digitalmars-d digitalmars-d at puremagic.com
Sun Aug 13 11:20:15 PDT 2017


Am 12.08.2017 um 13:47 schrieb Stefan Koch:
> Hi Guys,
> 
> I've just implemented a subset of the std.format functionality.
> In the same style that Johnathan Blow uses for JAI.
> 
> It's about 10x faster then using std.format and uses much less memory :)
> 
> the follwing code takes over 250 ms to compile :
> {
>      import std.format;
>      pragma(msg, format("Hello %s %s %s %s %s %s %s %s %s", " I ", 
>   "just", " have", " to", " concat",  " a lot", " of", " strings ...", 9));
> }
> Whereas the following alternative takes 20 ms :
> {
>      import ctfe_utils;
>      pragma(msg, format_jai("Hello % % % % % % % % %", " I ", " just", " 
> have" , " to", " concat", " a lot", " of", " strings ...", 9));
> }
> 
> see for yourself:
> https://www.youtube.com/watch?v=T0BJxdt61RY

I was a bit shocked by this number and performed a little test with 
multiple calls to format. Fortunately only the first one takes that long 
to compile. For 500 different calls I got about 2ms per call on Windows 
(which might get slowed down somewhat by the slow terminal output):

     import std.format;
     template T(size_t i) {
       static if (i > 0) {
         pragma(msg, format("Bye %s %s %s %s %s %s %s %s %s %s",
           i, " I ",  "just", " have", " to", " concat",  " a lot",
           " of", " strings ...", 9));
         enum T = T!(i-1);
       } else enum T = 0;
     }
     enum test = T!500;


More information about the Digitalmars-d mailing list