Skip to content
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

inspect.stack() is slow. #532

Closed
bckohan opened this issue Apr 5, 2024 · 0 comments
Closed

inspect.stack() is slow. #532

bckohan opened this issue Apr 5, 2024 · 0 comments

Comments

@bckohan
Copy link
Contributor

bckohan commented Apr 5, 2024

By default, if scope is not provided include() pulls the scope off the stack frame above the include() frame. It does this by calling inspect.stack(). This call does an enormous amount of unnecessary work. It builds frame info objects for the entire stack.

I noticed this on a production system that has some management commands that have shell tab completions. This means each time I hit tab, the entire Django stack is loaded. It was noticeably slow (~1.5 seconds). Profiling suggested 1/3 of that time was spent in inspect.stack(). This production system had roughly 20 calls to include() - to build the settings.

I removed all of that overhead by changing this line:

scope = scope or inspect.stack()[1][0].f_globals

To this:

scope = scope or sys._getframe(1).f_globals

I'll submit a PR.

bckohan added a commit to bckohan/django-split-settings that referenced this issue Apr 5, 2024
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

No branches or pull requests

1 participant