Thread overview
[Issue 3917] New: opEquals for Ojbect could be more efficient
March 09, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3917

           Summary: opEquals for Ojbect could be more efficient
           Product: D
           Version: 2.041
          Platform: Other
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: druntime
        AssignedTo: sean@invisibleduck.org
        ReportedBy: schveiguy@yahoo.com


--- Comment #0 from Steven Schveighoffer <schveiguy@yahoo.com> 2010-03-09 12:42:34 PST ---
The implementation of opEquals in object_.d has an unnecessary recursive call. The complete implementation is this:

bool opEquals(Object lhs, Object rhs)
{
    // If aliased to the same object or both null => equal
    if (lhs is rhs) return true;

    // If either is null => non-equal
    if (lhs is null || rhs is null) return false;

    // If same exact type => one call to method opEquals
    if (typeid(lhs) == typeid(rhs)) return lhs.opEquals(rhs);

    // General case => symmetric calls to method opEquals
    return lhs.opEquals(rhs) && rhs.opEquals(lhs);
}

bool opEquals(TypeInfo lhs, TypeInfo rhs)
{
    // If aliased to the same object or both null => equal
    if (lhs is rhs) return true;

    // If either is null => non-equal
    if (lhs is null || rhs is null) return false;

    // If same exact type => one call to method opEquals
    if (typeid(lhs) == typeid(rhs)) return lhs.opEquals(rhs);

    // Factor out top level const
    // (This still isn't right, should follow same rules as compiler does for
type equality.)
    TypeInfo_Const c = cast(TypeInfo_Const) lhs;
    if (c)
        lhs = c.base;
    c = cast(TypeInfo_Const) rhs;
    if (c)
        rhs = c.base;

    // General case => symmetric calls to method opEquals
    return lhs.opEquals(rhs) && rhs.opEquals(lhs);
}


The third if statement that compares two typeids recurses into opEquals for typeinfos.  In the typeinfo comparison, the typeinfos should never be null, and the typeinfos of the typeinfo will always be identical (TypeInfo_Class).  In addition, the stuff at the end dealing with const will never come into play because the dynamic typeinfo (the classinfo) never deals with const.

I'd propose to rewrite the opEquals as follows:

bool opEquals(Object lhs, Object rhs)
{
    // If aliased to the same object or both null => equal
    if (lhs is rhs) return true;

    // If either is null => non-equal
    if (lhs is null || rhs is null) return false;

    // If same exact type => one call to method opEquals
    if (typeid(lhs) is typeid(rhs) || typeid(lhs).opEquals(typeid(rhs))) return
lhs.opEquals(rhs);

    // General case => symmetric calls to method opEquals
    return lhs.opEquals(rhs) && rhs.opEquals(lhs);
}

I don't know what else the opEquals that compares typeinfos is for, because it's not a public function, so it could potentially be removed.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 13, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3917


Lars T. Kyllingstad <bugzilla@kyllingen.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@kyllingen.net
         Resolution|                            |FIXED


--- Comment #1 from Lars T. Kyllingstad <bugzilla@kyllingen.net> 2010-08-13 01:56:44 PDT ---
Fixed DMD 2.048.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 10, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=3917



--- Comment #2 from github-bugzilla@puremagic.com 2013-03-09 21:34:17 PST ---
Commit pushed to master at https://github.com/D-Programming-Language/druntime

https://github.com/D-Programming-Language/druntime/commit/87051cc25c5e69889a2084e1bf562fa798cbee93 Remove unused object.opEquals(TypeInfo, TypeInfo)

It seems that just had been forgotten to remove it when bug 3917 was fixed.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------