Repository navigation
Perf - Separate flag computation & intern functions per TyKind - #165396
Conversation
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.
Perf - InternedSet per `TyKind`
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Finished benchmarking commit (d3789d8): comparison URL. Overall result: ✅ 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 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.0%, secondary 1.0%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (primary -2.7%, secondary -1.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: 482.475s -> 481.535s (-0.19%) |
There was a problem hiding this comment.
My understanding of the LLM policy is that you are supposed to organize a reviewer in advance who is willing to review LLM-generated code, not dump it on a reviewer after talking with someone else. Nevertheless I will review this.
You mentioned boilerplate, which is a problem. That's one of the things about LLM-generated code -- you can end up with code you'd be unlikely to write as a human because it's so repetitive. Which means it's also not very nice to read.
There is also a bigger problem: some of the TyKinds only have a single value: Bool, Char, Str, Never, Error. It's silly to have a set for these variants, which will only have zero or one elements. Furthermore, some of the others have a fixed number of possible values: Int, Uint, Float. That's why values of all of these TyKinds are already pre-interned in CommonTypes.
Another problem is that some kinds are very common (e.g. Adt) while others are very rare (e.g. CoroutineWitness). I wonder if it's possible to focus on a subset of the variants to get the lion's share of the improvement with much less boilerplate.
It feels like there is a germ of an interesting idea here, but the current expression of it needs work.
|
I've made a bit of a shambles of explaining myself 🤦. My initial commit in this PR was not LLM generated. But does bare similarities to a draft which I did use an LLM for, so if something here looks terrible, that's down to my own skill issues 🙃. Thank you for the review. I think it's the declarative nature of the code with a lack of runtime branching along with the separate I've made a I'm pretty sure the method |
|
@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.
Perf - InternedSet per `TyKind`
This comment has been minimized.
This comment has been minimized.
|
Finished benchmarking commit (613b3d3): 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 @bors rollup=never 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 0.2%, secondary 1.5%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (primary 2.9%, secondary -0.6%)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: 480.462s -> 478.824s (-0.34%) |
|
@bors try @rust-timer queue |
|
I've simplified the idea as I've entwined two separate ideas
The latest commit, 1eaf80d, uses the original set up for the With this in mind. @rustbot ready |
TyKindTyKind
This comment has been minimized.
This comment has been minimized.
1eaf80d to
ebc1140
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. |
5ab23a1 to
8f775ee
Compare
| self.add_flags(TypeFlags::HAS_CANONICAL_BOUND); | ||
| } | ||
| pub fn for_empty_flags() -> Self { | ||
| Self { flags: TypeFlags::empty(), ..Self::new() } |
There was a problem hiding this comment.
This is equivalent to FlagComputation::new. We don't need both methods.
| // Avoid this in favour of more specific `Ty::new_*` methods, where possible. | ||
| // FIXME(Jamesbarford) Avoid this in favour of more specific `Ty::new_*` | ||
| // methods, where possible. It will also possibly be faster as it will avoid | ||
| // this match statement. |
There was a problem hiding this comment.
I'm confused by this comment. The first sentence was already present. What is the FIXME referring to? Are you planning a follow-up?
There was a problem hiding this comment.
Yes, how I got looking at this area was from an Alias refactor issue. Where the last point on the list is;
replace uses of
tcx.mk_ty_from_kindwith calls to a more specific constructor method
I've had an initial scan through the codebase and I don't think it will be possible to completely remove the function. I do intend to have a follow up PR is going to be minimise the use of mk_ty_from_kind and then update the comment again from "avoid using" to "is used in XYZ scenarios".
There was a problem hiding this comment.
Ok, for now can you change it to something like this:
Avoid this in favour of more specific `Ty::new_*` methods where possible. They will be faster because they avoid this match statement.
FIXME(Jamesbarford): Minimise usage.
|
@rustbot author |
|
Reminder, once the PR becomes ready for a review, use |
8f775ee to
7eb12bd
Compare
7eb12bd to
e2cd888
Compare
|
@bors r+ |
|
Creating a perf-sensitive rollup |
…uwer Rollup of 4 perf-sensitive pull requests Successful merges: - #165396 (Perf - Separate flag computation & intern functions per `TyKind`) - #165774 (Auto trait finder: Avoid recomputing `ParamEnv` if nothing changed) - #165863 (Avoid instantiating `FnSig` when not necessary) - #166084 (Avoid unnecessary LLVM passes when thin LTO buffer is null)
Rollup merge of #165396 - Jamesbarford:perf/intern-type-kind, r=nnethercote Perf - Separate flag computation & intern functions per `TyKind` Create a separate function for interning each `TyKind` and an individual flags computation function. Doing this means that we can avoid a match statement that previously existed when computing flags which improves perf; #165396 (comment) Separately there might be some wins with splitting out the `InternedSet`'s (see 'Original Description' below) however, in my view, there is enough of a benefit with this relatively simple change that modifying the `InternedSet`'s could be a separate exploration. **Edit:** My original write up for the PR is below. However what this PR has become and what the original intention was has drifted sufficiently that writing a new description made the most sense to me. <details> <summary> Original Description</summary> Create an `InternedSet` per `TyKind` variant; this is something I discussed with @lqd & @lcnr separately, both thought it was a reasonable idea. I believe the speedup comes from no longer having to check the `TyKind` on every call to `intern_ty(...)` when computing flags. This saves several function calls and branches. As such I also had to create separate `flags()` functions to get the flags for a `TyKind` so we don't have to do a match on the kind. ### LLM usage; TLDR; I prototyped a PR as I knew a lot of the implementation would be boilerplate, ran perf to see if it was worth following up on, @lqd & @lcnr thought it looked promising. Hence I've now opened this PR which is my work borrowing parts from the prototype. **Longer version** The idea is mine and I discussed it with @lcnr before doing any work, we also decided using an LLM to explore if the idea was worth pursing seemed reasonable. I used an LLM to completely vibe code the [first commit of a prototype-y PR](1b6b590) to verify the idea, which I then reviewed, and subsequently [ran perf, things looked decent](#165136 (comment)). The other commits in that prototype PR were me, not an LLM, trying to effectively create the below idea; ```rust fn compute_flags<const K: ty::TyKind<'tcx>() { const match K.kind() { // } } ``` Which I subsequently abandoned as I couldn't get it to work even when turning on a bunch of experimental features. I appreciate that might be me doing things wrong. I tried this as to me this idea seems to scream for compile-time evaluation as we can know the `TyKind` at compile time. This PR has been written by me borrowing bits from my original LLM/James collaborative prototype. </details> r? @nnethercote
|
Note This PR was benchmarked as part of triage of its containing rollup: triage URL. Finished benchmarking commit (8bd646e): comparison URL. Overall result: ✅ improvements - no action needed@rustbot label: -perf-regression 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 -1.3%, secondary -0.6%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (secondary -1.3%)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. Artifact size: 405.23 MiB -> 405.31 MiB (0.02%) |
Create a separate function for interning each
TyKindand an individual flags computation function. Doing this means that we can avoid a match statement that previously existed when computing flags which improves perf; #165396 (comment)Separately there might be some wins with splitting out the
InternedSet's (see 'Original Description' below) however, in my view, there is enough of a benefit with this relatively simple change that modifying theInternedSet's could be a separate exploration.Edit:
My original write up for the PR is below. However what this PR has become and what the original intention was has drifted sufficiently that writing a new description made the most sense to me.
Original Description
View all comments
Create an
InternedSetperTyKindvariant; this is something I discussed with @lqd & @lcnr separately, both thought it was a reasonable idea.I believe the speedup comes from no longer having to check the
TyKindon every call tointern_ty(...)when computing flags. This saves several function calls and branches. As such I also had to create separateflags()functions to get the flags for aTyKindso we don't have to do a match on the kind.LLM usage;
TLDR; I prototyped a PR as I knew a lot of the implementation would be boilerplate, ran perf to see if it was worth following up on, @lqd & @lcnr thought it looked promising. Hence I've now opened this PR which is my work borrowing parts from the prototype.
Longer version
The idea is mine and I discussed it with @lcnr before doing any work, we also decided using an LLM to explore if the idea was worth pursing seemed reasonable. I used an LLM to completely vibe code the first commit of a prototype-y PR to verify the idea, which I then reviewed, and subsequently ran perf, things looked decent.
The other commits in that prototype PR were me, not an LLM, trying to effectively create the below idea;
Which I subsequently abandoned as I couldn't get it to work even when turning on a bunch of experimental features. I appreciate that might be me doing things wrong. I tried this as to me this idea seems to scream for compile-time evaluation as we can know the
TyKindat compile time. This PR has been written by me borrowing bits from my original LLM/James collaborative prototype.r? @nnethercote