How to get to a class initializer through introspection?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Tue Aug 4 02:03:34 UTC 2020


On 8/3/20 10:44 AM, Johan wrote:
> On Monday, 3 August 2020 at 13:01:55 UTC, Andrei Alexandrescu wrote:
>> Would it be effective to iterate through the .tupleof and initialize 
>> each in turn?
> 
> Possibly. IIRC, the spec obliges us to initialize the padding in-between 
> address-aligned members aswell, such that a memcmp works to compare 
> structs. If that is true, then we have to initialize the padding aswell 
> and a memcpy would be that much nicer.

To play devil's advocate, the padding bytes should not have been changed 
by user code in the first place :o).

> If `__traits(type, getInitializer)` would return a symbol, then memcpy 
> is easy and we're done. However, that forces us to emit these init 
> symbols, except for very simple cases like all-zeros initialization (for 
> which LDC does no longer emit an init symbol). It is very benificial for 
> binary size to elide these all-zero initializer symbols. I don't know 
> how much benefit there is for eliding near-zero symbols.
> If `__traits(type, getInitializer)` would return a function, then that's 
> a different story...
> 
> My current solution [*]: 
> https://github.com/weka-io/druntime/blob/0dab4b0dc5cbccb891351095ff09b0558e3fbe06/src/core/internal/lifetime.d#L92-L140 
> 
> 
> -Johan
> 
> [*] Hits an obscure mangling bug, so doesn't quite work with Weka's 
> codebase yet

Cool. Also the https://dlang.org/spec/traits.html#isZeroInit flag may help.

There's also the related trickery TypeInfo uses for the initializer() 
function:

https://github.com/dlang/druntime/blob/master/src/object.d#L390

Array with null pointer and non-zero length.

There's also offset info for fields available so I'd say there is enough 
material for a complete solution.

Just throwing everything at the wall...


More information about the Digitalmars-d mailing list