Bug 11817 – Structs with void initialized fields get initialized anyway

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-12-25T13:38:54Z
Last change time
2017-08-25T10:06:58Z
Keywords
performance
Assigned to
No Owner
Creator
Walter Bright

Comments

Comment #0 by bugzilla — 2013-12-25T13:38:54Z
----- Code ----- struct S { int a; ubyte[100] b = void; } void test() { S s; s.a = 3; } --------------- The output: enter 068h,0 push ESI push EDI mov ESI,offset FLAT:_D4foo21S6__initZ lea EDI,-068h[EBP] mov ECX,01Ah rep movsd mov dword ptr -068h[EBP],3 pop EDI pop ESI leave ret I.e. the field s.b is initialized. While not pedantically a bug, this substantially interferes with performance tuning.
Comment #1 by maxim — 2013-12-25T13:55:30Z
Main question is what to do with aggregate members initialized to void. Currently default initialized instances have fields which are zeroed. T.init value is also zeroed, but both behaviors are under question. I think current practice should be reviewed because the issue tends to pop up instantly in NG in bugzilla. Meanwhile, recent discussions: http://d.puremagic.com/issues/show_bug.cgi?id=11331 https://d.puremagic.com/issues/show_bug.cgi?id=10643
Comment #2 by code — 2014-10-06T20:44:37Z
The compiler could skip those fields during initialization, but adding typeid(T).init arrays with gaps would be more difficult (require many changes). I'd say representing those gaps as 0 is the best trade-off for now, as it still allows to put the initializer into .bss.
Comment #3 by destructionator — 2016-04-23T13:48:52Z
There's also struct and field padding, which is defined as being filled with zeroes, so an initializer may have zeroes around it even if there's void. But, if everything at the end, including padding, is void-initialized, the .init could be truncated. If any code uses .init.length instead of .sizeof, such code would be broken, but I think that code is broken already. So the easy fix would be a truncated initializer - no holes in the middle (at least not at this point), but a big hole in the end... just you have to make sure there's no padding involved - the performance benefit would come only when you properly align things and set that to void too, which isn't hard to do. =void in the middle btw could just be zeroes in the initializer for now since it is undefined anyway.
Comment #4 by dfj1esp02 — 2016-05-27T16:45:25Z
I use it as a hack to have a zero-initialized member, results in a more efficient initialization of the struct - when it can be simply wiped with zeros.
Comment #5 by dfj1esp02 — 2017-08-25T10:06:58Z
*** This issue has been marked as a duplicate of issue 11331 ***