[Issue 12313] New: Unneeded stack temporaries created by tuple foreach

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Mar 7 07:23:10 PST 2014


https://d.puremagic.com/issues/show_bug.cgi?id=12313

           Summary: Unneeded stack temporaries created by tuple foreach
           Product: D
           Version: D1 & D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: public at dicebot.lv


--- Comment #0 from Dicebot <public at dicebot.lv> 2014-03-07 07:23:09 PST ---
This issue is present for both D1 and D2 but seems to be partially compensated
by better optimizer in dmd2.

Current foreach implementation (ForeachStatement::semantic) creates a local
stack temporary for every element of expression tuple foreach it iterates over:

=== test.d ===
struct A
{
    int a, b, c;
}

void main()
{
    A a;
    // this empty loop creates variables as copies of every member of a
    foreach (var; a.tupleof)
    {}
}
======

There are two issues with it:

1) stack space can easily go out of control with algorithms on long tuples (it
has just his us in Sociomantic)

2) it prevents one from writing to `var` and forces to use ugly index idiom
instead: `foreach (index, var; a.tupleof) { a.tupleof[index] = 42; }`

Can't tell possible impact of removing those temporaries right now, needs some
experiments.

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


More information about the Digitalmars-d-bugs mailing list