Skip to content

gh-143732: Specialize __setitem__ dunder method for STORE_SUBSCR - #156033

Open
johng wants to merge 1 commit into
python:mainfrom
johng:gh-143732-store-subscr-py-dunder
Open

gh-143732: Specialize __setitem__ dunder method for STORE_SUBSCR#156033
johng wants to merge 1 commit into
python:mainfrom
johng:gh-143732-store-subscr-py-dunder

Conversation

@johng

@johng johng commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

This is precursuor for JIT improvements but also results in standard interpreter improvements due to specializing the bytecode.

This improves performance by about 5% on linux and macos micro benchmark

import pyperf


class Grid:
    __slots__ = ("_cell",)

    def __setitem__(self, key, value):
        self._cell = value


def bench_store_subscr(loops):
    obj = Grid()
    range_it = range(loops)
    t0 = pyperf.perf_counter()
    for _ in range_it:
        # 20 stores per loop; keep in sync with inner_loops below
        obj[0] = 0; obj[0] = 0; obj[0] = 0; obj[0] = 0; obj[0] = 0
        obj[0] = 0; obj[0] = 0; obj[0] = 0; obj[0] = 0; obj[0] = 0
        obj[0] = 0; obj[0] = 0; obj[0] = 0; obj[0] = 0; obj[0] = 0
        obj[0] = 0; obj[0] = 0; obj[0] = 0; obj[0] = 0; obj[0] = 0
    return pyperf.perf_counter() - t0

if __name__ == "__main__":
    runner = pyperf.Runner()
    runner.metadata["description"] = "STORE_SUBSCR on an object with a Python __setitem__"
    runner.bench_time_func("store_subscr_py_dunder", bench_store_subscr, inner_loops=20)

This is precursuor for JIT improvements but also results in
standard improvements due to specializing the bytecode
@johng
johng force-pushed the gh-143732-store-subscr-py-dunder branch from 6ccc255 to 88e57eb Compare August 19, 2026 02:51
@johng
johng marked this pull request as ready for review August 19, 2026 03:01
@johng
johng requested a review from markshannon as a code owner August 19, 2026 03:01
@markshannon

Copy link
Copy Markdown
Member

This does not implement the specialization of STORE_SUBSCR for __setitem__ implemented in Python, as described in the issue. If the description in the issue is not clear, please ask for clarification (on the issue, not here)

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