Description
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.