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

           Summary: Stack allocation for small scope dynamic arrays
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nob...@puremagic.com
        ReportedBy: bearophile_h...@eml.cc


--- Comment #0 from bearophile_h...@eml.cc 2010-06-21 04:52:09 PDT ---
Dynamic arrays are used very often in D, much more often than malloc-allocated
arrays in C programs.

A scope dynamic array lives only inside the function it's allocated. So with
this program:


int[] foo(int n) {
    scope arr = new int[n];
    return arr;
}
void main() {}


DMD v2.047 generates this error:
test.d(3): Error: escaping reference to scope local arr

So the compiler can add an inlined runtime test: if the amount of memory
necessary to allocate 'arr' is "small" (like few hundred bytes) (or better if
it is small compared to the free space currently available on the stack), then
the memory for 'arr' can be allocated on the stack instead of the heap, with an
alloca. This can improve performance a little if foo() is called many times.

Compared to C programs (or D programs written in C-like style), this
optimization can make the D idiom of using dynamic arrays often less costly,
and avoids most of the need of the Variable Length Arrays of C99.

This enhancement request is marked as relative to the DMD component (instead of
being just a runtime thing) because the test and the optional stack allocation
need to be inlined.

A D compiler can perform the same optimization on dynamic array allocation even
if the 'scope' attribute is missing if it is able to perform some escape
analysis and it is able to verify that 'arr' never escapes foo().

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

Reply via email to