-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Make NominalInstanceType internally wrap an enum
#19811
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
Conversation
055747d to
f010554
Compare
Diagnostic diff on typing conformance testsNo changes detected when running ty on typing conformance tests ✅ |
|
CodSpeed WallTime Performance ReportMerging #19811 will improve performances by 63.76%Comparing Summary
Benchmarks breakdown
|
CodSpeed Instrumentation Performance ReportMerging #19811 will improve performances by 6.26%Comparing Summary
Benchmarks breakdown
|
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
00b10f8 to
0fc1a93
Compare
25a225c to
899fb87
Compare
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
b16f9af to
e8ceb16
Compare
a24e949 to
df1357f
Compare
e8ceb16 to
ce7ee42
Compare
|
Well... there's some bad news here. I just noticed that mypy_primer runs seem to be taking a very long time on this PR branch. It looks like it takes 10 minutes to type-check static-frame with this PR branch in mypy_primer, whereas it takes around 1 minute to type-check static-frame with #19669. I can reproduce a severe slowdown locally on this branch for that repo too. No idea why that would be yet. |
|
Okay, fbcb8e3 fixes the Edit: and it fixed it in CI as well -- mypy_primer is back to completing in ~3m! |
This comment was marked as outdated.
This comment was marked as outdated.
df1357f to
1dc9a1b
Compare
fbcb8e3 to
c84f961
Compare
This reverts commit 252a603.
677d378 to
3436d5d
Compare
8c00cdb to
3b39238
Compare
| // N.B. If this method is not Salsa-tracked, we take 10 minutes to check | ||
| // `static-frame` as part of a mypy_primer run! This is because it's called | ||
| // from `NominalInstanceType::class()`, which is a very hot method. | ||
| #[salsa::tracked] |
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.
See #19669 (comment)
| /// If this is an instance type where the class has a tuple spec, returns the tuple spec. | ||
| /// | ||
| /// I.e., for the type `tuple[int, str]`, this will return the tuple spec `[int, str]`. | ||
| /// For a subclass of `tuple[int, str]`, it will return the same tuple spec. | ||
| pub(super) fn tuple_spec(&self, db: &'db dyn Db) -> Option<Cow<'db, TupleSpec<'db>>> { |
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.
I moved this method from class.rs to instance.rs so that we could avoid having to materialize the ClassType if the instance type was an ExactTuple variant internally.
Now that I've added caching to TupleType::to_class_type(), this might not be a particularly significant optimisation anymore. But with our current callsites across the repo, we only ever need to know the tuple spec of instances, never the tuple spec of Type::GenericAliases. So there doesn't appear to be a huge amount of downside to making this a method on NominalInstanceType rather than a method on ClassType.
slice_literal could also possibly be made a method on NominalInstanceType rather than ClassType, but it doesn't seem to have any performance benefits from what I can tell locally, and it would make this PR's diff bigger.
| .apply_specialization(db, |_| generic_context.specialize_tuple(db, self)), | ||
| ), | ||
| }) | ||
| .expect("Typeshed should always have a `tuple` class in `builtins.pyi`"); |
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.
This .expect() call means we now panic if we're given a custom typeshed without a tuple class. I don't love this but I also think it's okay -- there are already classes where we panic if they're not available in typeshed, such as object and types.ModuleType. tuple is similarly special; I think it's okay if we effectively mandate that custom typesheds have to have a tuple class in them.
Doing this refactor without the .expect() call here would be quite a bit harder, I think.
3436d5d to
da3bc43
Compare
|
I've merged the changes here directly into #19669 |
Stacked on top of #19669; review that PR first!
Summary
This implements @carljm's suggestion from #19669 (comment), which appears to buy back almost all the performance and memory regressions from #19669. If we like this approach, I'll merge this PR into that branch shortly before merging that PR. (I'm keeping them as separate PRs for now, for ease of review.)
Test Plan
All existing tests pass. I don't believe this has any impact on semantics. There is still a small primer report on this PR, but it's now a bit of a mystery to me: I can't reproduce any of the "diagnostics going away" on the target branch of this PR, and I can reproduce all of the "diagnostics being added" on the target branch of this PR. I've seen slightly confusing results from mypy_primer in the past for stacked PRs; I suspect there's a subtle bug in the workflow somewhere that leads to it reporting a diagnostic diff even when there isn't one in cases like this.