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

Add LazyLoader for delayed imports of slow modules #4653

Merged
merged 6 commits into from
Nov 10, 2021

Conversation

dabacon
Copy link
Collaborator

@dabacon dabacon commented Nov 10, 2021

A solution for #4648

This creates a class LazyLoader that can be used in place of an import and delays importing the module.

optimize = _import.LazyLoader("optimize", globals(), "scipy.optimize")

For classes that take up extra initialization time this speeds the overall first import of cirq with the potential downside of slowing doing the first uses where it is used.

This PR applies this to previous cases of delayed imports in scipy.

@dabacon dabacon requested review from cduck, mrwojtek, vtomole and a team as code owners November 10, 2021 03:38
@dabacon dabacon requested a review from tanujkhattar November 10, 2021 03:38
@google-cla google-cla bot added the cla: yes Makes googlebot stop complaining. label Nov 10, 2021
@CirqBot CirqBot added the size: M 50< lines changed <250 label Nov 10, 2021
Copy link
Collaborator

@95-martin-orion 95-martin-orion left a comment

Choose a reason for hiding this comment

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

Very cool.

An aside: could this also be applied to fix import-order issues (e.g. when something in cirq.devices needs to import cirq.circuits) that are currently handled by function-level imports?

@dabacon
Copy link
Collaborator Author

dabacon commented Nov 10, 2021

Very cool.

An aside: could this also be applied to fix import-order issues (e.g. when something in cirq.devices needs to import cirq.circuits) that are currently handled by function-level imports?

I think so yes, I think I can probably craft a regex to look for those.

@dabacon dabacon merged commit a4f5561 into quantumlib:master Nov 10, 2021
@dabacon dabacon mentioned this pull request Nov 11, 2021
@dabacon dabacon deleted the lazylazylazy branch April 16, 2022 15:42
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
This allows for delayed import for modules, which is useful when the modules load time is slow and we don't want these imports on a global level.
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
This allows for delayed import for modules, which is useful when the modules load time is slow and we don't want these imports on a global level.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Makes googlebot stop complaining. size: M 50< lines changed <250
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants