GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-131798: constant fold classmethod and staticmethod in JIT by kumaraditya303 · Pull Request #148331 · python/cpython

Conversation

@kumaraditya303

@kumaraditya303 kumaraditya303 commented Apr 10, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor
  • Issue: Better uop coverage in the JIT optimizer #131798

Script used for benchmark:

"""Benchmark for classmethod/staticmethod JIT optimization."""

import time

LOOPS = 10_000_000


class MyClass:
    @classmethod
    def class_method(cls):
        return cls

    @staticmethod
    def static_method():
        return 42

    def regular_method(self):
        return self


obj = MyClass()


def bench_classmethod():
    o = obj
    for _ in range(LOOPS):
        o.class_method()
        o.class_method()
        o.class_method()
        o.class_method()
        o.class_method()


def bench_staticmethod():
    o = obj
    for _ in range(LOOPS):
        o.static_method()
        o.static_method()
        o.static_method()
        o.static_method()
        o.static_method()


benchmarks = [
    ("classmethod", bench_classmethod),
    ("staticmethod", bench_staticmethod),
]

for name, func in benchmarks:
    t0 = time.perf_counter()
    func()
    dt = time.perf_counter() - t0
    print(f"{name:20s} {dt:.3f}s")
Benchmark main PR Speedup
classmethod 1.000s 0.572s 1.75x faster
staticmethod 0.860s 0.482s 1.78x faster

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

stonebig, Fidget-Spinner, Sacul0457, and Object905 reacted with heart emoji
All reactions
  • ❤️ 4 reactions
@kumaraditya303
constant fold classmethod and staticmethod in JIT
e9a08b0
@kumaraditya303 kumaraditya303 added the skip news label Apr 10, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Apr 10, 2026
Better uop coverage in the JIT optimizer #131798
Open
@kumaraditya303 kumaraditya303 added the topic-JIT label Apr 10, 2026
@kumaraditya303
kumaraditya303 marked this pull request as ready for review April 10, 2026 14:01
@kumaraditya303
kumaraditya303 requested review from Fidget-Spinner, markshannon, savannahostrowski and tomasr8 as code owners April 10, 2026 14:01
@bedevere-app bedevere-app Bot added the awaiting core review label Apr 10, 2026
@kumaraditya303

kumaraditya303 commented Apr 11, 2026

Copy link
Copy Markdown
Contributor Author

This can be done for class lookups as well so things like Foo.bar() where bar is a classmethod gets constant folded.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Fidget-Spinner

Fidget-Spinner commented Apr 12, 2026

Copy link
Copy Markdown
Member

I don't see any change in deltablue, but that's because it's using cls.classmethod() instead of self.classmethod(). Would you like to add that to this PR as well? https://github.com/python/pyperformance/blob/main/pyperformance/data-files/benchmarks/bm_deltablue/run_benchmark.py#L234

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

kumaraditya303 added 3 commits April 12, 2026 14:41
@kumaraditya303
constant fold on classes as well
ac515b8
@kumaraditya303
Revert "constant fold on classes as well"
55bf43a
This reverts commit ac515b8.
@kumaraditya303
fix formatting
5b78953
@Fidget-Spinner

Fidget-Spinner commented Apr 12, 2026

Copy link
Copy Markdown
Member

I don't see any change in deltablue, but that's because it's using cls.classmethod() instead of self.classmethod(). Would you like to add that to this PR as well? https://github.com/python/pyperformance/blob/main/pyperformance/data-files/benchmarks/bm_deltablue/run_benchmark.py#L234

This turned out to be harder than I thought, because it would fold the classmethod descriptor into a unbound method lookup rather than a bound one, which changes the following CALL_BOUND_METHOD_EXACT_ARGS in the trace.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@NekoAsakura

NekoAsakura commented Apr 13, 2026

Copy link
Copy Markdown
Member

The outer else doesn't set self_or_null[0], intentional?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kumaraditya303
Merge branch 'main' of https://github.com/python/cpython into jit-cla…
e9e6faf
…ssmethod
markshannon
markshannon reviewed Apr 15, 2026
View reviewed changes
Comment thread Python/optimizer_bytecodes.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@markshannon

markshannon commented Apr 15, 2026

Copy link
Copy Markdown
Member

I don't see any change in deltablue, but that's because it's using cls.classmethod() instead of self.classmethod(). Would you like to add that to this PR as well? https://github.com/python/pyperformance/blob/main/pyperformance/data-files/benchmarks/bm_deltablue/run_benchmark.py#L234

This turned out to be harder than I thought, because it would fold the classmethod descriptor into a unbound method lookup rather than a bound one, which changes the following CALL_BOUND_METHOD_EXACT_ARGS in the trace.

That shouldn't be a problem for staticmethod though, as self_or_null is always NULL.

It would make sense for LOAD_ATTR 1 for classmethod to be leaving func cls on the stack.

If we special case classmethod in _PyObject_GetMethodStackRef, it should add negligible overhead in the interpreter (possibly speeding things up if classmethods are common) and will enable this optimization in the JIT.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Fidget-Spinner

Fidget-Spinner commented Apr 15, 2026

Copy link
Copy Markdown
Member

If we special case classmethod in _PyObject_GetMethodStackRef, it should add negligible overhead in the interpreter (possibly speeding things up if classmethods are common) and will enable this optimization in the JIT.

Working on that.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

kumaraditya303 added 2 commits April 16, 2026 17:03
@kumaraditya303
use optimize_pop_top
a36900e
@kumaraditya303
Merge branch 'main' of https://github.com/python/cpython into jit-cla…
7026360
…ssmethod
@kumaraditya303
kumaraditya303 requested a review from markshannon April 16, 2026 11:35
@github-actions

github-actions Bot commented May 18, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label May 18, 2026
@ambv

ambv commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

This PR is superseded by #157011, a newer open pull request for the same issue (gh-131798), and it no longer merges cleanly. Closing to cull the number of open pull requests. Do not reopen this pull request without getting a go-ahead from a triager or core developer.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ambv ambv closed this Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@tomasr8 tomasr8 Awaiting requested review from tomasr8 tomasr8 is a code owner
@Fidget-Spinner Fidget-Spinner Awaiting requested review from Fidget-Spinner Fidget-Spinner is a code owner
@savannahostrowski savannahostrowski Awaiting requested review from savannahostrowski
@markshannon markshannon Awaiting requested review from markshannon markshannon is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting core review skip news stale Stale PR or inactive for long period of time. topic-JIT

Projects

None yet

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.

5 participants

@kumaraditya303 @Fidget-Spinner @NekoAsakura @markshannon @ambv
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.