Reduce load-time invalidations from untyped operator methods#77
Open
DavidSagan wants to merge 1 commit into
Open
Reduce load-time invalidations from untyped operator methods#77DavidSagan wants to merge 1 commit into
DavidSagan wants to merge 1 commit into
Conversation
The out-of-place `*` for TaylorMaps and the `SymplecticS` `+/-/*//`
operators were defined with an untyped first (or second) argument, e.g.
`*(m2, m1::DAMap)` and `*(M, S::SymplecticS)`. Inserting these
`f(::Any, ...)` methods at load time invalidates large amounts of
precompiled generic code that calls the same functions.
Constrain the arguments to the types actually accepted:
- `*(m2, m1::TaylorMap)` is split into `*(m2::TaylorMap, m1::TaylorMap)`
(map composition) and `*(m2::Union{Number,AbstractArray}, m1::TaylorMap)`
(TPS scalar/vector function composition).
- `∘(m2, m1::TaylorMap)` gains the same `Union{Number,AbstractArray}`
constraint on `m2`.
- The `SymplecticS` operators constrain the matrix argument to
`AbstractVecOrMat` (they already index it via `size(M, .)`).
Measured with SnoopCompile on `using SciBmad`: NonlinearNormalForm's
invalidation children drop from 197 to 56 (the removed 141 were exactly
the untyped `*`/`SymplecticS` roots). No behavior change for valid
inputs; full test suite passes.
Refs bmad-sim/SciBmad.jl#77
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
Reduce load-time invalidations from untyped operator methods
Part of the invalidation-reduction work tracked in bmad-sim/SciBmad.jl#77.
Problem
Several operators were defined with an untyped first (or second) argument, which makes them
f(::Any, …)methods. Inserting such methods at package load time invalidates large amounts of already-compiled generic code that calls the same functions:map.jl:*(m2, m1::$t) = ∘(m2, m1)→*(::Any, ::DAMap)/*(::Any, ::TPSAMap)map.jl:∘(m2, m1::$t)→∘(::Any, ::DAMap)/∘(::Any, ::TPSAMap)utils.jl: theSymplecticS+ - * /operators →*(::Any, ::SymplecticS),*(::SymplecticS, ::Any), etc.Fix
Constrain the arguments to the types actually accepted (no behavior change for valid inputs):
*(m2, m1::TaylorMap)is split into*(m2::$t, m1::$t)— map ∘ map composition, and*(m2::Union{Number,AbstractArray}, m1::$t)— TPS scalar/vector-function composition.∘(m2, m1::$t)gains the samem2::Union{Number,AbstractArray}constraint (its body already requireseltype(m2)to be a TPS type).SymplecticSoperators constrain the matrix argument toAbstractVecOrMat(they already index it withsize(M, …)).DAMap/TPSAMapare<: TaylorMap(not<: Number/AbstractArray), so the split methods are unambiguous.Measurement
Using
SnoopCompile's@snoop_invalidations using SciBmad(Julia 1.11.7):The 141 removed are exactly the untyped
*andSymplecticSroots (*(m2, m1::DAMap)= 41,*(m2, m1::TPSAMap)= 41,*(M, S::SymplecticS)= 41,*(S::SymplecticS, M)= 16,+(M, S::SymplecticS)= 2). No new roots are introduced. The residual 56 are the concrete-argument∘map-composition methods, which are structural.Tests
Pkg.test("NonlinearNormalForm")passes, including the "Composition and inversion" (14/14) and "Comparison with FPP" (19/19) suites that directly exercise the changed operators.🤖 Generated with Claude Code