Skip to content

perf: find expose()'s caller from its frame instead of inspect.stack() - #74

Merged
lesnik512 merged 1 commit into
mainfrom
perf/expose-caller-frame
Oct 17, 2026
Merged

lesnik512 merged 1 commit into
mainfrom
perf/expose-caller-frame

Conversation

@lesnik512

Copy link
Copy Markdown
Member

expose() found its caller's module with inspect.stack()[1].frame. inspect.stack() builds a FrameInfo for every frame on the stack and reads source lines for each one, only for the code to use the first frame. Called from a conftest during pytest collection, with 24 frames on the stack, that took 1.3 ms of the 1.5 ms one expose() call cost.

It now takes the caller's frame with sys._getframe(1) and still passes it to inspect.getmodule, so the exec() case keeps raising the same RuntimeError. One expose() call from a conftest drops from 1.524 ms to 0.030 ms.

sys._getframe trips ruff's SLF001, so the line carries a noqa. inspect.currentframe() would avoid it but can return None, which would need its own handling. ADR-0001 and the docstring said "via inspect.stack()" and now say "the calling frame".

This is a performance change only. The existing tests cover it, and all 27 pass with 100% coverage.

@lesnik512
lesnik512 merged commit 0479d87 into main Oct 17, 2026
12 checks passed
@lesnik512
lesnik512 deleted the perf/expose-caller-frame branch October 17, 2026 19:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant