Skip to content

gh-131798: constant fold classmethod and staticmethod in JIT#148331

Open
kumaraditya303 wants to merge 1 commit intopython:mainfrom
kumaraditya303:jit-classmethod
Open

gh-131798: constant fold classmethod and staticmethod in JIT#148331
kumaraditya303 wants to merge 1 commit intopython:mainfrom
kumaraditya303:jit-classmethod

Conversation

@kumaraditya303
Copy link
Copy Markdown
Contributor

@kumaraditya303 kumaraditya303 commented Apr 10, 2026

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

@kumaraditya303
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.

@Fidget-Spinner
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

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.

2 participants