Repository navigation
Conversation
|
@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.
Add a fast path to match exhaustiveness checking.
This comment has been minimized.
This comment has been minimized.
|
Finished benchmarking commit (7fb128a): comparison URL. Overall result: ❌✅ regressions and improvements - please read:Benchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up. Next, please: If you can, justify the regressions found in this try perf run in writing along with @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 3.2%, secondary 0.5%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (primary -2.5%, secondary -2.1%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis perf run didn't have relevant results for this metric. Bootstrap: 460.722s -> 461.446s (0.16%) |
|
Nice! It's not surprising that enum-stress improves, that one is the pathological case, but I'd like to know what's happening with For context, here's a thread on zulip where this was discussed some time ago: https://rust-lang.zulipchat.com/#narrow/channel/131828-t-compiler/topic/derive.28Debug.29.20expansion.20for.20enums.20is.20not.20that.20good/with/618210698 |
The new function `is_obviously_one_to_one()` checks `match`es in O(n) time, provided that they are matching fieldless enums, with exactly one arm per variant and no guards, in exactly the declaration order of the variants. My hope is that this is a pattern common enough, particularly in derive macros, to make this extra mode worthwhile. Inspired by <https://yoyo-code.com/derive-debug-compile-time-improvements/>, linked from This Week in Rust issue 679, which said: > Of course, we should also adress the match checking cost. We can maybe > do something wacky in the derive, but I think the proper way to fix it > is just optimizing the algorithm, because it really doesn't have to be > quadratic in this case. That's easier said than done because the > algorithm is quite complex, but it should be doable. This change is an attempt to get the “doesn’t have to be quadratic” benefits without needing to think hard about optimization. And if the general algorithm ever does get optimized in that way, this will be easy to rip out.
|
The perf results appear to show that this is working more or less as I expected: great improvement in the I suspect that most of the added cost is calling But this might be worth it even as-is? |
|
Some changes occurred in exhaustiveness checking cc @Nadrieril |
|
r? @adwinwhite rustbot has assigned @adwinwhite. Use Why was this reviewer chosen?The reviewer was selected based on:
|
From the cachegrind diff, it looks like that's about 8% of the incrase. The biggest incrase (~40%) is directly in The top of the diff is here, but there's a fair bit of inlining noise
I'd myself like to see this neutral or green on primary benchmarks, unless that proves to be super difficult. We don't want to get into a situation where we regress many real world use cases by a little amount for a rare perf improvement on fairly artificial stress test. |
|
Most ADTs aren't fieldless enums, I'm unconvinced that speeding up that one case is particularly interesting. E.g. I would have wanted such an improvement to affect the |
| } | ||
|
|
||
| // Fast path for matches that pair up 1:1 with a fieldless enum. | ||
| if is_obviously_one_to_one(tycx, arms, &scrut_ty)? { |
There was a problem hiding this comment.
I think I'd probably try gating this behind something like if arms.len() > N in here (I'd try N=16 or something in that ballpark).
There was a problem hiding this comment.
It might even be lower, but the point is to skip the most common false positives that are the 2-3 variants cases like Option, Result, ? operator and things like that.
| let MatchArm { pat: arm_pat, has_guard: false, arm_data: _ } = arm else { | ||
| // The arm has a guard. | ||
| return false; | ||
| }; | ||
| let pat_ctor @ &Constructor::Variant(pat_variant_idx) = arm_pat.ctor() else { | ||
| // The pattern is not the right kind of pattern. | ||
| return false; | ||
| }; |
There was a problem hiding this comment.
I'd also try checking these arm paths before the ctors_for_ty lookup, maybe with some simple probe that checks just first few items and the last one. Last one is often wildcard, so that could help us skip a lot of cases. It's also common to have the last variant be the most complex one, but that's just my intuition.
There was a problem hiding this comment.
Added this check in 6b1159e. Instead of “first and last” I just have it scan the whole sequence, but in reverse. First and last might be a better idea, but I didn’t want to complicate the code further — let's see what happens.
To be fair, that case is motivated by real world issue. I guess more importantly, I think this fast path could also be a starting point that could be extended to simple matches on non-fieldless enums in the future. But I definitely have a feeling that we're silently regressing the common case here. Matches on fieldless enums are not that uncommon, this should have a broader positive impact. [edit] More specifically, I'd expect improvement on |
It is? Your post mentions @theemathas's case of an enum with 10000 variants, which also says that for 1000 variants the compile time is bad but not abysmal. Even an enum with 1000 unit variants does not seem real world to me, is it? |
|
I've seen two or three people run into this problem on discord so far. They were doing something like listing all sprites in a game. |
|
There's an idea I've had for a while to avoid the quadratic behavior; I tested it out here #166656 , looks promising! Working on making a proper implementation now. |
This is the type of use case I am personally interested in — a large set of entities relevant to the application, each of which is identified by a single enum variant. That said, my enums aren’t yet so big that this PR will likely make a significant difference to them. |
|
@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.
Add a fast path to match exhaustiveness checking.
This comment has been minimized.
This comment has been minimized.
|
Finished benchmarking commit (c7e6b2b): comparison URL. Overall result: ❌✅ regressions and improvements - please read:Benchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up. Next, please: If you can, justify the regressions found in this try perf run in writing along with @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 3.7%, secondary -2.0%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (secondary -7.7%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis perf run didn't have relevant results for this metric. Bootstrap: 462.15s -> 472.536s (2.25%) |
|
I made a proper implementation of my idea here: #166678; it solves the problem at its root :) Namely, the one quadratic behavior is that for each enum constructor we've found in the match, we iterate over all the match rows to keep only the matching ones; that PR entirely removes this behavior by tracking matching rows at the same time that we determine which constructors are present in the match. So this works for non-unit variants, for PartialEq derives, basically any big match benefits, at the cost of many small allocations. |
|
I imagine that #166678 will make this PR almost completely pointless if it merges, but I'll keep this one open to check anyway. |
View all comments
The new function
is_obviously_one_to_one()checksmatches in O(n) time, provided that they are matching fieldless enums, with exactly one arm per variant and no guards, in exactly the declaration order of the variants. My hope is that this is a pattern common enough, particularly in derive macros, to make this extra mode worthwhile.Inspired by @panstromek’s blog post https://yoyo-code.com/derive-debug-compile-time-improvements/, linked from This Week in Rust issue 679, which said:
This change is an attempt to get the “doesn’t have to be quadratic” benefits without needing to think hard about optimization. And if the general algorithm ever does get optimized in that way, this will be easy to rip out.