[Performance] PyScene::update does GetAttrString("update") every frame even when unoverridden #343

Closed
opened 2026-07-10 21:57:25 +00:00 by john · 0 comments
Owner

Problem

PyScene::update calls PyObject_GetAttrString((PyObject*)self, "update") every frame (src/PySceneObject.cpp:405), a string-keyed dict lookup, even for scenes whose Python subclass does not override update.

Proposed fix

Cache whether the subclass overrides update (a bool flag resolved once, or a cached bound-method reference), and skip the lookup when it doesn't. The codebase already uses this pattern on the hover hot path — PyScene.cpp:361 checks is_python_subclass "to avoid overhead on hot path" before calling into Python. Apply the same guard here.

Benchmark

Measure per-frame scene-update overhead headless with a plain (non-overriding) scene, before/after. Add to tests/benchmarks/.

Related: #331.

## Problem `PyScene::update` calls `PyObject_GetAttrString((PyObject*)self, "update")` **every frame** (`src/PySceneObject.cpp:405`), a string-keyed dict lookup, even for scenes whose Python subclass does not override `update`. ## Proposed fix Cache whether the subclass overrides `update` (a bool flag resolved once, or a cached bound-method reference), and skip the lookup when it doesn't. The codebase already uses this pattern on the hover hot path — `PyScene.cpp:361` checks `is_python_subclass` "to avoid overhead on hot path" before calling into Python. Apply the same guard here. ## Benchmark Measure per-frame scene-update overhead headless with a plain (non-overriding) scene, before/after. Add to `tests/benchmarks/`. Related: #331.
john closed this issue 2026-07-11 03:02:54 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
john/McRogueFace#343
No description provided.