Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 6a99ed1

Browse files
committed
Remove static initializer from struct destroy.
This change removes a shared static initializer from a `destroy` implementation for structs, and with it a dependency on `memcpy`.
1 parent feb9616 commit 6a99ed1

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/object.d

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -506,20 +506,21 @@ any GC memory.
506506
*/
507507
void destroy(T)(ref T obj) if (is(T == struct))
508508
{
509-
// We need to re-initialize `obj`. Previously, the code
510-
// `auto init = cast(ubyte[])typeid(T).initializer()` was used, but
511-
// `typeid` is a runtime call and requires the `TypeInfo` object which is
512-
// not usable when compiling with -betterC. If we do `obj = T.init` then we
513-
// end up needlessly calling postblits and destructors. So, we create a
514-
// static immutable lvalue that can be re-used with subsequent calls to `destroy`
515-
shared static immutable T init = T.init;
509+
// We need to re-initialize `obj`. Previously, an immutable static
510+
// and memcpy were used to hold an initializer. With improved unions, this is no longer
511+
// needed.
512+
union UntypedInit
513+
{
514+
T dummy;
515+
}
516+
static struct UntypedStorage
517+
{
518+
align(T.alignof) void[T.sizeof] dummy;
519+
}
516520

517521
_destructRecurse(obj);
518522
() @trusted {
519-
import core.stdc.string : memcpy;
520-
auto dest = (cast(ubyte*) &obj)[0 .. T.sizeof];
521-
auto src = (cast(ubyte*) &init)[0 .. T.sizeof];
522-
memcpy(dest.ptr, src.ptr, T.sizeof);
523+
*cast(UntypedStorage*) &obj = cast(UntypedStorage) UntypedInit.init;
523524
} ();
524525
}
525526

0 commit comments

Comments
 (0)