Repository navigation
perf: find expose()'s caller from its frame instead of inspect.stack() - #74
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
expose()found its caller's module withinspect.stack()[1].frame.inspect.stack()builds aFrameInfofor 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 oneexpose()call cost.It now takes the caller's frame with
sys._getframe(1)and still passes it toinspect.getmodule, so theexec()case keeps raising the sameRuntimeError. Oneexpose()call from a conftest drops from 1.524 ms to 0.030 ms.sys._getframetrips ruff's SLF001, so the line carries anoqa.inspect.currentframe()would avoid it but can returnNone, which would need its own handling. ADR-0001 and the docstring said "viainspect.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.