Thread overview
[Issue 4487] New: 16 bytes long structs requires 32 bytes if allocated singularly on the heap
Dec 20, 2012
Andrej Mitrovic
July 19, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4487

           Summary: 16 bytes long structs requires 32 bytes if allocated
                    singularly on the heap
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: performance
          Severity: major
          Priority: P2
         Component: druntime
        AssignedTo: sean@invisibleduck.org
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2010-07-19 12:46:34 PDT ---
(This is my first bug report with 'major' severity, because this is a quite
important bug.)

This comes after a report by Steven Schveighoffer. This program allocates a linked list of 10 million structs on the heap (this number is set to high just to improve the measurements).

The presence of GC.disable() doesn't change the total memory allocated, but decreases a lot the run time. On a 32 bit Windows at the end of the list allocation this program has allocated about 326 MB, it means:

326_200_000 bytes / 10_000_000 ~= 32.62 bytes each Foo

This can't be accepted in a serious "system language" (also because 16 bytes long structs are quite common in my 32 bit code).

import core.memory: GC;
struct Foo {
    Foo* next;
    ubyte[12] arr;
    this(Foo* ptr) { this.next = ptr; }
}
static assert(Foo.sizeof == 16);
void main() {
    GC.disable();
    enum n = 10_000_000;
    Foo* lh;
    foreach (i; 0 .. n)
        lh = new Foo(lh);
    GC.enable();
}


Maybe this bug can be fixed introducing a specific allocator function for single structs, that don't sees them as arrays of length 1 (that needs 1 byte of information padding for appends).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 19, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4487


Steven Schveighoffer <schveiguy@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy@yahoo.com
          Component|druntime                    |DMD
         AssignedTo|sean@invisibleduck.org      |nobody@puremagic.com
           Severity|major                       |enhancement


--- Comment #1 from Steven Schveighoffer <schveiguy@yahoo.com> 2010-07-19 13:10:48 PDT ---
DMD is the main culprit here, not druntime.  And this is not a bug, it's an enhancement.  DMD functions exactly as designed.

DMD is the one generating the code to call the arrayNew function with length 1.
 Druntime cannot tell between someone actually allocating an array of 1 or
someone allocating a single struct.

With the "Appendable" bit I just added for druntime, this could be alleviated if the compiler would call a separate function for struct allocators.

As a workaround, you can pre-allocate a large block of nodes, which will only have one byte of pad per block allocated.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 20, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4487


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich@gmail.com


--- Comment #2 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2012-12-20 14:14:57 PST ---
I can only see around 160MB used now, bear please verify and close if it's fixed, thanks.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 21, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4487


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


--- Comment #3 from bearophile_hugs@eml.cc 2012-12-20 16:48:22 PST ---
(In reply to comment #2)
> I can only see around 160MB used now, bear please verify and close if it's fixed, thanks.

It was fixed time ago. Closed.

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