www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4357] New: Stack allocation for small scope dynamic arrays

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


--- Comment #0 from bearophile_hugs 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: -------
Jun 21 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4357


nfxjfg gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |nfxjfg gmail.com
         Resolution|                            |WONTFIX


--- Comment #1 from nfxjfg gmail.com 2010-06-21 05:03:49 PDT ---
Andrei said this use of scope is gone.
Sorry pal, WONTFIX *bamm*

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 21 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4357



--- Comment #2 from bearophile_hugs eml.cc 2010-06-21 11:23:19 PDT ---
Sorry, I didn't know that. I have thought that the removal of the scope
attribute was only for class instances.

Even if the scope attribute can't be used on dynamic arrays, the last point I
have expressed applies still: if the compiler is able to perform escape
analysis (LLVM has some of such capability) and it can statically verify a
dynamic array never escapes, then if a runtime test shows such dynamic arrays
is small it can be allocated on the stack.

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