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

Use lazy imports in topology to speed up library import. #942

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from

Conversation

jsmnbom
Copy link

@jsmnbom jsmnbom commented Mar 18, 2025

Loads IPython and scipy only when actually needed. Made build123d load about 1 second (on average) faster on my machine. Python caches modules so all in all this should only be a net benefit.

This is nice when needing to restart the jupyter kernel and having to not wait as long for the library to import.
It would be even better if we could perhaps split the exporters and importers into seperate submodules, and require the user to be explicit about importing them. This would allow ezdxf and svgpathtools to be lazily imported, allowing for about half a second speed up (theoretically).

jsmnbom added 2 commits March 18, 2025 10:31
Loads IPython and scipy only when actually needed. Made build123d load about 1 second faster on my machine. Python caches modules so all in all this should only be a net benefit.
@gumyr
Copy link
Owner

gumyr commented Mar 18, 2025

I'm not convinced about this one. Users can skip reloading all of the build123d (and ocp_vscode) content by following the best practices shown here: https://github.com/bernhard-42/vscode-ocp-cad-viewer?tab=readme-ov-file#best-practices (yes, this should be in the build123d documentation too).

Use the Jupyter extension for a more interactive experience. This allows to have one cell (separated by # %%) at the beginning to import all libraries

# %%
from build123d import *
from ocp_vscode import *

# %%
b = Box(1,2,3)
show(b)
# %%

In addition, there is already a proposal to optimize the IPython content see: #924

@jsmnbom
Copy link
Author

jsmnbom commented Mar 18, 2025

My use case is to have helper code in a separate python package. This helper code needs to import build123d objects.

I thought the only way to refresh the help code in jupyter was to restart the kernel, since IPython extension autoload didn't seem to work - after adddional testing, autoreload does seem to work, but it just behaves weird in quite a few circumstances. All in all I still believe that it makes sense to try to make the library load faster and lazy load heavier dependencies when they are required, but if you do not agree, feel free to close the PR :)

@gumyr
Copy link
Owner

gumyr commented Mar 18, 2025

Getting faster loading times is clearly a good thing. Let me think about it a bit...

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.

2 participants