[Issue 11331] New: Inefficient initialization of struct

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Oct 23 08:54:27 PDT 2013


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

           Summary: Inefficient initialization of struct
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: andrei at erdani.com


--- Comment #0 from Andrei Alexandrescu <andrei at erdani.com> 2013-10-23 08:54:25 PDT ---
Consider the following stack region:

struct InSituRegion2(size_t size)
{
    // The store will be aligned to realof.align
    union
    {
        private ubyte[size] _store = void;
        real _forAlignmentOnly;
    }
    void* _crt, _end;

    void[] allocate(size_t bytes)
    {
          assert(_crt && _end);
        // round up
        const rounded = (bytes + real.alignof - 1) / real.alignof;
        auto newCrt = _crt + rounded;
        if (newCrt > _end) return null;
        auto result = _crt[0 .. bytes];
        _crt = newCrt;
        return result;
    }
}

size_t fun2(size_t s)
{
    InSituRegion2!(1024 * 64) r;
      r._crt = r._store.ptr;
      r._end = r._store.ptr + r._store.length;
    auto a = cast(uint[]) r.allocate(s);
    return a[s / 2];
}

(The code of fun2 has been written to be complex enough to avoid a few elisions
effected by optimizers.)

Disassembly reveals that constructing the struct object entails a memcpy of the
object's init over the object memory, even though most of the object is
deliberately left uninitialized. This undoes the performance gains of defining
and using an encapsulated stack region.

The initialization function should either use multiple memcpy calls or
individual word assignments.

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


More information about the Digitalmars-d-bugs mailing list