www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5787] New: Invisible multiple function calls in static foreach

reply d-bugmail puremagic.com writes:
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 puremagic.com
        ReportedBy: bearophile_hugs eml.cc



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: -------
Mar 27 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5787




Kai Meyer has shown this related program:


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


Its output:

foo
foo

So maybe the problem seems isn't caused by the static foreach.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 28 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5787




The problem is in .tupleof, where (expr).tupleof is rewritten to
TypeTuple!((expr).field0, (expr).field1, (expr).field2, ...) even if expr have
side effect, e.g.

-----------

import std.c.stdio : printf;

struct S {
    int x;
    int y;
}

void main() {
    cast(void)
    (printf("Hi\n"), S(2,3)).tupleof;
}

// print "Hi" twice.

-----------

This should be rewritten as (tmp=expr, TypeTuple!(tmp.field0, ...)).

(The ',' here is a comma expression.)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 28 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5787


SomeDude <lovelydear mailmetrash.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lovelydear mailmetrash.com



PDT ---
With 2.059 Win32

First example gives:

PS E:\DigitalMars\dmd2\samples> rdmd bug
foo
1
2

Second example:
PS E:\DigitalMars\dmd2\samples> rdmd bug
foo

Third example:
PS E:\DigitalMars\dmd2\samples> rdmd bug
Hi

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 23 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5787


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED



This issue has been already fixed as part of bug 4940.

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