perf: build CompoundGeometry.geom lazily#359
Merged
mortenengen merged 2 commits intoJun 5, 2026
Merged
Conversation
self.geom (the MultiPolygon used only by _repr_svg_ for display) was built in __init__ by buffering every point geometry into a circle on every construction -- including every rotate() inside the equilibrium solvers. Defer it to a lazy `geom` property computed on first access. Standalone speed-ups vs main (medium section, best of 5): marin: bending 1.5x, moment-curvature 1.4x, N-M 1.4x fiber: ~1.0-1.1x (fiber rarely reconstructs geometry) All 5494 geometry + section tests pass.
Assert that `geom` is built on first access (not in __init__), contains one polygon per surface geometry plus a buffered circle per point geometry, is cached on repeated access, and is recomputed by a transformed copy.
talledodiego
approved these changes
Jun 3, 2026
talledodiego
left a comment
Collaborator
There was a problem hiding this comment.
I totally agree with this, thanks!
mortenengen
approved these changes
Jun 5, 2026
mortenengen
left a comment
Member
There was a problem hiding this comment.
Thanks for this useful improvement!
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.
Closes #358
CompoundGeometry.__init__eagerly builtself.geom— theMultiPolygonused only by
_repr_svg_for display — by buffering every point geometry intoa circle. This ran on every construction, including every
rotate()callinside the equilibrium solvers, even though the result is only ever needed for
SVG display.
This PR defers it to a lazy
geomproperty computed on first access (cached in_geom). Behaviour on read is unchanged:geomreturns the sameMultiPolygonand
_repr_svg_is untouched.Performance (medium-complexity RC section, best of 5, vs
dev)(marin reconstructs geometry once per iteration)
(fiber reconstructs geometry only once per calculation)
Risk assessment
Low, in my assessment. No material state, no public API change. The lazy property is the
only writer of
_geom;_repr_svg_is the only reader ofgeom. Allconstruction paths (
__init__,rotate/translate/mirror/__add__/__sub__/from_geometry) initialise_geom.Tests
Full geometry + section suite passes;
ruff formatandruff checkclean.Added
test_compound_geometry_lazy_geomcovering lazy build, correct contents,caching, and recomputation after a transform.