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

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.

1 participant