Skip to content

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

Merged
kumaraditya303 merged 32 commits into
python:mainfrom
kumaraditya303:mrocache
Jul 21, 2026
Merged

gh-145685: per-type method cache implementation#150160
kumaraditya303 merged 32 commits into
python:mainfrom
kumaraditya303:mrocache

Conversation

@kumaraditya303

@kumaraditya303kumaraditya303 commented May 20, 2026

Copy link
Copy Markdown
Contributor

@kumaraditya303kumaraditya303 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-botbedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label May 27, 2026
Comment threadInclude/internal/pycore_typecache.h Outdated

@colesburycolesbury left a comment

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.

A few more comments below (mostly found by Claude). Also:

  • Have you run single-threaded benchmarks to ensure that we haven't introduced any regressions with this?
  • Does the type_lookup benchmark scale as well as the other scaling benchmarks? In other words, should we consider this a partial or full fix for the type cache scaling issues?

Comment threadObjects/typeobject.c
Comment threadObjects/typeobject.c Outdated
Comment threadPython/typecache.c
Comment threadInclude/internal/pycore_typecache.h Outdated
@read-the-docs-community

read-the-docs-communityBot commented Jun 16, 2026

Copy link
Copy Markdown

Comment threadPython/typecache.c
@kumaraditya303

Copy link
Copy Markdown
ContributorAuthor

Have you run single-threaded benchmarks to ensure that we haven't introduced any regressions with this?

Benchmark results show that it is 1% slower on gil enabled build https://github.com/facebookexperimental/free-threading-benchmarking/blob/main/results/bm-20260616-3.16.0a0-4fbec93/bm-20260616-vultr-x86_64-kumaraditya303-mrocache-3.16.0a0-4fbec93-vs-base.md

I think it it is within the error of benchmark machines and not significant.

@markshannon

Copy link
Copy Markdown
Member

I'm very wary of dismissing 1% slower as "within the noise" without some other evidence that there is no slowdown for a change like this.

It also looks like you've missed several of the stats.
How do the stats look for cache hits, misses and collisions?

How does this change memory use?

@colesbury

Copy link
Copy Markdown
Contributor

Results from running locally on my machine (via Claude, GIL-enabled, clang-20, optimized, lto=thin) look good: https://gist.github.com/colesbury/1e5c1eb4ccc1e626696b568d18d8bbc4. Cache misses drop substantially, but the overall effect is small (possibly within the noise margin) because:

  • the MRO cache doesn't matter as much now that we rely heavily on specialization.
  • the MRO hit rate was already high on these benchmarks

A few more suggestions:

  • We should drop the type_cache_collisions stat because it's not longer tracked and doesn't make sense in the same way. Previously a collision in the MRO cache would overwrite the previous cache entry; now we do linear probing.
  • We should track the number of times we resize/clear cache a type's cache

@kumaraditya303

Copy link
Copy Markdown
ContributorAuthor

We should track the number of times we resize/clear cache a type's cache

I've added stats for resizes and invalidations and removed collisions.

# Conflicts:
#	Lib/test/test_free_threading/test_type.py
@kumaraditya303

kumaraditya303 commented Jul 15, 2026

Copy link
Copy Markdown
ContributorAuthor

@markshannon Do you have more comments/concerns on this?

@dpdani

Copy link
Copy Markdown
Contributor

I would still like to see the top comment "Lock-free per type method cache implementation" rephrased since it's not a lock free implementation.

@kumaraditya303

Copy link
Copy Markdown
ContributorAuthor

I would still like to see the top comment "Lock-free per type method cache implementation" rephrased since it's not a lock free implementation.

How would you rephrase it, do you have a suggestion?

@dpdani

Copy link
Copy Markdown
Contributor

How about just "Per-type method cache implementation"?

@kumaraditya303
kumaraditya303 merged commit daf09e1 into python:mainJul 21, 2026
71 of 72 checks passed
@kumaraditya303
kumaraditya303 deleted the mrocache branch July 21, 2026 08:41
Sign up for freeto 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.

6 participants

@kumaraditya303@bedevere-bot@markshannon@colesbury@dpdani@zooba