Trajectory datavec now cached during NLP obj/jac/hess assembly callbacks, leaving only constant allocation overhead#97
Merged
Conversation
…cks, leaving only constant allocation overhead
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Merged
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.
DTO Evaluator cache refactor — PR summary
What changed
_update_trajectory_cache!inDirectTrajOpt.jl/src/solvers/evaluator.jlwas constructing a freshNamedTrajectoryon every MOI callback (objective, gradient, constraints, Jacobian, Hessian — 5-8 calls per Ipopt iteration). Each construction triggered the full copy-constructor → inner-constructor chain in NamedTrajectories.jl, which re-derives dims, names, states, bounds (via an OrderedDict round-trip), and global metadata from scratch — ~10-12 allocations per call, all redundant since onlydatavecandglobal_datachange between calls.The fix adds a single
_cached_traj::NamedTrajectoryfield to theEvaluatorstruct, built once at construction with ownedVector{Float64}copies ofdatavecandglobal_data. On each callback,_update_trajectory_cache!now doescopyto!into these buffers instead of constructing a new struct. All structural metadata (components, dims, bounds, N, etc.) is shared withprob.trajectoryand never recomputed.Why it's safe
Investigation confirmed that every downstream consumer of the returned
traj— all integratorevaluate!/eval_jacobian/eval_hessian_of_lagrangianmethods, all constraintevaluate!/eval_jacobianmethods, all objectiveobjective_value/gradient!methods — usestrajpurely as a read-only data accessor. None store, alias, or compare it by identity. Views viatraj[k]are re-derived fromtraj.datavecon each access (no stale views across callbacks). Full trace inevaluator-cache-analysis.md.