-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
[Enhancement] Speed up Registry initialization #1844
[Enhancement] Speed up Registry initialization #1844
Conversation
This PR addresses open-mmlab#1843. Instead of calling inspect.stack() to read the entire stack and its associated source files from disk, walk up the stack to get only the specific frame that we need (see [1] for additional information). This makes imports in downstream projects ~2.5x faster in my local dev environment. For mmaction2, for example: Before: $ python -m timeit -n1 -r1 "from mmaction.apis import init_recognizer, inference_recognizer" 1 loop, best of 1: 1.94 sec per loop After: $ python -m timeit -n1 -r1 "from mmaction.apis import init_recognizer, inference_recognizer" 1 loop, best of 1: 754 msec per loop [1] https://stackoverflow.com/a/42636264/895769
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test shows it did help.Could you add some comment as suggested? @astahlman
Explain why we avoid `inspect.stack()` with link to PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
@imabackstabber you've already reviewed this one but Github still has the review in the "Requested changes" state. Mind approving if there are no other issues? |
Hi @astahlman, thanks for your contribution, I have merged the PR. |
Thanks for your contribution and we appreciate it a lot. The following instructions would make your pull request more healthy and more easily get feedback. If you do not understand some items, don't worry, just make the pull request and seek help from maintainers.
Motivation
Please describe the motivation of this PR and the goal you want to achieve through this PR.
This PR addresses #1843 by improving the performance of
Registry.__init__()
Modification
Instead of calling inspect.stack() to read the entire stack and its
associated source files from disk, walk up the stack to get only the
specific frame that we need (see this answer on StackOverflow
for additional information).
This change results in a ~2.5x speed-up when importing from downstream
projects in my local dev environment. For mmaction2, for example:
Before:
After:
BC-breaking (Optional)
Does the modification introduce changes that break the backward-compatibility of the downstream repositories?
If so, please describe how it breaks the compatibility and how the downstream projects should modify their code to keep compatibility with this PR.
No
Use cases (Optional)
If this PR introduces a new feature, it is better to list some use cases here, and update the documentation.
Checklist
Before PR:
After PR: