Deferred loading of NumPy and SciPy dependencies#1521
Open
josemmo wants to merge 3 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
import compas.geometrytakes several seconds due to top-level NumPy and SciPy imports incompas.geometryandcompas.data. This triggers a cascade of around 470 transitive imports at startup, even when those libraries are not used.While the COMPAS codebase already defers heavy imports in several places123, some modules don't follow this practice, which defeats the effort made elsewhere.
Impact of the PR
How to reproduce
The profile will show
_io.open_codedominating runtime across 500+ file reads. This is Python pulling in source/bytecode files from NumPy/SciPy from disk.For my particular device, it took 2.21 seconds to run
import compas.geometry. Note this time may vary significant depending on the machine hardware and cache status.The log will contain around 470 NumPy/SciPy imports with high cumulative times. For example:
How to verify fix
Repeat the steps above after switching to this branch:
uv add "git+https://github.com/josemmo/compas@performance/deferred-loading"The profile should be significantly shorter (0.14 seconds in my case) and the import log should contain no NumPy or SciPy entries.
Alternatives considered
lazy_loader(SPEC 1): The approach used by NumPy, scikit-image, NetworkX, and other libraries. Compatible with Python 3.9+, but would require restructuring large parts of the codebase.lazykeyword (PEP 810): Native language support, but requires Python 3.15+, which is outside COMPAS's current support range.Footnotes
https://github.com/compas-dev/compas/blob/e03e40975ebec56ab98b4cfaead1eedf70241cb4/src/compas/geometry/polyhedron.py#L395 ↩
https://github.com/compas-dev/compas/blob/e03e40975ebec56ab98b4cfaead1eedf70241cb4/src/compas/geometry/_core/distance.py#L595 ↩
https://github.com/compas-dev/compas/blob/e03e40975ebec56ab98b4cfaead1eedf70241cb4/src/compas/datastructures/mesh/mesh.py#L404 ↩