Repository navigation
CMake: bound the growth of conditions in loops over conditional items - #517
Merged
Merged
Conversation
This was referenced Dec 1, 2026
Member
|
Don't hard code limits, go ahead and do the refactor to put the others in the toml file |
leftibot
force-pushed
the
cmake-memory
branch
from
December 1, 2026 15:27
f0c7b08 to
4237e1d
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
leftibot
force-pushed
the
cmake-memory
branch
from
December 1, 2026 19:49
e63bf0a to
d21dcb7
Compare
… 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
…est-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
leftibot
force-pushed
the
cmake-memory
branch
from
December 1, 2026 22:22
d21dcb7 to
3dbe391
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 GBulimit -vin about 2 minutes, atCMakeLists.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:
foreach(_lib IN LISTS CURL_LIBS ...)sets_explicit_libsto""under its own condition. The old items were kept as separate""items underold ∧ ¬when, so the variable gained an item per pass, each item with a "last writer" chain over every later pass.CURL_USE_GNUTLS, ...) comes before everyTARGET 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
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 passedMOST_VALUESare now kept (libzmq gains itstest_xpub_*CTest tests).conditions.bounded,step): while a build file is evaluated, the condition operations of one command may add at mostpolicy/limits.toml'smost_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 aPartlyReadnote: "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, atCMakeLists.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:
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.pyBoundTests: 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.pyConditionGrowthTests: a value set again on each pass stays one item (this test fails without the merge); curl's loop overCURL_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.tomlmatchesconditions.MOST_NODES.No existing test or snapshot changed.
For the maintainer
most_nodesis 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, andlimits.pyholds the nesting limits. Whether all of these belong together is your call.Gates
gates2.shall 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 intopolicy/limits.tomlwith their values unchanged:conditions:most_disjuncts,most_choicesnesting: every depth inlimits.py. That is CMake calls; GitLab includes, extends and references;.bazelrcimports; 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_valuesmeson:most_iterations,most_statementsfiles:most_bytes(the--max-file-sizedefault),most_parsed_bytes(data files),most_decompressed,license_headyaml:deepest_place,most_placesThe PRs above this one did the same for their own limits: #522 moved automake's
most_references, and #525 moved shell'sloop_passesandfunction_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'sLimitsTableTests, 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'stest_a_step_outside_any_bound_sets_none, which covers the unbounded branch ofconditions.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