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

[Enhancement] Speed up Registry initialization #1844

Merged
merged 2 commits into from
Apr 14, 2022

Conversation

astahlman
Copy link
Contributor

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:

$ 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

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:

  • [ X ] I have read and followed the workflow indicated in the CONTRIBUTING.md to create this PR.
  • [ X ] Pre-commit or linting tools indicated in CONTRIBUTING.md are used to fix the potential lint issues.
  • [ X ] Bug fixes are covered by unit tests, the case that causes the bug should be added in the unit tests.
  • [ X ] New functionalities are covered by complete unit tests. If not, please add more unit test to ensure the correctness.
  • [ X ] The documentation has been modified accordingly, including docstring or example tutorials.

After PR:

  • If the modification has potential influence on downstream or other related projects, this PR should be tested with some of those projects, like MMDet or MMCls.
  • CLA has been signed and all committers have signed the CLA in this PR.

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
@CLAassistant
Copy link

CLAassistant commented Mar 28, 2022

CLA assistant check
All committers have signed the CLA.

imabackstabber
imabackstabber previously approved these changes Mar 29, 2022
Copy link
Contributor

@imabackstabber imabackstabber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Copy link
Contributor

@imabackstabber imabackstabber left a 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

mmcv/utils/registry.py Show resolved Hide resolved
Explain why we avoid `inspect.stack()` with link to PR
Copy link
Contributor

@imabackstabber imabackstabber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@astahlman
Copy link
Contributor Author

@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?

@zhouzaida zhouzaida changed the title Speed up Registry initialization [Enhancement] Speed up Registry initialization Apr 14, 2022
@zhouzaida zhouzaida merged commit cd9dcc1 into open-mmlab:master Apr 14, 2022
@zhouzaida
Copy link
Collaborator

zhouzaida commented Apr 14, 2022

Hi @astahlman, thanks for your contribution, I have merged the PR.

@imabackstabber imabackstabber linked an issue Apr 14, 2022 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Speed up Registry initialization
5 participants