AA code 50x slower
Steven Schveighoffer
schveiguy at gmail.com
Sun Feb 16 17:44:04 UTC 2020
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
More information about the Digitalmars-d-learn
mailing list