Jump to page: 1 2
Thread overview
jai-like CTFE string formating
Aug 12, 2017
Stefan Koch
Aug 12, 2017
tetyys
Aug 12, 2017
ixid
Aug 12, 2017
Stefan Koch
Aug 13, 2017
Manu
Aug 13, 2017
Stefan Koch
Aug 13, 2017
Manu
Aug 13, 2017
Sönke Ludwig
Aug 13, 2017
Stefan Koch
Aug 13, 2017
Sönke Ludwig
Aug 13, 2017
Jerry
Aug 13, 2017
Stefan Koch
Aug 20, 2017
Jerry
August 12, 2017
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
August 12, 2017
On Saturday, 12 August 2017 at 11:47:10 UTC, Stefan Koch wrote:
> [...]

very nice

August 12, 2017
On Saturday, 12 August 2017 at 11:47:10 UTC, Stefan Koch wrote:
> 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

Could we not improve the syntax to something like:

     pragma(msg, format_jai("Hello %9", " I ", "
 just", " have" , " to", " concat", " a lot", " of", " strings
 ..."));
 }

Or whatever is closest that doesn't step on the current print syntax.
August 12, 2017
On Saturday, 12 August 2017 at 13:19:12 UTC, ixid wrote:
> On Saturday, 12 August 2017 at 11:47:10 UTC, Stefan Koch wrote:
>> 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
>
> Could we not improve the syntax to something like:
>
>      pragma(msg, format_jai("Hello %9", " I ", "
>  just", " have" , " to", " concat", " a lot", " of", " strings
>  ..."));
>  }
>
> Or whatever is closest that doesn't step on the current print syntax.

The point of format is that you want to replace the % my the argument and then write more static text.

"The Parameter '%' was null and it should not be"
"Rule '%' was not found in catalog '%' is possible the closest match"

August 13, 2017
On 13 August 2017 at 00:15, Stefan Koch via Digitalmars-d < digitalmars-d@puremagic.com> wrote:

> On Saturday, 12 August 2017 at 13:19:12 UTC, ixid wrote:
>
>> On Saturday, 12 August 2017 at 11:47:10 UTC, Stefan Koch wrote:
>>
>>> 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
>>>
>>
>> Could we not improve the syntax to something like:
>>
>>      pragma(msg, format_jai("Hello %9", " I ", "
>>  just", " have" , " to", " concat", " a lot", " of", " strings
>>  ..."));
>>  }
>>
>> Or whatever is closest that doesn't step on the current print syntax.
>>
>
> The point of format is that you want to replace the % my the argument and then write more static text.
>
> "The Parameter '%' was null and it should not be"
> "Rule '%' was not found in catalog '%' is possible the closest match"
>

If you're keen to introduce a new function, I'd strongly suggest changing to {1} {2} {3}, or %1 %2 %3, format/printf functions where you don't supply the place index are useless a lot of the time and need to be rewritten, which is a tedious task.


August 13, 2017
On Sunday, 13 August 2017 at 00:42:08 UTC, Manu wrote:
> On 13 August 2017 at 00:15, Stefan Koch via Digitalmars-d <
>> [ ... ]
>
> If you're keen to introduce a new function, I'd strongly suggest changing to {1} {2} {3}, or %1 %2 %3, format/printf functions where you don't supply the place index are useless a lot of the time and need to be rewritten, which is a tedious task.

I am not too keen introducing a new function, given that it most likely won't be accepted into phobos.

However your suggestion is indeed a good suggestion.
The check would then see if the number of args are equal to the max index.
And it would check that all indices from 0 to maxIdx are used at least once.

Thanks,
Stefan
August 13, 2017
On 13 August 2017 at 12:07, Stefan Koch via Digitalmars-d < digitalmars-d@puremagic.com> wrote:

> On Sunday, 13 August 2017 at 00:42:08 UTC, Manu wrote:
>
>> On 13 August 2017 at 00:15, Stefan Koch via Digitalmars-d <
>>
>>> [ ... ]
>>>
>>
>> If you're keen to introduce a new function, I'd strongly suggest changing to {1} {2} {3}, or %1 %2 %3, format/printf functions where you don't supply the place index are useless a lot of the time and need to be rewritten, which is a tedious task.
>>
>
> I am not too keen introducing a new function, given that it most likely won't be accepted into phobos.
>
> However your suggestion is indeed a good suggestion.
> The check would then see if the number of args are equal to the max index.
> And it would check that all indices from 0 to maxIdx are used at least
> once.
>

No, it's not always the case that a format string uses every argument. Format strings are usually taken from the translation table. Different languages have different word orders (which is why placeholders need to accept an arg index), and occasionally, some translations don't use all the args because the sentence structure doesn't make sense, or excess args are supplied because one language requires a string that others don't use.


August 13, 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;
August 13, 2017
On Sunday, 13 August 2017 at 18:20:15 UTC, Sönke Ludwig wrote:
> 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;

Wouldn't you say that 2ms is still a pretty long time for a function call ?

August 13, 2017
Am 13.08.2017 um 20:25 schrieb Stefan Koch:
> On Sunday, 13 August 2017 at 18:20:15 UTC, Sönke Ludwig wrote:
>> 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;
> 
> Wouldn't you say that 2ms is still a pretty long time for a function call ?
> 

For this particular function call, yes, but still a lot less scary ;) To get some better numbers, can you do a side by side comparison of your two versions both executed a few hundred times? If the simplified version still turns out considerably faster then there may be some low hanging fruits in vibe.d's web/REST modules.
« First   ‹ Prev
1 2