lazy imports - #82
Merged
Merged
lazy imports#82
Conversation
max-models
marked this pull request as ready for review
September 29, 2026 11:48
spossann
approved these changes
Sep 29, 2026
max-models
added a commit
to struphy-hub/struphy
that referenced
this pull request
Sep 29, 2026
feectools PR: struphy-hub/feectools#82 Importing `struphy` or `Simulation` eagerly loads models, propagators, plotting libraries, and numerical dependencies that may never be used. This change resolves public API objects, models, and propagators on first access and moves expensive imports into the operations that need them. **Solves the following issue(s):** Reduces Python startup overhead. Companion PR: [feectools #82](struphy-hub/feectools#82). **Core changes:** - Add lazy module exports with cached lookups, `dir()` support, and imports for static type checking. Update the package initializer generator to produce lazy exports as well. - Load all models when iterating over `StruphyModel`, so model discovery still includes classes that have not yet been accessed. - Defer PyVista, Matplotlib, IPython, and selected SciPy imports until visualization, display, or numerical operations need them. Centralize the optional IPython display fallbacks. - Use MPI launch detection from feectools and avoid importing MPI solely for type annotations. Serial startup uses the mock communicator without initializing MPI. - Reuse feectools' lightweight integer factorization and use `scipy.special.ndtri` for normally distributed Sobol samples instead of importing `scipy.stats.norm`. - Update the feectools submodule to include the companion import optimizations and CI fixes. **Model-specific changes:** No model equations or algorithms are intentionally changed; model classes are loaded on demand. **Documentation changes:** Fix the escaped `mu` in the `Particles5D` mathematical docstring. **Validation:** The existing PR reports the following cluster measurement for: ```bash python -c "from struphy import Simulation" ``` - Before: 10.6 s. - After: 1.6 s (approximately 6.6 times faster).
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.
Struphy PR: struphy-hub/struphy#383
Reduce feectools startup overhead by avoiding MPI initialization in ordinary serial processes, removing the SymPy import from partitioning, and deferring solver and compiler dependencies until needed. This supports the lightweight imports in struphy #383.
Core changes:
launched_under_mpi()to detect MPI launcher environment variables or an already initialized MPI runtime without importing mpi4py.STRUPHY_MPIcan override launch detection.MockMPIoutside an MPI launch. Support explicit MPI opt-out throughFEECTOOLS_MPI=0orfeectools.use_mpi = False, set before importingfeectools.ddm.mpi. The in-process option is not inherited by MPI subprocesses.CI fixes:
Cython>=3.2,<3.3. Cython 3.3.0 rejects pointer indexing inPC.pyxwithInvalid index type 'int', causing all six compilation jobs to fail during prerequisite installation. Retain the existing setuptools constraint and build without isolation so the constraints apply.github.actorandGITHUB_TOKEN, withpackages: read, instead of the customGHCR_PROJECT_TOKEN. This uses the package's configured Actions access for feectools.Validation: