Repository navigation
match: Use an aggregate equality comparison for constant array/slice patterns - #155216
jakubadamw wants to merge 19 commits into
Conversation
|
Some changes occurred in match lowering cc @Nadrieril |
|
rustbot has assigned @JonathanBrouwer. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
|
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
match: Use an aggregate equality comparison for constant array/slice patterns when possible
This comment has been minimized.
This comment has been minimized.
|
Finished benchmarking commit (5a6dba6): comparison URL. Overall result: ❌✅ regressions and improvements - no action neededBenchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf. @bors rollup=never Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
Max RSS (memory usage)Results (primary -0.0%, secondary 0.5%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (primary -2.3%, secondary 14.4%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeResults (primary -0.1%, secondary 0.1%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Bootstrap: 491.114s -> 490.988s (-0.03%) |
|
@rustbot reroll |
| if let PatKind::Constant { value } = pat.kind { | ||
| Some(ty::Const::new_value(tcx, value.valtree, value.ty)) | ||
| } else { | ||
| None | ||
| } |
There was a problem hiding this comment.
It might also be worth reconstructing aggregate constants for arrays/slices of arrays of constants, etc.? I'm not a specialization expert, but it looks like arrays of bytewise-comparable things are also bytewise-comparable, at least for common array lengths1. Since array and slice equality are specialized based on their element types' bytewise-comparability, we should be able to get better codegen for nested array patterns too (as long as the inner arrays are of one of those common lengths), I think?
Footnotes
There was a problem hiding this comment.
@dianne, interesting. I’ll look into this next! 🙂
| // When there is no `..`, all elements are constants, and | ||
| // there are at least two of them, collapse the individual | ||
| // element subpairs into a single aggregate comparison that | ||
| // is performed after the length check. | ||
| if slice.is_none() |
There was a problem hiding this comment.
An additional possibility: even if there is a .., the comparisons for the sub-slices before and after the .. could be done via aggregate equality when applicable. Credit to #121540, which I think did this?
Edit: assuming prefixes and suffixes are typically small and hand-written, it's probably not worth the trouble to use aggregate equality for them.
Even if only handling the case with no .., it might be worth moving the special-casing into prefix_slice_suffix to share it between PatKind::Slice and PatKind::Array, since that's where the commonalities live.
Edit: after prefix_slice_suffix's cleanup in #154943, I don't think it makes much sense to put this in there. I still think the logic for deciding whether to use aggregate equality is complex enough that it could be worth factoring out, but that's probably not the way to do it.
This comment has been minimized.
This comment has been minimized.
|
@dianne do you want to take over review here? |
There was a problem hiding this comment.
I don't have much context on the ctfe or const traits side of things to evaluate the approach. cc @oli-obk maybe? It feels like we have to make some sort of compromise here to avoid calling anything const-unstable. Possibly we could block on const_cmp stabilizing, or possibly we could have some workaround until then if we want to land this first.
I think I can handle the technical review, at least. Tentatively, r? me (though feel free to steal the assignment if that'd be easier ^^)
…n't get captured by this logic
…ng it Following the review feedback, `const_to_pat()` now records the original constant value on the array and slice pattern nodes it expands, and match lowering reads that value back instead of attempting to reconstruct an aggregate constant from the individual element subpatterns. This has two consequences. First, hand-written array and slice patterns are no longer collapsed into aggregate comparisons; the user's intent to match element by element is respected before the MIR boundary. Only patterns that were expanded from an actual constant (a named constant or a byte-string literal) use the aggregate `PartialEq::eq` comparison, and for those the semantics of matching against a constant and comparing with `PartialEq::eq` coincide. Second, nested constant arrays now benefit from the aggregate comparison as well, since the recorded value covers the whole constant, whereas the reconstruction required every immediate element of the pattern to be a leaf constant.
Give the `PartialEq::eq` call emitted for constant array/slice patterns `UnwindAction::Unreachable` instead of an unwind edge. The built-in `PartialEq` implementations for arrays and slices can be trusted not to panic, and the unwind edge would not be harmless: since the aggregate comparison replaces a series of `SwitchInt` tests that could never unwind, the extra edge would make borrow-checking stricter about the drop order in unwinding code, turning previously accepted programs into errors. The string equality tests keep their unwind edge, as they have always had one.
- Drop the extra length test for constant slices; `PartialEq::eq` already checks the length. - Make an expanded constant on a non-array, non-slice pattern a bug. - Add a FIXME to lift the const-context restriction with `const_cmp`. - Justify keeping `AGGREGATE_EQ_MIN_LEN` in its doc comment. - Note in `core` that the bytewise `PartialEq` specialisations must not panic, and test that matches resolve to them.
Call the `raw_eq` and `compare_bytes` intrinsics directly instead of `PartialEq::eq`. Add a MIR test for a constant below `AGGREGATE_EQ_MIN_LEN`. Remove the codegen-units test and the comments in `core`.
e0ef1aa to
b7e5b00
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
- Nest the aggregate slice comparison under a slice length test, so that patterns of the same length share one length check. - Move the array and slice aggregate comparisons into their own helpers. - Assert that the intrinsics called for aggregate comparisons cannot unwind. - Correct the comments on which intrinsic is not const-stable. - Check in the MIR test that the scrutinee is not indexed, including before the first `raw_eq`. - Check that the fallback `switchInt`s index into the scrutinee. - Add MIR tests for a short slice, a multi-arm slice match and wider slice elements. - Describe the length threshold in the MIR test without naming the constant. - Add a `#![no_core]` MIR test for crates without the intrinsic lang items. - Add run-time tests for multi-arm and wider-element slice matches.
b7e5b00 to
2e0b6a0
Compare
|
Never hurts! @bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
match: Use an aggregate equality comparison for constant array/slice patterns
This comment has been minimized.
This comment has been minimized.
|
Finished benchmarking commit (4c09b15): comparison URL. Overall result: ✅ improvements - no action neededBenchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up. @bors rollup=iffy rustc-perf Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
Max RSS (memory usage)Results (primary -6.1%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (primary 2.5%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeResults (primary -0.2%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Bootstrap: 484.651s -> 482.321s (-0.48%) |
View all comments
When every element in an array or slice pattern is a constant and there is no
..subpattern, the match builder will now emit a single call toPartialEq::eqinstead of comparing each element from the value one by one against the respective constant in the pattern.This drastically reduces the number of MIR basic blocks for large constant-array matches – e.g. a 64-element
[u8; 64]match previously generated 64 separate comparison blocks and now generates just onePartialEq::eqcall that LLVM can lower to amemcmp(). The optimisation is gated on having at least two constant elements, meaning single-element arrays will still use a plain scalar comparison.Example:
Closes #103073.
Closes #110870.