Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Cache type_object_type()#19514

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ilevkivskyi merged 4 commits intopython:masterfromilevkivskyi:cache-type-obj-type
Jul 27, 2025

Conversation

@ilevkivskyi
Copy link
Member

@ilevkivskyiilevkivskyi commentedJul 26, 2025
edited
Loading

This gives almost 4% performance boost (Python 3.12, compiled). Note there is an old bug intype_object_type(), we treat not ready types asAny without deferring, I disable caching in this case.

Unfortunately, using this in fine-grained mode is tricky, essentially I have three options:

  • Use some horrible hacks to invalidate cache when needed
  • Add (expensive) class target dependency from__init__/__new__
  • Only allow constructor caching during initial load, but disable it in fine-grained increments

I decided to choose the last option. I think it has the best balance complexity/benefits.

@github-actions

This comment has been minimized.

@ilevkivskyi
Copy link
MemberAuthor

Non-trivial results inmypy_primer are surprising. I know there is a bug intype_object_type() that when it is called before the constructor is processed we may get wrong result (e.g. because it is decorated) instead of deferring the current node. But I thought the way I implemented caching should not be affected by this bug. I will take a look today or tomorrow.

@github-actions

This comment has been minimized.

@ilevkivskyi
Copy link
MemberAuthor

Although remaining errors are kind of correct, they are sill weird. It looks like they appear because previously

classC@no_type_checkdef__new__(cls): ...

resulted inAny as the type ofC class object. But somehow it is nowC in case of an import cycle. FWIW I can't reproduce this in a simple test.

@ilevkivskyi
Copy link
MemberAuthor

OK, so it turns out there was an existing bug (inconsistency) with@no_type_check, where the decorated function was notAny for a brief period between semantic analysis and type checking. I simply fix that.

While looking at this I found second place where we don't handle not-ready types properly (decorated overloads), again I simply don't cache in this case, since a proper fix may be non-trivial (I also update the relevant TODO item). I update the PR description to mention this.

@github-actions

This comment has been minimized.

Copy link
Collaborator

@sterliakovsterliakov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

LG! The idea makes sense, and the bench on GHA shows 2.6-2.9% win for selfcheck. Nice! I'm a bit worried that it uncovered at least three semi-unrelated issues - can there be others, not revealed by primer runs? It may be too much fun to debug caching-related bugs later...

else:
builtins_type=named_type("builtins.type")

fallback=info.metaclass_typeorbuiltins_type
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Can we keep it lazy (only callnamed_type ifinfo.metaclass_type is None)?

@ilevkivskyi
Copy link
MemberAuthor

@sterliakov Of course there can be more. But those are all real bugs, we would need to fix them sooner or later anyway. In an ideal bug-free world, we should be able toalways cache.

@github-actions
Copy link
Contributor

According tomypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@ilevkivskyiilevkivskyi merged commita8d2f13 intopython:masterJul 27, 2025
20 checks passed
@ilevkivskyiilevkivskyi deleted the cache-type-obj-type branchJuly 27, 2025 21:49
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@JukkaLJukkaLAwaiting requested review from JukkaL

@hauntsaninjahauntsaninjaAwaiting requested review from hauntsaninja

1 more reviewer

@sterliakovsterliakovsterliakov approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@ilevkivskyi@sterliakov

[8]ページ先頭

©2009-2025 Movatter.jp