www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 18899] New: destroy is inefficient for small structs


          Issue ID: 18899
           Summary: destroy is inefficient for small structs
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: druntime
          Assignee: nobody puremagic.com
          Reporter: schveiguy yahoo.com

When destroy is called on a small struct, it runs code like this:

    shared static immutable T init = T.init;

    ()  trusted {
        auto dest = (cast(ubyte*) &obj)[0 .. T.sizeof];
        auto src = (cast(ubyte*) &init)[0 .. T.sizeof];
        dest[] = src[];
    } ();

Which is WAY overkill for a struct like:

struct S
   int x;

Using obj = T.init should be done for cases where it's proven to be proper. In
other words, no postblit (or disabled postblit).

It used to be that this function used typeid, and the initializer within. One
of the speedups is if the type is all 0's, then buf[] = 0 can be used. Not sure
if there's a mechanism to tell if a type is all zeros, but if it can be done,
that would be faster.

In addition, maybe using the ubyte array is more efficient in some cases,
depending on the size. But I'm not sure.

May 23 2018