From 956a84bfe0502e126dcdd173fc84eecc10df5ebc Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Wed, 8 Jun 2022 18:22:48 +0200 Subject: [PATCH 1/3] Optimize `SbTag::eq` The code before generated really bad code with a branch. This nudges LLVM towards being smarter and simply comparing the integers. --- src/stacked_borrows.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/stacked_borrows.rs b/src/stacked_borrows.rs index 0d671ec653..fd33eea790 100644 --- a/src/stacked_borrows.rs +++ b/src/stacked_borrows.rs @@ -27,12 +27,27 @@ pub type CallId = NonZeroU64; pub type AllocExtra = Stacks; /// Tracking pointer provenance -#[derive(Copy, Clone, Hash, PartialEq, Eq)] +#[derive(Copy, Clone, Hash, Eq)] pub enum SbTag { Tagged(PtrId), Untagged, } +impl SbTag { + fn as_u64(self) -> u64 { + match self { + SbTag::Tagged(id) => id.get(), + SbTag::Untagged => 0, + } + } +} + +impl PartialEq for SbTag { + fn eq(&self, other: &Self) -> bool { + self.as_u64() == other.as_u64() + } +} + impl fmt::Debug for SbTag { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { From c01bf62ee42f7fa28a95449b374812cb47d38a95 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Wed, 8 Jun 2022 18:43:12 +0200 Subject: [PATCH 2/3] Allow `clippy::derive_hash_xor_eq` --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index 7d8eb92ac5..22869e9b24 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,6 +17,7 @@ clippy::single_match, clippy::useless_format, clippy::derive_partial_eq_without_eq, + clippy::derive_hash_xor_eq, clippy::too_many_arguments )] From 93db9a6d71a7f66df81b5c8adbac7bb71d182bc1 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Wed, 8 Jun 2022 19:29:54 +0200 Subject: [PATCH 3/3] Add comment to explain manual optimization --- src/stacked_borrows.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/stacked_borrows.rs b/src/stacked_borrows.rs index fd33eea790..88d1b1f105 100644 --- a/src/stacked_borrows.rs +++ b/src/stacked_borrows.rs @@ -44,6 +44,9 @@ impl SbTag { impl PartialEq for SbTag { fn eq(&self, other: &Self) -> bool { + // The codegen for the derived Partialeq is bad here and includes a branch. + // Since this code is extremely hot, this is optimized here. + // https://github.com/rust-lang/rust/issues/49892 self.as_u64() == other.as_u64() } }