[Issue 14451] static-foreach uses huge stack for no reason

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Thu Apr 16 09:29:15 PDT 2015


https://issues.dlang.org/show_bug.cgi?id=14451

--- Comment #2 from Tomer Filiba <tomerfiliba at gmail.com> ---
you are correct:

===================
void foo() {
    {
        ulong[32] x;
        writeln(x.ptr);
    }
    {
        ulong[32] x;
        writeln(x.ptr);
    }
    {
        ulong[32] x;
        writeln(x.ptr);
    }
    {
        ulong[32] x;
        writeln(x.ptr);
    }
    {
        ulong[32] x;
        writeln(x.ptr);
    }
}
===================

yields

7FFFB9A1E8A0
7FFFB9A1E9A0
7FFFB9A1EAA0
7FFFB9A1EBA0
7FFFB9A1ECA0

it seems a pity to waste so much stack... having the backend reuse dead parts
of the stack is crucial for embedded applications or heavily-parallel ones that
rely to thousands of small stacks.

of course it could be re-written

====================
void foo() {
    (){
        ulong[32] x;
        writeln(x.ptr);
    }();
    (){
        ulong[32] x;
        writeln(x.ptr);
    }();
    (){
        ulong[32] x;
        writeln(x.ptr);
    }();
    (){
        ulong[32] x;
        writeln(x.ptr);
    }();
    (){
        ulong[32] x;
        writeln(x.ptr);
    }();
}
===================

which then works just fine:

7FFF4101C4A0
7FFF4101C4A0
7FFF4101C4A0
7FFF4101C4A0
7FFF4101C4A0

couldn't inner-scopes be rewritten like that, only making sure the lambda is
fully-scoped so that external variables will be heap-allocated

--


More information about the Digitalmars-d-bugs mailing list