Skip to content

exhaustiveness: filter matrix rows while splitting ctors - #166678

Open
Nadrieril wants to merge 7 commits into
rust-lang:mainfrom
Nadrieril:pre-filter-matrix-rows
Open

Nadrieril wants to merge 7 commits into
rust-lang:mainfrom
Nadrieril:pre-filter-matrix-rows

Conversation

@Nadrieril

@Nadrieril Nadrieril commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

That's an idea I'd had for a long time but never thought would be worth it. Then #166637 came along, with a reminder that yes people do write immense match statements.

Before this PR, once we've figured out a minimal set of constructors to use to analyze the matrix, we'd iterate through the whole matrix to find which rows correspond to the current constructor. That's somewhat redundant with the splitting analysis that already went through all the row constructors. This PR combines the two in one: now constructor splitting also returns the filtered row indices for each split constructor.

This removes the by-construction quadratic behavior of exhaustiveness for mostly disjoint arms. I did leave quadratic behavior for slice patterns and integer ranges for simplicity, which we can fix later if we want.

r? dianne

LLM disclosure: handcrafted with love. No LLMs were involved at any stage in the making of this PR. I did use an LLM to throw down #166656 quickly with the basic idea to see if perf would be worth the effort. No code or idea from that PR made it into this one that didn't already come from me.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 27, 2026
@Nadrieril

Copy link
Copy Markdown
Member Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 27, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Nov 27, 2026
exhaustiveness: filter matrix rows while splitting ctors
@rust-log-analyzer

This comment has been minimized.

@rust-bors

rust-bors Bot commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 70023ca (70023ca1f68593eb7d7ae1295acb6778cf008f61)
Base parent: b61bfe5 (b61bfe5f22374c5570299aee056c23ad9acc81ff)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown

Finished benchmarking commit (70023ca): comparison URL.

Overall result: ❌✅ regressions and improvements - please read:

Benchmarking 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.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@bors rollup=never rustc-perf
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
1.9% [1.9%, 1.9%] 6
Improvements ✅
(primary)
-1.6% [-5.3%, -0.1%] 18
Improvements ✅
(secondary)
-7.7% [-69.8%, -0.1%] 54
All ❌✅ (primary) -1.6% [-5.3%, -0.1%] 18

Max RSS (memory usage)

Results (primary 2.8%, secondary 0.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.8% [1.8%, 3.7%] 3
Regressions ❌
(secondary)
1.2% [0.5%, 2.3%] 6
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-4.7% [-4.7%, -4.7%] 1
All ❌✅ (primary) 2.8% [1.8%, 3.7%] 3

Cycles

Results (primary 1.1%, secondary -9.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
4.4% [4.4%, 4.4%] 1
Regressions ❌
(secondary)
4.0% [1.1%, 8.9%] 16
Improvements ✅
(primary)
-2.1% [-2.1%, -2.1%] 1
Improvements ✅
(secondary)
-29.2% [-57.3%, -0.5%] 11
All ❌✅ (primary) 1.1% [-2.1%, 4.4%] 2

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 462.15s -> 461.074s (-0.23%)
Artifact size: 405.59 MiB -> 405.79 MiB (0.05%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Nov 28, 2026
@Nadrieril
Nadrieril marked this pull request as ready for review November 28, 2026 08:30
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 28, 2026
@Nadrieril

Nadrieril commented Nov 28, 2026 •

Copy link
Copy Markdown
Member Author

I asked an LLM to investigate the one perf regression; it seems entirely unrelated to this PR.

Namely: the regression is fully explained by the issue_159933.rs file which has no matches, and is in the typeck_root self-profile category. Looks like weird optimizer side-effects (reproducible tho, because #166656 had it too). Also to note that this regression is visible on x86 but not aarch64.

@panstromek

Copy link
Copy Markdown
Contributor

Yes, I looked at the cachegrind diff locally and I think the regression unrelated. next-solver-stress is a new benchmark and there seems to be some spiky noise pattern in FnCtxt that we have seen a few times, but haven't investigated much yet. This one is slightly different but still unrelated to this PR.

@rust-log-analyzer

This comment has been minimized.

@Nadrieril
Nadrieril force-pushed the pre-filter-matrix-rows branch from d6c3914 to 5a37fb2 Compare November 28, 2026 09:02
@panstromek

Copy link
Copy Markdown
Contributor

Also, some of the doc and doc-json results are the dreaded get_module_children noise (e.g. helloworld doesn't contain any matches, so a green result doesn't make much sense). This one disappeared once but it looks like it's back.

@theemathas

Copy link
Copy Markdown
Contributor

I had an LLM look at this, and it found the following issues (although I'm unsure how important they are). It also found that this PR fixes a previously-unknown ICE: #166738.

1. unreachable_patterns lint false negative.

The following code doesn't compile on nightly, but compiles with this PR. That is, with this PR, the lint doesn't detect the unreachable pattern (the second true), even though it should.

#![deny(unreachable_patterns)]

const NIL: *const u8 = std::ptr::null();

fn main() {
    let x = (true, NIL);
    match x {
        (true | true, NIL) => (),
        _ => (),
    }
}

2. overlapping_range_endpoints lint false negative.

Similarly to the above case, the following code doesn't compile on nightly, but compiles with this PR.

#![deny(overlapping_range_endpoints)]

const NIL: *const u8 = std::ptr::null();

fn main() {
    let x = (0, NIL);
    match x {
        (0..=1 | 1..=2, NIL) => (),
        _ => (),
    }
}

3. Stack overflow when checking a large array pattern

The following python script generates rust source code that compiles on nightly, but causes a SIGSEGV in rustc due to a stack overflow with this PR. The rust source code uses a match to check whether a value of type [u8; 12000] matches a large pattern containing all zeroes.

print(
    """fn main() {
    let x = [0_u8; 12000];
    match x {
        [""",
    end="",
)
for _ in range(12000):
    print("0, ", end="")
print("""] => {}
        _ => {}
    }
}""")

Part of the generated code:

fn main() {
    let x = [0_u8; 12000];
    match x {
        [0, 0, 0, /* omitted... */, 0] => {}
        _ => {}
    }
}

4. Performance regression with the non_exhaustive_omitted_patterns lint.

Note that this non_exhaustive_omitted_patterns lint is currently unstable.

The following python script generates rust source code that compiles much slower with this PR compared to on nightly. On my computer, a cargo check takes 0.88s on nightly, but takes 3.71s with this PR.

print(
    """#![feature(non_exhaustive_omitted_patterns_lint)]
#![warn(non_exhaustive_omitted_patterns)]

fn main() {
    let x = (0_i32, 0.0_f64);
    match x {"""
)
for i in range(13000):
    print(f"        ({i}_i32, 1.0_f64) => {{}}")
print("""        _ => {}
    }
}""")

Part of the generated code:

#![feature(non_exhaustive_omitted_patterns_lint)]
#![warn(non_exhaustive_omitted_patterns)]

fn main() {
    let x = (0_i32, 0.0_f64);
    match x {
        (0_i32, 1.0_f64) => {}
        (1_i32, 1.0_f64) => {}
        (2_i32, 1.0_f64) => {}
// omitted...
        (12997_i32, 1.0_f64) => {}
        (12998_i32, 1.0_f64) => {}
        (12999_i32, 1.0_f64) => {}
        _ => {}
    }
}

@Nadrieril

Copy link
Copy Markdown
Member Author

Huh ok, so the ids of opaques was useful after all 🤔 That's 1 and 2. 3 I'll ignore because that's just a difference in stack usage (the logic was recursive and still is, nothing changed here). Can you try case 4 with #166703 to see if the regression is still there?

@theemathas

Copy link
Copy Markdown
Contributor

@Nadrieril Case 4 still has a performance regression with #166703. A cargo check takes 0.99s on nightly, but 3.23s on that PR.

@rustbot

rustbot commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

These commits modify the Cargo.lock file. Unintentional changes to Cargo.lock can be introduced when switching branches and rebasing PRs.

If this was unintentional then you should revert the changes before this PR is merged.
Otherwise, you can ignore this comment.

@rust-log-analyzer

This comment has been minimized.

@Nadrieril

Copy link
Copy Markdown
Member Author

Oh, for 4 the regression is actually because of the floats! That test is cleverly engineered so that normal exhaustiveness doesn't hit a float quadratic case, but the lint now does. I'll ignore: the lint is unstable, and this would require someone figuring out a way to partition float ranges into disjoint subranges which I don't want to attempt.

@Nadrieril
Nadrieril force-pushed the pre-filter-matrix-rows branch from 75ece38 to f1f7112 Compare November 29, 2026 08:39
@rust-log-analyzer

This comment has been minimized.

@Nadrieril
Nadrieril force-pushed the pre-filter-matrix-rows branch from f1f7112 to 780fc22 Compare November 29, 2026 17:08
@rust-log-analyzer

This comment has been minimized.

@Nadrieril
Nadrieril force-pushed the pre-filter-matrix-rows branch from 780fc22 to 725257e Compare November 29, 2026 19:18

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

perf-regression Performance regression. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants