-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Rc/Arc equality checking could short-circuit on equal pointers if T: Eq #42655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
We would ideally like to do without this flag and simply check `new_constraint_set != old_constraint_set`. However, this performs a full (slow) equality check, because immutable-rs doesn't short-circuit equality checking, which in turn is because Rc doesn't. rust-lang/rust#42655
Hm, I believe this is actually impossible to implement until we have support for specialization (#31844). Please correct me if I'm wrong. |
Specialization already exists - it's just unstable. That doesn't matter for use in the standard library, though. |
Tagging C-feature-accepted based on the discussion in #42965. Someone will need to rebase the PR and address the review comments. |
This optimization was implemented by servo/servo#18793 in a fork of It occurs to me that if this is valid for |
The &T case was discussed in #42965. |
The
PartialEq
implementations forRc
andArc
currently always defer to the equality implementations for their inner values (rc.rs:770):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 checkself.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.
The text was updated successfully, but these errors were encountered: