Skip to content

Commit 575d424

Browse files
committed
Auto merge of rust-lang#107717 - nnethercote:opt-TyKind-eq, r=compiler-errors
Optimize `TyKind::eq`. r? `@ghost`
2 parents ef934d9 + 1dbed69 commit 575d424

File tree

1 file changed

+45
-41
lines changed
  • compiler/rustc_type_ir/src

1 file changed

+45
-41
lines changed

compiler/rustc_type_ir/src/sty.rs

+45-41
Original file line numberDiff line numberDiff line change
@@ -310,47 +310,51 @@ impl<I: Interner> Clone for TyKind<I> {
310310
impl<I: Interner> PartialEq for TyKind<I> {
311311
#[inline]
312312
fn eq(&self, other: &TyKind<I>) -> bool {
313-
tykind_discriminant(self) == tykind_discriminant(other)
314-
&& match (self, other) {
315-
(Int(a_i), Int(b_i)) => a_i == b_i,
316-
(Uint(a_u), Uint(b_u)) => a_u == b_u,
317-
(Float(a_f), Float(b_f)) => a_f == b_f,
318-
(Adt(a_d, a_s), Adt(b_d, b_s)) => a_d == b_d && a_s == b_s,
319-
(Foreign(a_d), Foreign(b_d)) => a_d == b_d,
320-
(Array(a_t, a_c), Array(b_t, b_c)) => a_t == b_t && a_c == b_c,
321-
(Slice(a_t), Slice(b_t)) => a_t == b_t,
322-
(RawPtr(a_t), RawPtr(b_t)) => a_t == b_t,
323-
(Ref(a_r, a_t, a_m), Ref(b_r, b_t, b_m)) => a_r == b_r && a_t == b_t && a_m == b_m,
324-
(FnDef(a_d, a_s), FnDef(b_d, b_s)) => a_d == b_d && a_s == b_s,
325-
(FnPtr(a_s), FnPtr(b_s)) => a_s == b_s,
326-
(Dynamic(a_p, a_r, a_repr), Dynamic(b_p, b_r, b_repr)) => {
327-
a_p == b_p && a_r == b_r && a_repr == b_repr
328-
}
329-
(Closure(a_d, a_s), Closure(b_d, b_s)) => a_d == b_d && a_s == b_s,
330-
(Generator(a_d, a_s, a_m), Generator(b_d, b_s, b_m)) => {
331-
a_d == b_d && a_s == b_s && a_m == b_m
332-
}
333-
(GeneratorWitness(a_g), GeneratorWitness(b_g)) => a_g == b_g,
334-
(
335-
&GeneratorWitnessMIR(ref a_d, ref a_s),
336-
&GeneratorWitnessMIR(ref b_d, ref b_s),
337-
) => a_d == b_d && a_s == b_s,
338-
(Tuple(a_t), Tuple(b_t)) => a_t == b_t,
339-
(Alias(a_i, a_p), Alias(b_i, b_p)) => a_i == b_i && a_p == b_p,
340-
(Param(a_p), Param(b_p)) => a_p == b_p,
341-
(Bound(a_d, a_b), Bound(b_d, b_b)) => a_d == b_d && a_b == b_b,
342-
(Placeholder(a_p), Placeholder(b_p)) => a_p == b_p,
343-
(Infer(a_t), Infer(b_t)) => a_t == b_t,
344-
(Error(a_e), Error(b_e)) => a_e == b_e,
345-
(Bool, Bool) | (Char, Char) | (Str, Str) | (Never, Never) => true,
346-
_ => {
347-
debug_assert!(
348-
false,
349-
"This branch must be unreachable, maybe the match is missing an arm? self = self = {self:?}, other = {other:?}"
350-
);
351-
true
352-
}
313+
// You might expect this `match` to be preceded with this:
314+
//
315+
// tykind_discriminant(self) == tykind_discriminant(other) &&
316+
//
317+
// but the data patterns in practice are such that a comparison
318+
// succeeds 99%+ of the time, and it's faster to omit it.
319+
match (self, other) {
320+
(Int(a_i), Int(b_i)) => a_i == b_i,
321+
(Uint(a_u), Uint(b_u)) => a_u == b_u,
322+
(Float(a_f), Float(b_f)) => a_f == b_f,
323+
(Adt(a_d, a_s), Adt(b_d, b_s)) => a_d == b_d && a_s == b_s,
324+
(Foreign(a_d), Foreign(b_d)) => a_d == b_d,
325+
(Array(a_t, a_c), Array(b_t, b_c)) => a_t == b_t && a_c == b_c,
326+
(Slice(a_t), Slice(b_t)) => a_t == b_t,
327+
(RawPtr(a_t), RawPtr(b_t)) => a_t == b_t,
328+
(Ref(a_r, a_t, a_m), Ref(b_r, b_t, b_m)) => a_r == b_r && a_t == b_t && a_m == b_m,
329+
(FnDef(a_d, a_s), FnDef(b_d, b_s)) => a_d == b_d && a_s == b_s,
330+
(FnPtr(a_s), FnPtr(b_s)) => a_s == b_s,
331+
(Dynamic(a_p, a_r, a_repr), Dynamic(b_p, b_r, b_repr)) => {
332+
a_p == b_p && a_r == b_r && a_repr == b_repr
353333
}
334+
(Closure(a_d, a_s), Closure(b_d, b_s)) => a_d == b_d && a_s == b_s,
335+
(Generator(a_d, a_s, a_m), Generator(b_d, b_s, b_m)) => {
336+
a_d == b_d && a_s == b_s && a_m == b_m
337+
}
338+
(GeneratorWitness(a_g), GeneratorWitness(b_g)) => a_g == b_g,
339+
(&GeneratorWitnessMIR(ref a_d, ref a_s), &GeneratorWitnessMIR(ref b_d, ref b_s)) => {
340+
a_d == b_d && a_s == b_s
341+
}
342+
(Tuple(a_t), Tuple(b_t)) => a_t == b_t,
343+
(Alias(a_i, a_p), Alias(b_i, b_p)) => a_i == b_i && a_p == b_p,
344+
(Param(a_p), Param(b_p)) => a_p == b_p,
345+
(Bound(a_d, a_b), Bound(b_d, b_b)) => a_d == b_d && a_b == b_b,
346+
(Placeholder(a_p), Placeholder(b_p)) => a_p == b_p,
347+
(Infer(a_t), Infer(b_t)) => a_t == b_t,
348+
(Error(a_e), Error(b_e)) => a_e == b_e,
349+
(Bool, Bool) | (Char, Char) | (Str, Str) | (Never, Never) => true,
350+
_ => {
351+
debug_assert!(
352+
tykind_discriminant(self) != tykind_discriminant(other),
353+
"This branch must be unreachable, maybe the match is missing an arm? self = self = {self:?}, other = {other:?}"
354+
);
355+
false
356+
}
357+
}
354358
}
355359
}
356360

@@ -408,7 +412,7 @@ impl<I: Interner> Ord for TyKind<I> {
408412
(Error(a_e), Error(b_e)) => a_e.cmp(b_e),
409413
(Bool, Bool) | (Char, Char) | (Str, Str) | (Never, Never) => Ordering::Equal,
410414
_ => {
411-
debug_assert!(false, "This branch must be unreachable, maybe the match is missing an arm? self = self = {self:?}, other = {other:?}");
415+
debug_assert!(false, "This branch must be unreachable, maybe the match is missing an arm? self = {self:?}, other = {other:?}");
412416
Ordering::Equal
413417
}
414418
}

0 commit comments

Comments
 (0)