GeezerVison is showing github.com — GH-93533: Shrink the `LOAD_ATTR` caches by brandtbucher · Pull Request #103014 · python/cpython
Conversation
brandtbucher
commented
Mar 24, 2023
•
edited by bedevere-bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
This adds a fixed array of cached methods to the PyTypeObject struct. This approach saves memory if there are ~23x more LOAD_ATTR sites than there are types.
A size of 16 was chosen because:
A flexible buffer was also considered, but that was 1% slower, likely due to the additional indirection and management of the buffer.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
brandtbucher
changed the title
GH-93533: Shrink the LOAD_ATTR caches.
GH-93533: Shrink the LOAD_ATTR caches
Mar 24, 2023
|
So if I'm understanding this correctly, for each class only 16 methods/attributes can be specialised, with everything after just failing? If so 16 seems fairly small, especially for larger libraries/applications. Something like ndarray or flask.Flask for instance has 50+ methods. I could imagine a larger application using different sets of methods in different areas, filling up the cache. That wouldn't be the sort of thing showing up on benchmarks.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
So if I'm understanding this correctly, for each class only 16 methods/attributes can be specialised, with everything after just failing?
Correct (not including instance attributes). I definitely prefer a growable cache to this approach, except that the numbers we have are slower.
I'm also open to using a larger number of entries, like 32 or 64, if others feel similarly.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
I'm also not sure if this causes problems for interpreter isolation. Maybe putting this sort of state on static built-in types is a bad idea, whether it's resizable or not.
CC @ericsnowcurrently
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
(Docs now passing now #103019 is merged, and updating this with main. Thanks for flagging!)
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
markshannon
commented
Mar 25, 2023
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
From a correctness point of view, adding the cache to static classes should be fine. All the attributes of superclasses of static classes must also be static.
If they aren't that's a bug, and this isn't the place to fix it.
No harm in adding some asserts, though.
However, we would like static classes to be const so that they can be properly shared, which would mean that the cache would need to be pre-populated with all attributes of the class and all its superclasses.
The caches would be a bit big, but not that bad: len(object.__dict__ | int.__dict__) == 74; much smaller if we exclude special methods.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Closing because of the various issues outlined above.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
markshannon
Awaiting requested review from markshannon
markshannon is a code owner
Uh oh!
There was an error while loading. Please reload this page.
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|