www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4244] New: AA insert from fixed-sized array much slower than from equivalent struct

http://d.puremagic.com/issues/show_bug.cgi?id=4244

           Summary: AA insert from fixed-sized array much slower than from
                    equivalent struct
           Product: D
           Version: future
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: performance
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



This D2 program gets much slower if foo1() is used inside the main() instead of
foo2() that apparently does something very similar (compiled with dmd v2.046,
__gshared not used, compilation arguments -O -release -inline):


import std.c.stdio: printf;

int foo1(int x, int y) {
    static int[int[2]] cache;
    int[2] args = [x, y];
    cache[args] = x;
    return x;
}

int foo2(int x, int y) {
    static struct Pair { int x, y; }
    static int[Pair] cache;
    Pair args = Pair(x, y);
    cache[args] = x;
    return x;
}

void main() {
    enum int N = 600;
    int tot;
    foreach (x; 1 .. N)
        foreach (y; 1 .. N)
            tot += foo1(x, y);
    printf("%d\n", tot);
}



An experiment shows that foo1 gets quite faster if the [x,y] is written inside
the cache AA itself:

int foo1(int x, int y) {
    static int[int[2]] cache;
    int[2] args = [x, y];
    cache[[x, y]] = x;
    return x;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 27 2010