Skip to content

gh-145685: per-type method cache implementation#150160

Open
kumaraditya303 wants to merge 18 commits into
python:mainfrom
kumaraditya303:mrocache
Open

gh-145685: per-type method cache implementation#150160
kumaraditya303 wants to merge 18 commits into
python:mainfrom
kumaraditya303:mrocache

Conversation

@kumaraditya303
Copy link
Copy Markdown
Contributor

@kumaraditya303 kumaraditya303 commented May 20, 2026

@kumaraditya303 kumaraditya303 added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label May 27, 2026
@bedevere-bot
Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @kumaraditya303 for commit 728021c 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F150160%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label May 27, 2026
Comment thread Python/typecache.c
}
else {
// double the cache size when resizing
new_size = old_size * 2;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we should have some (fairly large) upper bound on the per-type cache size. Maybe 65536?

Comment thread Python/typecache.c
}

static struct type_cache *
cache_allocate(uint32_t size)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should the cache be freed in type_dealloc?

Comment thread Python/typecache.c Outdated
static inline void
cache_set(PyTypeObject *type, struct type_cache *cache)
{
FT_ATOMIC_STORE_PTR(*cache_slot(type), cache);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this can be FT_ATOMIC_STORE_PTR_RELEASE

Comment thread Python/typecache.c Outdated
Comment thread Include/internal/pycore_typecache.h Outdated
uint32_t version_tag; // initialized from type->tp_version_tag
uint32_t available; // number of available entries in hashtable
uint32_t used; // number of used entries in hashtable
struct type_cache_entry hashtable[1]; // hashtable entries, the total size is always power of 2 and at least _Py_TYPECACHE_MINSIZE
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think the preference now is to use C99 flexible array members (e.g., struct type_cache_entry hashtable[]) in internal-only headers, especially if it's not include transitively by one of the commonly used headers. You may need to adjust the allocation size logic if you change this.

Comment thread Python/typecache.c Outdated
assert(cache->available > 0);
}
cache_insert(cache, name, value);
FT_ATOMIC_STORE_UINT_RELAXED(cache->version_tag, FT_ATOMIC_LOAD_UINT_RELAXED(type->tp_version_tag));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Some of the cache->version_tag and type->tp_version_tag handling doesn't make sense to me:

  • First, I think having a version_tag on the cache make sense. We still need the version tag in some places and getting it from the cache (instead of the type) is good
  • cache->version_tag should be immutable for the lifetime of the cache. tp_version_tag only changes after it gets reset to zero, which also clears the type's cache, so I don't see any case where cache->version_tag can reasonably change on a given cache.

@JelleZijlstra JelleZijlstra removed their request for review June 3, 2026 12:48
@kumaraditya303 kumaraditya303 requested a review from itamaro as a code owner June 4, 2026 11:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants