Skip to content
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

GH-89987: Shrink the BINARY_SUBSCR caches #103022

Merged
merged 6 commits into from
Mar 29, 2023

Conversation

brandtbucher
Copy link
Member

@brandtbucher brandtbucher commented Mar 25, 2023

BINARY_SUBSCR has 3 cache entries that are only used by BINARY_SUBSCR_GETITEM (a type version and a function version). However, we don't really need to store all of this information per call-site... we can just keep the function version on the type (next to the cached method) and clear the cached method whenever the type is modified.

Besides the savings in code size, this has a couple of other minor benefits:

  • Any BINARY_SUBSCR_GETITEM site can work with any heap type defining __getitem__, provided it's already been cached at least once somewhere.
  • We can specialize for more than 65k function versions.

Performance is neutral.

@markshannon
Copy link
Member

markshannon commented Mar 28, 2023

I'm a bit concerned about ABA errors.
For example:

  1. Site X is specialized for some version of class C and C.getitem.
  2. Then C.getitem is modified
  3. Site Y is specialized, setting the cached version to the new function.
  4. Now site X runs again.

Is this OK?

I think it is, but it requires a different contract between the specializer, the specialized code and the class's cache, than we had before.

This contract will need to be clearly documented.
Does this seem correct:

  • If the _specialization_cache.getitem field is non-NULL, then the value it holds is what PyType_Lookup(cls, "__getitem__") would return.
  • If _specialization_cache.getitem is NULL, the getitem_version field is meaningless.
  • If the _specialization_cache.getitem field is non-NULL and _specialization_cache.getitem_version == _specialization_cache.getitem->func_version then _specialization_cache.getitem is a Python function that can be called with two positional arguments and no keyword arguments, and has neither *args nor **kwargs.

@brandtbucher
Copy link
Member Author

Yes, that's correct. I'll document this clearly in a comment.

@brandtbucher brandtbucher merged commit 121057a into python:main Mar 29, 2023
warsaw pushed a commit to warsaw/cpython that referenced this pull request Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance or resource usage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants