-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Optimize integral pattern matching #65089
Optimize integral pattern matching #65089
Conversation
This reduces the number of call sites for `constructor_intersects_pattern()` from two to one, which the next commit will take advantage of.
This is a 2% instruction count win on `unicode_normalization-check-clean`.
This commit moves a lot of code around but doesn't change functionality at all. The main goal was for `from_pat()` to no longer call `from_ctor()`. The increase in inlining resulted in less function call overhead, for a 3% instruction count win on `unicode_normalization-check-clean`.
The `if let Some(val) = value.try_eval_bits(...)` branch in `from_const()` is very hot for the `unicode_normalization` benchmark. This commit introduces a special-case alternative for scalars that avoids `try_eval_bits()` and all the functions it calls (`Const::eval()`, `ConstValue::try_to_bits()`, `ConstValue::try_to_scalar()`, and `Scalar::to_bits()`), instead extracting the result immediately. The type and value checking done by `Scalar::to_bits()` is replicated by moving it into a new function `Scalar::check_raw()` and using that new function in the special case. PR rust-lang#64673 introduced some special-case handling of scalar types in `Const::try_eval_bits()`. This handling is now moved out of that function into the new `IntRange::integral_size_and_signed_bias` function. This commit reduces the instruction count for `unicode_normalization-check-clean` by about 10%.
`filter_map()` is less general, but more efficient, and has the same effect in this case. This commit reduces the instruction count for `unicode_normalization-check-clean` by about 2%.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes look good to me; they're all just shuffling code around so we can probably land this without additional review, but cc @oli-obk
r? @varkor |
oops; r? @Mark-Simulacrum but cc @varkor :) |
@bors r+ rollup=never since perf |
📌 Commit 2a3a544 has been approved by |
…, r=Mark-Simulacrum Optimize integral pattern matching Various improvements to integral pattern matching. Together they reduce instruction counts for `unicode_normalization-check-clean` by about 16%. r? @Mark-Simulacrum
☀️ Test successful - checks-azure |
Integer::from_attr(&tcx, UnsignedInt(uty)).size() | ||
} | ||
_ => tcx.layout_of(param_env.with_reveal_all().and(ty)).ok()?.size, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A quick grep shows a few more sites that call layout_of
just to get the size or alignment (and there's likely more but split across multiple lines). So it might still be worth having size_and_align_of(Ty<'tcx>)
somewhere with an optimization like this -- maybe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yea, this could just be a method on TyCtxt
This reverses changes from rust-lang#65089, but I need this temporarily for rebase to be tractable.
Various improvements to integral pattern matching. Together they reduce instruction counts for
unicode_normalization-check-clean
by about 16%.r? @Mark-Simulacrum