Skip to content

Commit

Permalink
Auto merge of #136593 - lukas-code:ty-value-perf, r=oli-obk
Browse files Browse the repository at this point in the history
valtree performance tuning

Summary: This PR makes type checking of code with many type-level constants faster.

After rust-lang/rust#136180 was merged, we observed a small perf regression (rust-lang/rust#136318 (comment)). This happened because that PR introduced additional copies in the fast reject code path for consts, which is very hot for certain crates: /~https://github.com/rust-lang/rust/blob/6c1d960d88dd3755548b3818630acb63fa98187e/compiler/rustc_type_ir/src/fast_reject.rs#L486-L487

This PR improves the performance again by properly interning the valtrees so that copying and comparing them becomes faster. This will become especially useful with `feature(adt_const_params)`, so the fast reject code doesn't have to do a deep compare of the valtrees.

Note that we can't just compare the interned consts themselves in the fast reject, because sometimes `'static` lifetimes in the type are be replaced with inference variables (due to canonicalization) on one side but not the other.

A less invasive alternative that I considered is simply avoiding copies introduced by rust-lang/rust#136180 and comparing the valtrees it in-place (see commit: rust-lang/rust@9e91e50 / perf results: rust-lang/rust#136593 (comment)), however that was still measurably slower than interning.

There are some minor regressions in secondary benchmarks: These happen due to changes in memory allocations and seem acceptable to me. The crates that make heavy use of valtrees show no significant changes in memory usage.
  • Loading branch information
bors committed Feb 13, 2025
2 parents 8b45f4a + db57a5f commit 812566d
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/mir/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,9 @@ The most important rule for
this representation is that every value must be uniquely represented. In other
words: a specific value must only be representable in one specific way. For example: there is only
one way to represent an array of two integers as a `ValTree`:
`ValTree::Branch(&[ValTree::Leaf(first_int), ValTree::Leaf(second_int)])`.
`Branch([Leaf(first_int), Leaf(second_int)])`.
Even though theoretically a `[u32; 2]` could be encoded in a `u64` and thus just be a
`ValTree::Leaf(bits_of_two_u32)`, that is not a legal construction of `ValTree`
`Leaf(bits_of_two_u32)`, that is not a legal construction of `ValTree`
(and is very complex to do, so it is unlikely anyone is tempted to do so).

These rules also mean that some values are not representable. There can be no `union`s in type
Expand Down

0 comments on commit 812566d

Please sign in to comment.