GC vs. Manual Memory Management Real World Comparison

Benjamin Thaut code at benjamin-thaut.de
Wed Sep 5 05:27:38 PDT 2012


Am 05.09.2012 14:14, schrieb Alex Rønne Petersen:
>
> Where's the catch? From looking in druntime, I don't see where the
> allocation could occur.
>

Everything is in object_.d:

     equals_t opEquals(Object lhs, Object rhs)
     {
         if (lhs is rhs)
             return true;
         if (lhs is null || rhs is null)
             return false;
         if (typeid(lhs) == typeid(rhs))
             return lhs.opEquals(rhs);
         return lhs.opEquals(rhs) &&
                rhs.opEquals(lhs);
     }

Will trigger a comparison of the TypeInfo objects with
if (typeid(lhs) == typeid(rhs))

Which will after some function calls trigger opEquals of TypeInfo

     override equals_t opEquals(Object o)
     {
         /* TypeInfo instances are singletons, but duplicates can exist
          * across DLL's. Therefore, comparing for a name match is
          * sufficient.
          */
         if (this is o)
             return true;
         TypeInfo ti = cast(TypeInfo)o;
         return ti && this.toString() == ti.toString();
     }

Then because they are const, TypeInfo_Const.toString() will be called:

     override string toString()
     {
         return cast(string) ("const(" ~ base.toString() ~ ")");
     }

which allocates, due to array concardination.

But this only happens, if they are not of the same type, and if one of 
them has a storage qualifier.

Kind Regards
Benjamin Thaut



More information about the Digitalmars-d-announce mailing list