Skip to content

Get dataclass declaration scope locals with inspect.currentframe() #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 22, 2022

Conversation

noirbee
Copy link
Contributor

@noirbee noirbee commented Apr 22, 2022

inspect.stack() can be very costly, especially when called with
context=1: it has to get information about source files / lines
etc. This gets very slow when using dozens of dataclasses in some
large-ish codebases (where stacks can be relatively deep).

Also add the frame when class_schema() is called directly.

inspect.stack() can be very costly, especially when called with
context=1: it has to get information about source files / lines
etc. This gets very slow when using dozens of dataclasses in some
large-ish codebases (where stacks can be relatively deep).

Also add the frame when class_schema() is called directly.
@lovasoa lovasoa merged commit 22cb614 into lovasoa:master Apr 22, 2022
@lovasoa
Copy link
Owner

lovasoa commented Apr 22, 2022

Thanks for the pr

@noirbee noirbee deleted the fix/faster-inspect-get-caller-frame branch April 26, 2022 15:13
@luk2302
Copy link

luk2302 commented May 26, 2022

@noirbee @lovasoa This causes quite a significant memory leak due to _internal_class_schema being lru_cache-d and the stack frame therefore being cached and kept alive for a very long time. Especially if there are big locals around when calling class_schema, even only in the calling code way higher up in the stack frame since the f_back properties of the class_frame bubble up and contain the entire stack and all the local variables. If we manually call _internal_class_schema.cache_clear() the memory is freed. You must not insert the clazz_frame into any cache.

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.

3 participants