Repository navigation
Avoid eager Holder allocation in input_variables discovery - #663
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #663 +/- ##
===========================================
+ Coverage 84.69% 94.79% +10.09%
===========================================
Files 223 354 +131
Lines 11830 33228 +21398
Branches 1169 2887 +1718
===========================================
+ Hits 10019 31497 +21478
+ Misses 1496 1351 -145
- Partials 315 380 +65 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Thanks for tracking this down, Deepak. The diagnosis in #662 is right: building I'd like to change how the fix gets there. Core already records inputs as they are stored in This would also address two problems with the current version:
In recorded_input_names = {
name for name, _branch_name, _period in simulation._user_input_keys
}
simulation.input_variables = [
name
for name in simulation.tax_benefit_system.variables
if name in recorded_input_names
]In recorded_input_names = {
name for name, _branch_name, _period in self._user_input_keys
}
if populations is not None:
# Populations passed in may already hold values this simulation
# did not record.
recorded_input_names.update(
name
for population in self.populations.values()
for name, holder in population._holders.items()
if len(holder.get_known_periods()) > 0
)
# A reform may have moved a recorded variable to another entity, leaving
# its value in a holder this simulation no longer reads.
self.input_variables = [
name
for name, variable in self.tax_benefit_system.variables.items()
if name in recorded_input_names
and name in self.populations[variable.entity.key]._holders
]With this, I tried this on a copy of current
I haven't run the Core or Could you:
|
|
Thanks for catching the I've updated the PR as requested:
|
|
Thanks, Deepak. The rework covers everything I asked for, and the benchmark results are what we were hoping to see. I'd recommend three changes before merge. The first fixes a problem in the snippet I suggested. 1. Handle simulations that have no input record yet. Simulation(situation={
"people": {"adult": {}},
"benunits": {"benunit": {"members": ["adult"]}},
"households": {"household": {"members": ["adult"]}},
})The same happens when a person only requests an output, such as # A subclass that replaces ``__init__`` has no record until an input is stored.
recorded_input_names = {
name
for name, _branch_name, _period in getattr(simulation, "_user_input_keys", ())
}We plan to remove this tolerance once country packages use Core's constructor. 2. Add a regression test. Please cover 3. Update the changelog. Please mention that The source change here follows code I suggested, so this is partly a self-review. I compared this version with |
|
Thanks for catching the missing
Re-ran |
Fixes #662.
Summary
SimulationBuilder.build_from_dict()andSimulation.__init__()to buildinput_variablesfrom_user_input_keys(usinggetattr(simulation, "_user_input_keys", ())inbuild_from_dictfor subclasses that replace__init__, and filtering againsttax_benefit_system.variablesto preserve variable registration order) instead of callingget_holder(variable.name)on every variable in the system.populationsis passed directly toSimulation.__init__(), existing non-empty holders are included without allocating new holders; variables whose entity changed under a reform are excluded vianame in self.populations[variable.entity.key]._holders.Simulationinitialization eagerly allocated emptyHolderobjects for every variable in the system, and added regression tests covering lazy holder allocation, dataset variable order, pre-populatedpopulations=,clone(), reforms removing or moving input variables, andbuild_from_dicton a simulation without_user_input_keys.changelog.d/avoid-eager-holder-allocation.fixed.md(including the effect onmemory_configset after setup).Checks run
uv run --extra dev ruff format --check .(386 files already formatted)uv run --extra dev ruff check .(All checks passed!)uv run policyengine-core test policyengine_core/country_template/tests -c policyengine_core.country_template(39 passed)uv run pytest tests --disable-pytest-warnings -q(3887 passed, 4 skipped, 3 xfailed)policyengine-us:uv run --with-editable /Users/deepakjindal/policyengine-core pytest policyengine_us/tests/core/test_social_security_input_state_machine.py -q(1 passed, run prior to adding thegetattrguard inbuild_from_dict)Checks not run
make mypyandmake documentationwere not run (type-checking and Jupyter Book docs build were outside the scope of this runtime fix).