Skip to content

Commit 4d6eca1

Browse files
committed
Auto merge of rust-lang#2218 - Nilstrieb:faster-tag-partial-eq, r=RalfJung
Optimize `SbTag::eq` The code before generated really bad code with a branch. This nudges LLVM towards being smarter and simply comparing the integers. See rust-lang/miri#2214 (comment)
2 parents 5a76e9f + 93db9a6 commit 4d6eca1

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
clippy::single_match,
1818
clippy::useless_format,
1919
clippy::derive_partial_eq_without_eq,
20+
clippy::derive_hash_xor_eq,
2021
clippy::too_many_arguments
2122
)]
2223

src/stacked_borrows.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,30 @@ pub type CallId = NonZeroU64;
2727
pub type AllocExtra = Stacks;
2828

2929
/// Tracking pointer provenance
30-
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
30+
#[derive(Copy, Clone, Hash, Eq)]
3131
pub enum SbTag {
3232
Tagged(PtrId),
3333
Untagged,
3434
}
3535

36+
impl SbTag {
37+
fn as_u64(self) -> u64 {
38+
match self {
39+
SbTag::Tagged(id) => id.get(),
40+
SbTag::Untagged => 0,
41+
}
42+
}
43+
}
44+
45+
impl PartialEq for SbTag {
46+
fn eq(&self, other: &Self) -> bool {
47+
// The codegen for the derived Partialeq is bad here and includes a branch.
48+
// Since this code is extremely hot, this is optimized here.
49+
// https://github.com/rust-lang/rust/issues/49892
50+
self.as_u64() == other.as_u64()
51+
}
52+
}
53+
3654
impl fmt::Debug for SbTag {
3755
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3856
match self {

0 commit comments

Comments
 (0)