Skip to content

CMake: bound the growth of conditions in loops over conditional items - #517

Merged
lefticus merged 4 commits into
cpp-best-practices:mainfrom
leftibot:cmake-memory
Dec 2, 2026
Merged

lefticus merged 4 commits into
cpp-best-practices:mainfrom
leftibot:cmake-memory

Conversation

@leftibot

@leftibot leftibot commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #515 — merge after it; until then this diff includes its commits.

Fixes #469

Reproduced

On curl at fccb17eb, the CMake evaluation alone reached the 4 GB ulimit -v in about 2 minutes, at CMakeLists.txt:2197 (set(_explicit_libs "")), with 10.7M diagram nodes and 14.7M memo entries.

Cause

There were two causes, and the second still blew up once the first was fixed:

  1. Items grew by one per pass. Each pass of foreach(_lib IN LISTS CURL_LIBS ...) sets _explicit_libs to "" under its own condition. The old items were kept as separate "" items under old ∧ ¬when, so the variable gained an item per pass, each item with a "last writer" chain over every later pass.
  2. Exponential diagrams. Requirements are ordered by first use, so every option (CURL_USE_GNUTLS, ...) comes before every TARGET CURL::x. With that order, ∨ (option_j ∧ TARGET_j) and its chains are exponential in the number of libraries. After fix 1, the evaluation got past line 2197 and blew up the same way at line 2285 (set(_prev "${_lib}")), whose value differs on each pass.

Fix

  • The growth itself (Scope.assign, _merged): a set that writes the same text set at the same place as an item it replaces becomes one item, holding in the builds of both. Each build holds only one side's items (the kept ones are restricted to ¬when), so any interleaving that keeps each side's order is the same list in every build. Unmatched items keep the old order: kept first, then added. One side effect, seen across the corpus: fewer duplicate facts (abseil 43,139 → 29,985 CMake facts, the same 222 options), and values that previously passed MOST_VALUES are now kept (libzmq gains its test_xpub_* CTest tests).
  • An unavoidable bound, noted, never silent (conditions.bounded, step): while a build file is evaluated, the condition operations of one command may add at most policy/limits.toml's most_nodes (10,000) nodes. An operation that would go past it returns a requirement of its own, the builds at CMakeLists.txt:2197 (too complex to follow), and the value is kept. The file gets a PartlyRead note: "the builds a value or condition holds in grew too complex to follow; they are kept as not known". Outside a bounded evaluation, nothing changes. Under the bound the memo stays small, so I didn't need to clear it.

Result

The whole curl run (--facts, ulimit -v 6000000) now finishes in 2 min with 596 MB RSS and exit 0. The CMake evaluation alone takes 11 s and 431 MB. It has one note, at CMakeLists.txt:2197.

Corpus

I evaluated the CMake of all 49 other CMake projects in the corpus, with this branch's code and with the branch below it, and compared the facts:

  • none reaches the bound (no notes);
  • evaluation times stay about the same (within noise on a loaded machine);
  • facts differ only by how conditions are split into Applies, because merged items are written as one condition. The builds are the same. abseil, nlohmann_json, pybind11 and libzmq change as described above.

Tests

  • tests/test_conditions.py BoundTests: an exponential ∨(A_i ∧ B_i) under a small bound becomes the place's requirement and is counted; outside a bound, and within it, it is exact.
  • tests/test_cmake.py ConditionGrowthTests: a value set again on each pass stays one item (this test fails without the merge); curl's loop over CURL_LIBS (its real lines, 9 dependencies) is followed with no unknown; under a tiny bound the options are kept, the file is noted (PartlyRead), nothing is unknown, and the requirements name the place.
  • tests/test_policy.py: limits.toml matches conditions.MOST_NODES.

No existing test or snapshot changed.

For the maintainer

  • most_nodes is in a new policy table, limits.toml, as the stack rules ask. The other evaluation limits (MOST_ITERATIONS, MOST_COMMANDS, MOST_VALUES, MOST_CHOICES) are still constants in code, and limits.py holds the nesting limits. Whether all of these belong together is your call.
  • 10,000 nodes per command is a guess with a wide margin: nothing in the corpus but curl comes near it.

Gates

gates2.sh all passed: ruff, ruff format, mypy, lizard, 3482 tests, coverage 99%, jscpd, markdownlint, strict self-check.

Limits in policy/limits.toml

The maintainer asked that no limit be hard-coded (comment). Beside this PR's conditions.most_nodes, these moved into policy/limits.toml with their values unchanged:

  • conditions: most_disjuncts, most_choices
  • nesting: every depth in limits.py. That is CMake calls; GitLab includes, extends and references; .bazelrc imports; Azure templates; make includes; reusable workflows; composite actions; tsconfig extends; GitHub expressions; shell substitutions, compounds and quoting; JSON.
  • cmake: most_iterations, most_commands, most_calls, most_values
  • meson: most_iterations, most_statements
  • files: most_bytes (the --max-file-size default), most_parsed_bytes (data files), most_decompressed, license_head
  • yaml: deepest_place, most_places

The PRs above this one did the same for their own limits: #522 moved automake's most_references, and #525 moved shell's loop_passes and function_calls.

These stay in code because they are not limits on how much a reader reads: display counts, network timeouts, the platform-policy minimums, heuristics (the license note and name sizes, a pin's pattern length) and cabal's brace indent. test_policy's LimitsTableTests, which this PR added, now checks the whole table against each reader that uses it. I rewrote it to do that.

Coverage

Added test_conditions's test_a_step_outside_any_bound_sets_none, which covers the unbounded branch of conditions.step(). Patch coverage locally: 122/122 lines and branches for this PR's own diff, 138/138 for the stack against main.

🤖 Generated with Claude Code

https://claude.ai/code/session_01ByTXNqDWdM7HzezrFtEvQt

@lefticus

lefticus commented Dec 1, 2026

Copy link
Copy Markdown
Member

Don't hard code limits, go ahead and do the refactor to put the others in the toml file

@codecov

codecov Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.44%. Comparing base (e4b03c0) to head (3dbe391).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #517   +/-   ##
=======================================
  Coverage   99.44%   99.44%           
=======================================
  Files         374      374           
  Lines       27250    27339   +89     
  Branches     3429     3440   +11     
=======================================
+ Hits        27099    27188   +89     
  Misses        151      151           

☔ 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.

leftibot and others added 4 commits December 1, 2026 14:39
… a line says so

data-wellformed failed files that are not data until they are rendered:
redis/docker-library-redis's Dockerfile.template (a jq template) and
trailofbits/algo's files/cloud-init/base.yml (a Jinja2 template Ansible's
lookup('template', ...) renders). A file that does not parse is now noted as
a template, not failed, when a word of its name after the first says so
(`Dockerfile.template`, `values.tpl.yaml`, `*.j2`) or when a template block
opens one of its lines (`{%` or `{{` after its indentation). A file that
parses is data whatever its name (CloudFormation's `*.template.json`). The
words and openers are a new policy table, `policy/templates.toml`.

Part of cpp-best-practices#473

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ByTXNqDWdM7HzezrFtEvQt
On curl, evaluating CMakeLists.txt exhausted memory: the loop that writes
libcurl.pc sets `_explicit_libs` again for each of CURL_LIBS' conditional
items, so the variable gained an item per pass, each with a "last writer"
condition over every pass after it, and with the requirements named in a bad
order (each `TARGET CURL::x` after every option) the diagrams grew
exponentially.

- A variable set again to the same value where it was set before is one item,
  in the builds of both sides: each build holds one side's items alone, so
  merging changes nothing a build sees, and the condition no longer grows
  with every pass.
- A build file's evaluation is bounded: one command's operations on conditions
  add at most `policy/limits.toml`'s `most_nodes` (10000) nodes. Past it a
  condition is a requirement of its own, "the builds at CMakeLists.txt:2197
  (too complex to follow)", the value is kept, and the file is noted as read
  in part (a PartlyRead fact), never dropped.

curl's whole run now takes about two minutes and 600 MB under `ulimit -v 6000000`.

Fixes cpp-best-practices#469

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ByTXNqDWdM7HzezrFtEvQt
The maintainer asked that no limit be hard-coded. Beside the CMake
conditions' most_nodes, limits.toml now holds, with unchanged values:
the conditions' most_disjuncts and most_choices; every nesting depth of
limits.py (CMake calls, GitLab includes/extends/references, .bazelrc
imports, Azure templates, make includes, reusable workflows, composite
actions, tsconfig extends, GitHub expressions, shell substitutions,
compounds and quoting, JSON); the CMake evaluation's most_iterations,
most_commands, most_calls and most_values; Meson's most_iterations and
most_statements; the file sizes read (--max-file-size's default, data
files parsed, archives decompressed, a license file's head); and the YAML
places recorded. test_policy checks the table against each reader.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ByTXNqDWdM7HzezrFtEvQt
@lefticus
lefticus merged commit 171273b into cpp-best-practices:main Dec 2, 2026
34 of 35 checks passed
@lefticus
lefticus deleted the cmake-memory branch December 2, 2026 01:50
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.

CMake evaluation exhausts memory on curl: condition diagrams grow without bound in a foreach over conditional list items

2 participants