Skip to content

Test modules keep cached tax-benefit systems and simulations for the rest of the test run #2357

Open

Description

@MaxGhenis

Five Python test modules cache tax-benefit systems or simulations in module-level functools.lru_caches that are never cleared, so everything they hold stays alive for the rest of the pytest worker's run:

Module Cache Holds
test_two_child_limit_age_exemption_properties.py tax_benefit_system(parameter, value), maxsize=None one fresh CountryTaxBenefitSystem plus a reform per (parameter, value) drawn: up to 21
test_cliff_evaluated_properties.py tax_benefit_system(mtr_adults), maxsize=None 4 reforms, each over its own fresh system
test_state_pension_age.py differential_simulation(), situation_template() a ~10,000-person simulation with every year it calculated, and a template simulation
test_state_pension_credit_qualifying_age.py its own copy of differential_simulation() (pytest imports test_state_pension_age a second time under policyengine_uk.tests.), one_person_units_template() as above
test_uc_work_related_groups_properties.py qualifying_age_couples() a simulation and the system it built

Each cached system also keeps the last simulation built on it alive: Simulation.__init__ sets tax_benefit_system.simulation = self.

On CI run 38165445604 (branch mem-measure/2331-run2b, per-worker memprobe artifacts), the worker that ran the two-child-limit file went from 1.10 to 1.65 GiB RSS during that file (13 systems built) and stayed there; malloc_trim after the module did not return it, because the cache still referenced the systems.

Goal: release this memory when each module finishes, and hold less while it runs, without changing which tests run or their outcomes.

Related: #2330, #2331.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions