perf: cache MatrixAccessor properties to avoid redundant recomputation#616
Open
MaykThewessen wants to merge 1 commit intoPyPSA:masterfrom
Open
perf: cache MatrixAccessor properties to avoid redundant recomputation#616MaykThewessen wants to merge 1 commit intoPyPSA:masterfrom
MaykThewessen wants to merge 1 commit intoPyPSA:masterfrom
Conversation
Change vlabels, clabels, A, c, b, sense, lb, ub, vtypes, and Q from @Property to @cached_property in MatrixAccessor, and add them to clean_cached_properties() for proper invalidation. Previously, each access to these properties recomputed from scratch. During to_highspy(), properties like vlabels and clabels are accessed multiple times (directly and indirectly through A), causing redundant sparse matrix operations and DataFrame lookups. For large models (~593K vars, ~1.38M constraints), this adds ~15s of overhead per solve. The cache is already properly invalidated at the start of solve() and _mock_solve() via existing clean_cached_properties() calls. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
3 tasks
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.
Summary
vlabels,clabels,A,c,b,sense,lb,ub,vtypes, andQfrom@propertyto@cached_propertyinMatrixAccessorclean_cached_properties()for proper invalidationMotivation
During
to_highspy()(and other direct API exporters),MatrixAccessorproperties are accessed multiple times — both directly and indirectly. For example,M.Ainternally accessesM.clabelsandM.vlabels, andM.caccessesM.flat_vars. Each access recomputes from scratch, including rebuilding sparse matrices and flattening DataFrames.For large models (~593K variables, ~1.38M constraints), this redundant recomputation adds measurable overhead per solve call. Caching eliminates this since the underlying model data doesn't change between property accesses within a single solve.
The cache is already properly invalidated at the start of
Model.solve()andModel._mock_solve()via existingclean_cached_properties()calls.Context
See discussion in #198 (comment) for profiling data from a real-world 52-chunk DC-OPF optimization.
Test plan
test_matrices.pytests pass (verify shapes, values, masked models)test_matrices_properties_are_cachedverifies identity caching and invalidationtest_optimization.pyhighs-direct tests pass (24/25 — one pre-existing failure intest_modified_model)test_io.pytests passNote:
test_modified_modelfails on upstreammasteras well — it's a pre-existing issue unrelated to this change.🤖 Generated with Claude Code