Skip to content

fix(conditions): avoid exponential CNF when registering conditions - #4742

Merged
kddejong merged 3 commits into
aws-cloudformation:mainfrom
moneytool:perf/conditions-cnf-blowup
Oct 20, 2026
Merged

kddejong merged 3 commits into
aws-cloudformation:mainfrom
moneytool:perf/conditions-cnf-blowup

Conversation

@moneytool

Copy link
Copy Markdown
Contributor

Issue #, if available: #4737

Description of changes:

Both condition engines register each named condition as Symbol(name) <-> expression by handing the whole equivalence to EncodedCNF.add_prop. sympy converts that to clauses by distributing And over Or, so an Fn::Or of n Fn::And pairs becomes 2^n clauses. Every later copy()/satisfiable() then works on that clause set, and since #4455 this happens at template load whether or not the condition is used.

This adds add_equivalence() in cfnlint/conditions/_utils.py and uses it in both conditions/conditions.py and context/conditions/_conditions.py. It Tseitin-encodes the expression, which keeps the clause count linear in the size of the expression:

  • Each nested And/Or subterm gets an auxiliary variable defined by a full equivalence.
  • The condition symbol itself is used as the variable of the outermost And/Or. One-level conditions, the common case, therefore encode to exactly the same clauses as before.

Why results are unchanged: auxiliary variables are fully determined by the original variables. Later add_prop calls only reference condition symbols and equals hashes. No caller inspects models. So every satisfiable query behind build_scenarios, check_implies, build_scenerios_on_region, evolve and is_reachable returns the same answer.

Timings (cfn-lint wall clock):

Template Before After
t20.json from the issue 68.3s 0.70s
20 pairs, condition unused 37.3s 0.58s
40 pairs (2^40 clauses) 0.65s

@hoganbr-amzn independently tried the same per-node encoding and reported that the 421 KB template that surfaced the bug went from more than an hour to 12s, with the same findings.

Testing:

  • TestAddEquivalence compares the encoding against direct Equivalent over every assignment for a set of And/Or/Not/Implies/Equivalent/Xor expressions. It also bounds the clause count for an Or of 40 Ands and checks that a single-level And/Or adds no auxiliary variable.
  • A 30-pair regression test for each condition engine asserts a linear clause bound plus scenario, implication and reachability results. These tests time out on main.
  • Differential check against main: 600 randomly generated condition templates gave identical results for build_scenarios, check_implies, build_scenerios_on_region, satisfiable, evolve and is_reachable. Full cfn-lint output over test/fixtures/templates/good and bad was identical.
  • Full pytest test passes. Snapshot and rule test time is unchanged.

Out of scope: template Rules assertions still go through add_prop directly, so an Or-of-And inside a rule assertion can still expand. That could be a follow-up.

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

Both condition engines added `Symbol(name) <-> expression` to the
EncodedCNF directly, and sympy converts that by distributing And over
Or. An Fn::Or of n Fn::And pairs became 2^n clauses, so a 20-pair
condition took over a minute to lint.

Encode the equivalence with Tseitin-style auxiliary variables instead:
each And/Or subterm gets its own variable defined by a full
equivalence, keeping the clause count linear. The auxiliary variables
are fully determined by the original ones, so satisfiability and
implication queries over condition and equals symbols are unchanged.

Fixes aws-cloudformation#4737
add_equivalence gave every And/Or subterm a fresh Dummy, including the
outermost one, and then tied the condition symbol to it with a two
clause Equivalent. For the common one-level condition (an Fn::And or
Fn::Or of equals) that is one extra variable and two extra clauses per
condition, which showed up as ~25% more time in build_scenarios on
condition-heavy templates. Use the condition symbol itself as the
variable of the outermost And/Or so such conditions encode to exactly
the clauses Equivalent produced before.
@codecov

codecov Bot commented Oct 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.17647% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.71%. Comparing base (1bb5e59) to head (4c8ac3b).

Files with missing lines Patch % Lines
src/cfnlint/conditions/_utils.py 89.65% 1 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4742      +/-   ##
==========================================
- Coverage   94.72%   94.71%   -0.01%     
==========================================
  Files         439      439              
  Lines       15858    15886      +28     
  Branches     3067     3076       +9     
==========================================
+ Hits        15021    15046      +25     
- Misses        457      458       +1     
- Partials      380      382       +2     
Flag Coverage Δ
unittests 94.71% <91.17%> (-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 fd17da4 into aws-cloudformation:main Oct 20, 2026
20 of 21 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.

2 participants