Skip to content

Perf #7: Investigate Dict{Int, Any} type stability in KKT construction#157

Open
hmzh-khn wants to merge 4 commits into
perf/06-typed-function-vectorsfrom
perf/07-typed-dicts
Open

Perf #7: Investigate Dict{Int, Any} type stability in KKT construction#157
hmzh-khn wants to merge 4 commits into
perf/06-typed-function-vectorsfrom
perf/07-typed-dicts

Conversation

@hmzh-khn

Copy link
Copy Markdown
Collaborator

Summary

Investigation PR for Dict{Int, Any} type instability in KKT construction (qp_kkt.jl lines 92-95, nonlinear_kkt.jl line 256).

Key finding: All Dict{Int, Any} instances are COLD PATH only. They are used during one-time symbolic construction and never accessed in the Newton iteration loop. The hot-path containers were already correctly typed from previous PRs (Perf #4-6):

  • Vector{Union{Matrix{Float64}, Nothing}} for K/M/N evals
  • Dict{Int, Matrix{Float64}} for M/N buffers
  • Vector{MNFunctionWrapper} for compiled M/N functions
  • Vector{Int} for π_sizes

Changes

  • src/nonlinear_kkt.jl: Add explicit Dict{Int, Int} type annotation on π_sizes_trimmed (self-documenting, Julia already inferred correctly)
  • test/test_typed_kkt_dicts.jl: 24 new regression tests documenting hot-path vs cold-path type guarantees
  • test/test_tiers.jl: Register new test in slow tier
  • test/test_test_tiers.jl: Update expected file lists
  • scripts/benchmark_typed_kkt_dicts.jl: Benchmark script for typed dict access
  • RETROSPECTIVES.md: PR retrospective

Testing

  • All 1405 tests pass (julia --project=. -e 'using Pkg; Pkg.test()')
  • No test tolerance changes
  • No public API changes

Investigation Details

Call chain trace: solve()run_nonlinear_solver()compute_K_evals() (hot path)

Dict Location Path Value Type Action
πs qp_kkt.jl:92, nonlinear_kkt.jl:256 Cold BlockVector/Vector{Num} (heterogeneous) Leave as Dict{Int, Any}
Ms qp_kkt.jl:93 Cold Symbolic Matrix Leave as Dict{Int, Any}
Ns qp_kkt.jl:94 Cold Symbolic Matrix Leave as Dict{Int, Any}
Ks qp_kkt.jl:95 Cold Symbolic Matrix Leave as Dict{Int, Any}
π_sizes_trimmed nonlinear_kkt.jl:476 Cold Int Add explicit Dict{Int, Int}
M_buffers nonlinear_kkt.jl:896 Hot Matrix{Float64} Already typed ✓
N_buffers nonlinear_kkt.jl:897 Hot Matrix{Float64} Already typed ✓
K_evals nonlinear_kkt.jl:649 Hot Union{Matrix{Float64}, Nothing} Already typed (Vector) ✓

Changelog

  • 2026-02-16: Initial PR with investigation findings, regression tests, benchmarks

🤖 Generated with Claude Code

claude and others added 4 commits February 16, 2026 22:51
Tests verify that:
- Hot-path containers (K_evals, M_evals, N_evals, M_buffers, N_buffers)
  use concrete value types for type-stable access during Newton iterations
- Cold-path dicts (πs, Ms, Ns, Ks) intentionally use Dict{Int, Any}
  for heterogeneous symbolic types (BlockVector vs Vector{Num})
- π_sizes_trimmed is Dict{Int, Int} (not Any)
- Pre-allocated k_eval_buffers are reused (identity check)
- End-to-end solve produces correct results with all typed containers

These tests protect against regressions if someone "improves" the cold-path
dicts or accidentally introduces Any-typed containers on the hot path.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
- Add explicit type annotation on π_sizes_trimmed in preoptimize_nonlinear_solver
  for self-documentation (Julia already inferred this correctly)
- Register test_typed_kkt_dicts.jl in slow test tier
- Update test_test_tiers.jl expected file lists

Investigation finding: All Dict{Int, Any} instances in qp_kkt.jl (lines 92-95)
and nonlinear_kkt.jl (line 256) are COLD PATH only — used during one-time
symbolic construction, never accessed in the Newton iteration loop. The hot-path
containers (K_evals, M_evals, M_buffers, etc.) already use concrete types
(Vector{Union{Matrix{Float64}, Nothing}}, Dict{Int, Matrix{Float64}}).

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Benchmarks verify that hot-path dict access (Dict{Int, Matrix{Float64}})
is not a bottleneck. Includes micro-benchmark comparing typed vs untyped
dict access, and full solve / compute_K_evals benchmarks.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Key finding: All Dict{Int, Any} in KKT construction are cold-path only.
Hot-path containers were already well-typed from previous PRs.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants