perf: import awkward/vector/graphviz/particle lazily#382
Open
henryiii wants to merge 1 commit into
Open
Conversation
Importing pylhe previously eagerly imported graphviz, particle (plus building the PDGID->LaTeX DirectionalMaps at module load) and, via the bottom-of-module ``from .awkward import to_awkward``, awkward and vector (which register vector/awkward behaviors at import time). None of these are needed for the core LHE reader/writer. After this change ``import pylhe`` imports none of those four modules: - ``to_awkward`` is exposed lazily through a module-level ``__getattr__`` that imports it from ``.awkward`` on first access. ``from pylhe import to_awkward``, ``pylhe.to_awkward`` and ``import pylhe.awkward`` all still work, and the vector/awkward behavior registrations happen only when the awkward bridge is actually used. - graphviz and particle (and the DirectionalMaps construction, now cached via ``functools.cache``) are imported inside ``LHEEvent._build_graph``, so they load only when the ``graph`` property is used. Annotations are quoted with a ``TYPE_CHECKING`` guarded import. Public API is unchanged; dependencies remain required (making them optional extras is a possible follow-up). Adds a test that asserts the four heavy modules are not imported by ``import pylhe`` in a fresh interpreter. Assisted-by: ClaudeCode:claude-opus-4-8
🏎️ Benchmark Comparison |
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.
🤖 AI text below 🤖
Summary
import pylhepreviously eagerly imported four heavy dependencies that the core reader/writer never needs:to_awkward(pulled in by the module-bottomfrom .awkward import to_awkward, which also runsvector.register_awkward()and severalak.mixin_classregistrations at import time).LHEEvent.graphvisualization (plus the module-level_PDGID2LaTeXNameMap = DirectionalMaps(...)construction).This PR makes all four lazy so the core parse path imports nearly instantly, with no public API changes.
Changes
from .awkward import to_awkward;to_awkwardis now exposed via a module-level__getattr__that imports it from.awkwardon first access (raising the standardAttributeErrorfor unknown names)."to_awkward"stays in__all__/__dir__.from pylhe import to_awkward,pylhe.to_awkward, andimport pylhe.awkwardall still work, and the vector/awkward behavior registrations now happen only when the awkward bridge is actually used.graphviz/particleimports and theDirectionalMapsconstruction intoLHEEvent._build_graph. The PDGID→LaTeX map is built once and cached viafunctools.cache. Type annotations are quoted with aTYPE_CHECKING-guardedimport graphviz.strict(the# type: ignore[import-untyped]for untyped graphviz now lives on theTYPE_CHECKINGimport; no stale ignores).Laziness verification
passes after the change. A new
tests/test_api.py::test_lazy_heavy_importsasserts this in a fresh interpreter (viasubprocess.run) so other tests' imports do not pollutesys.modules.pylhe.to_awkwardandLHEEvent.graphwere verified end-to-end (covered bytests/test_awkward.pyandtests/test_visualize.py).Notes
Part of #378
🤖 Generated with Claude Code