AA code 50x slower

AlphaPurned Alpha at Beta.com
Mon Feb 17 02:18:15 UTC 2020


On Sunday, 16 February 2020 at 13:50:49 UTC, evilrat wrote:
> On Sunday, 16 February 2020 at 12:57:43 UTC, AlphaPurned wrote:
>> template AA(string[] S)
>> {
>> 	auto _do() { int[string] d; foreach(s; S) d[s] = 0; return d; 
>> }
>> 	enum AA = _do;
>> }
>>
>
> My best guess is that enum arrays(except strings) and AA's are 
> instantiated every time you access them.
> This is especially bad with loops.
>
> Probably you want static or module level variable instead.
> Module level AA's can't have compile time initializer, however 
> you can do initialization with module constructor instead.
> https://dlang.org/spec/module.html#staticorder

But the input to the AA is static, it never changes. I thought D 
would essentially treat it as a constant and compute it once?

(I'm only using the AA in one place but it is in another template 
that is used twice. I can't imagine it would couldn't figure out 
how to optimzie it, I'm feeding it a constant so...)



More information about the Digitalmars-d-learn mailing list