Skip to content

Rc/Arc equality checking could short-circuit on equal pointers if T: Eq #42655

Closed
@joliss

Description

@joliss

The PartialEq implementations for Rc and Arc currently always defer to the equality implementations for their inner values (rc.rs:770):

    fn eq(&self, other: &Rc<T>) -> bool {
        **self == **other
    }

However, where T: Eq, we may assume that the inner values obey reflexivity (a == a), and so we could short-circuit the comparison if the pointers are equal. That is, instead of checking **self == **other, we could check self.ptr_eq(other) || **self == **other.

It is conceivable that in rare cases the equality-check on the inner value is so cheap that attempting to short-circuit adds more overhead than it saves. However, my sense is that Rc is usually used on types that are too complex to clone cheaply, and so it stands to reason that in most cases, the equality check on the inner value is expensive. (This is certainly true for the use cases I've encountered.) Checking equality on the pointers furthermore is very cheap, since we're dereferencing them anyway.

I'm cautiously volunteering a pull request if people think that this is a reasonable change. Update: Pull request at #42965.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-feature-acceptedCategory: A feature request that has been accepted pending implementation.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions