Avoid macro cache initialization when reads are disabled#489
Merged
Conversation
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
SimulationMacroCacheonly aftercheck_macro_cacheconfirms a macro-cache read is available.Context
This surfaced while investigating elevated household API latency on requests that include Medicaid outputs. The broader Medicaid calculation graph comes from PolicyEngine/policyengine-us#8169, which fixed MAGI Medicaid household construction by moving from a tax-unit proxy to person-specific tax-filer and non-filer rules.
That policyengine-us change is valid model behavior, but it expands the number of variables calculated for Medicaid. In household simulations, macro-cache reads are normally unavailable, yet
Simulation._calculatewas still constructingSimulationMacroCachebefore checking availability. That constructor reads package metadata, so the expanded Medicaid graph turned a small per-variable overhead into a noticeable latency regression.In a local reproduction using the current household API stack and a partner-style payload with Medicaid among requested outputs, warm
/us/calculateendpoint timing was about 2.5s on current core and about 0.36-0.64s with this patch. The change preserves the existing macro-cache read path when a cache is actually available.Testing
uv run pytest tests/core/test_simulations.py::test_calculate_without_macro_cache_does_not_build_macro_cache tests/core/test_simulations.py::test_macro_cache -quv run ruff check policyengine_core/simulations/simulation.py tests/core/test_simulations.py