Perf T1-11: Pre-compute sparse pattern for use_sparse mode#152
Open
hmzh-khn wants to merge 3 commits into
Open
Perf T1-11: Pre-compute sparse pattern for use_sparse mode#152hmzh-khn wants to merge 3 commits into
hmzh-khn wants to merge 3 commits into
Conversation
Tests verify: - Sparse pattern (rowval/colptr) is structurally identical across z values - Cached sparse solve matches fresh sparse solve numerically - run_nonlinear_solver works with use_sparse=:always - Cached nzval update allocates less than fresh sparse() construction Co-Authored-By: Claude Opus 4.6 <[email protected]>
Pre-compute SparseMatrixCSC sparsity patterns once per player during solver setup, then reuse them across iterations by updating nzval in-place instead of calling sparse(M) each time. Changes: - Add _update_sparse_from_dense!() helper for zero-alloc nzval update - Add sparse_cache kwarg to _solve_K!() to use cached patterns - Add sparse_M_caches kwarg to compute_K_evals() to thread caches through - Pre-compute patterns in run_nonlinear_solver() via probe evaluation at z_est - Import SparseMatrixCSC, nonzeros, rowvals, nzrange from SparseArrays The sparse pattern is structurally fixed (determined by symbolic derivatives) so the CSC structure (rowval, colptr) never changes — only nzval varies. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Co-Authored-By: Claude Opus 4.6 <[email protected]>
hmzh-khn
pushed a commit
that referenced
this pull request
Feb 19, 2026
…ut PR #152 Co-Authored-By: Claude Opus 4.6 <[email protected]>
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
Pre-compute
SparseMatrixCSCsparsity patterns once per player during solver setup,then reuse them across iterations by updating
nzvalin-place instead of callingsparse(M)each time.Changes
src/MixedHierarchyGames.jl: ImportSparseMatrixCSC,nonzeros,rowvals,nzrangefrom SparseArrayssrc/nonlinear_kkt.jl:_update_sparse_from_dense!()helper for zero-alloc nzval updatesparse_cachekwarg to_solve_K!()to use cached sparsity patternssparse_M_cacheskwarg tocompute_K_evals()to thread caches throughrun_nonlinear_solver()via probe evaluation at initialz_esttest/test_sparse_pattern_cache.jl: New test file (12 tests) verifying:sparse()(27x reduction: 1311→47 bytes/call)test/test_tiers.jl: Add new test file to slow test tiertest/test_test_tiers.jl: Update test tier self-test expectationsBenchmark Results
3-player chain (T=3, s=2, c=2), 20 iterations:
Sparse construction allocations (isolated):
sparse(M): 1311 bytes/callTesting
julia --project=. -e 'using Pkg; Pkg.test()')Changelog
🤖 Generated with Claude Code