Bug 7243 – Compiler should call separate function when allocating a struct on the heap
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-01-07T12:00:00Z
Last change time
2012-05-11T10:56:49Z
Assigned to
nobody
Creator
schveiguy
Comments
Comment #0 by schveiguy — 2012-01-07T12:00:56Z
Note, this requires druntime fixes too, but I can't select both DMD and druntime.
Currently, when creating a struct on the heap, _d_newarrayT is called with a length of 1.
However, the runtime must assume this is an array of length 1. Therefore, it must set the appendable bit, and reserve space for the 'used' length.
This can waste space.
For instance, the following could fit in a 16-byte block, but will go into a 32-byte block due to the used field:
import core.memory;
struct S
{
int[4] buf;
}
void main()
{
auto s = new S;
assert(GC.sizeof(s) == 32);
}
I think instead, a _d_newstruct function should be called (and implemented in druntime) to initialize a single struct on the heap in the most efficient manner possible.
Comment #1 by schveiguy — 2012-04-29T11:29:39Z
Martin Nowak has found a very compelling case for this to be fixed, it is affecting performance of RedBlackTree.
From bug 5650:
When in 64-bit mode, each pointer is 8 bytes. RedBlackNodes have a left, right, and parent pointer, consuming 24 bytes. Then it contains the payload, followed by a byte for the color (red or black). The basic RedBlackTree!int case has a payload of int, or 4 bytes. With alignment padding, this brings the size of the struct to 32 bytes.
Now, given that array appending needs the extra byte for 'used' length (also serves as a sentinel to prevent accidentally accessing the subsequent block), this will be with array padding, 33 bytes, pushing the block required to 64 bytes.
Not only that, but because the struct has pointers, it must zero out those 31 unused bytes.
With his measurements as quoted in bug 5650, fixing this problem resulted in roughly a 40% runtime reduction.
Can we please get this fixed? I'll be happy to implement the druntime function if someone will take care of the compiler part.
Comment #2 by github-bugzilla — 2012-04-29T14:04:13Z