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
Commit pushed to master at https://github.com/D-Programming-Language/druntime https://github.com/D-Programming-Language/druntime/commit/c8897db61b76f4f2eac12581042adcfb35543bd4 fix Issue 7243 - Compiler should call separate function when allocating a struct on the heap
Comment #3 by github-bugzilla — 2012-04-29T14:05:47Z
Commit pushed to dmd-1.x at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/4004bb3b222676c74a41c0bb2507979963167457 fix Issue 7243 - Compiler should call separate function when allocating a struct on the heap
Comment #4 by bugzilla — 2012-04-29T14:06:50Z
Back to you, Steven!
Comment #5 by github-bugzilla — 2012-04-29T14:07:27Z
Commit pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/a766e4a395e4fb86ff31c6161d343d85805d96b3 fix Issue 7243 - Compiler should call separate function when allocating a struct on the heap
Comment #6 by schveiguy — 2012-04-30T07:56:53Z
Added pull request: https://github.com/D-Programming-Language/druntime/pull/202 Note that this isn't technically fixed until this pull request is incorporated. Also, I'm not sure this should affect D1, thoughts?
Comment #7 by github-bugzilla — 2012-05-11T10:48:13Z
Commit pushed to master at https://github.com/D-Programming-Language/druntime https://github.com/D-Programming-Language/druntime/commit/7375e9098e7852cd333d3ed6d211aa3eaf719352 Merge pull request #202 from schveiguy/issue7243 Implement druntime functions for allocating individual structs on the heap.