Skip to content

perf(conditions): solve each set of condition pins once - #4992

Merged
kddejong merged 1 commit into
aws-cloudformation:mainfrom
hoganbr-amzn:perf/conditions-evolve-cache
Nov 5, 2026
Merged

kddejong merged 1 commit into
aws-cloudformation:mainfrom
hoganbr-amzn:perf/conditions-evolve-cache

Conversation

@hoganbr-amzn

Copy link
Copy Markdown
Contributor

Issue #, if available: Fixes #4991

Description of changes:

Conditions.evolve and Conditions.is_reachable copied the template's condition CNF and ran a satisfiability check on every call. A template where many resources use the same conditions in Fn::If asks the same few questions thousands of times: the reproduction in #4991 makes 6,390 checks for 114 distinct sets of pinned conditions.

A Conditions now stores its pins (every pinned condition) and its hard pins (the pins that are facts rather than scenario assumptions) instead of two CNF copies. Satisfiability is cached per set of pins, which fully decides it, in a dict shared by every Conditions evolved from the same template. cnf and hard_cnf become read-only properties that build a new CNF from the template clauses plus one clause per pin; nothing in src/ reads them, and the constructor no longer takes them. This also stops cnf from collecting a duplicate clause for each carried-over pin on every evolve.

Reproduction from #4991 (cfn-lint --regions us-east-1 -- repro.yaml, after cfn-lint --update-specs):

wall time satisfiability checks
main 26.0 s 6,390
this PR 10.2 s 114

Peak RSS is unchanged (about 135-140 MB on both).

The remaining 2,400 solver calls on the reproduction come from build_scenarios in src/cfnlint/conditions/conditions.py, which this PR does not change.

pytest test (3,449 passed) and pre-commit run --all-files pass. The 258 templates under test/fixtures/templates lint to the same matches as main, apart from the order of resource names inside E3019 messages, which already varies with PYTHONHASHSEED on main. The new tests cover repeated, reordered, reachability and unsatisfiable questions, facts versus assumptions with equal statuses, and the one-clause-per-pin CNF. An end-to-end test lints 5 and then 20 conditional alarms that reuse the same conditions in Fn::If and asserts the solver call count does not change: 9 at both sizes here, 65 and 260 on main.

#4756 (open) also changes _conditions.py. Its evolve_for_regions adds a region clause to cnf but not hard_cnf, so that W1028 does not treat region-specific branches as unreachable. With this PR the clause cannot simply join _base_cnf, which sits under the hard pins too. It would need its own base for cnf only, and a separate cache for it, whichever PR lands first.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Conditions.evolve and Conditions.is_reachable copied the template's
condition CNF and ran a satisfiability check on every call. A template
where many resources use the same conditions in Fn::If asks the same few
questions thousands of times.

A Conditions now stores its pins (every pinned condition) and its hard
pins (the pins that are facts rather than scenario assumptions) instead
of two CNF copies. Satisfiability is cached per set of pins, which fully
decides it, in a dict shared by every Conditions evolved from the same
template. cnf and hard_cnf become read-only properties that build a new
CNF from the template clauses plus one clause per pin.

Fixes aws-cloudformation#4991
@codecov

codecov Bot commented Nov 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.81%. Comparing base (c4746e8) to head (4d3227b).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4992   +/-   ##
=======================================
  Coverage   94.80%   94.81%           
=======================================
  Files         441      441           
  Lines       16165    16179   +14     
  Branches     3135     3137    +2     
=======================================
+ Hits        15326    15340   +14     
  Misses        463      463           
  Partials      376      376           
Flag Coverage Δ
unittests 94.80% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@kddejong
kddejong merged commit ccbf4d5 into aws-cloudformation:main Nov 5, 2026
26 checks passed
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.

Most lint time on templates with many Fn::If goes to re-checking the same condition combinations

2 participants