[Issue 5787] New: Invisible multiple function calls in static foreach

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Mar 27 08:13:37 PDT 2011


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

           Summary: Invisible multiple function calls in static foreach
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Keywords: performance
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2011-03-27 08:10:09 PDT ---
Through profiling I have found a performance problem in my code. I have later
reduced the case and created this demo code:


import std.typecons: tuple;
import std.c.stdio: printf;
auto foo() {
    printf("foo\n");
    return tuple(1, 2);
}
void main() {
    foreach (x; foo().tupleof)
        printf("%d\n", x);
}


Its output, DMD 2.052:

foo
1
foo
2

In my code foo() was an expensive computation.
In my opinion in this situation foo() needs to be computed only once (or I'd
like some other solution to avoid this invisible multiple calls, like some kind
of error, etc).

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


More information about the Digitalmars-d-bugs mailing list