www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - AA code 50x slower

reply AlphaPurned <Alpha Beta.com> writes:
template AA(string[] S)
{
	auto _do() { int[string] d; foreach(s; S) d[s] = 0; return d; }
	enum AA = _do;
}


if (t in AA!(["a", "and", "mp4", "mp3", "the", "with", "live", 
"no", "&", "of", "band"])) continue;		


The if statement literally causes a 50x slow down of the code. 
(LDC debug, dmd debug takes about 1000 times longer)
Feb 16 2020
next sibling parent reply evilrat <evilrat666 gmail.com> writes:
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
Feb 16 2020
parent reply AlphaPurned <Alpha Beta.com> writes:
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...)
Feb 16 2020
parent evilrat <evilrat666 gmail.com> writes:
On Monday, 17 February 2020 at 02:18:15 UTC, AlphaPurned wrote:
 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...)
The reason is that AA is a runtime-dependent construct, but it's not available at compile time, nor that there is some known ahead of time interface to it(ok, not exactly but something like that), so instead it does this whenever AA enum is referenced. See Steven's code above, it might work like you thought however despite whatever input it will likely have exactly one variant of it because instantiated on string[] type, if that's the case and this is not what you wanted you might try tuple parameter instead of string[].
Feb 16 2020
prev sibling next sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 2/16/20 7:57 AM, AlphaPurned wrote:
 template AA(string[] S)
 {
      auto _do() { int[string] d; foreach(s; S) d[s] = 0; return d; }
      enum AA = _do;
evilrat is 100% correct. Each time you call that if statement, it constructs a NEW AA, checks to see if the t parameter is in there, and then leaves it for garbage. In order to fix it, you have to initialize it in a shared static constructor (D doesn't currently allow static initialization of runtime AAs). example: template AA(string[] S) { __gshared const int[string] AA; enum _do = (){ int[string] d; foreach(s; S) d[s] = 0; return d; }(); shared static this() { AA = _do; } } -Steve
Feb 16 2020
prev sibling parent Nathan S. <no.public.email example.com> writes:
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;
 }


 if (t in AA!(["a", "and", "mp4", "mp3", "the", "with", "live", 
 "no", "&", "of", "band"])) continue;		


 The if statement literally causes a 50x slow down of the code. 
 (LDC debug, dmd debug takes about 1000 times longer)
template AA(string[] S) { __gshared int[string] AA; shared static this() { int[string] d; foreach (s; S) d[s] = 0; AA = d; }; } if (t in AA!(["a", "and", "mp4", "mp3", "the", "with", "live", "no", "&", "of", "band"])) continue; This will have the performance you want if you don't care whether it works in CTFE.
Feb 16 2020