Compute Context.module_names lazily to avoid rescan on every evolve() - #4586
Merged
kddejong merged 3 commits intoJul 23, 2026
Merged
Conversation
Context.__post_init__ rescanned all resources to rebuild module_names on every Context construction. Context.evolve() constructs a new Context on nearly every schema-walk descend, so validation cost grew with nodes x resources (introduced in aws-cloudformation#4510, first released in v1.51.1). On a 250KB template with ~270 resources this is ~84k constructions triggering ~22M str.endswith calls, ~46% of total runtime. Replace the eager field with functools.cached_property so the tuple is computed at most once per instance, and only when actually read (the two readers only consult it at specific nodes, so most instances never compute it at all). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Asserts that Context construction and evolve() do not scan resources, and that module_names is computed on first read and cached per instance. Fails against the eager __post_init__ implementation. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
kddejong
approved these changes
Jul 23, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4586 +/- ##
==========================================
- Coverage 94.43% 94.42% -0.01%
==========================================
Files 429 429
Lines 15099 15099
Branches 2916 2916
==========================================
- Hits 14258 14257 -1
Misses 461 461
- Partials 380 381 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This was referenced Jul 24, 2026
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.
Issue #, if available: #4585
Description of changes:
Context.__post_init__rebuiltmodule_nameson everyContextconstruction — including allevolve()clones created during the schema walk — making validation O(nodes × resources) on every template (#4510, first released in 1.51.1). See the linked issue for profiles: on a ~250 KB template with ~270 resources this is ~84k constructions triggering ~22.4Mstr.endswithcalls, ~46% of total runtime.This change replaces the eager field with
functools.cached_property, so the tuple is computed at most once per instance and only when actually read. Both readers (dynamicValidationin_keywords_cfn.pyandGetAtt) consult it only at specific nodes, so most instances never compute it at all.cached_propertyworks on the frozen dataclass because it writes to the instance__dict__directly, bypassing the frozen__setattr__.Behavior note:
module_namesis no longer a dataclass field, so it is not part of the generated__eq__/__repr__. It is fully derived fromresources(which is a field), and nothing in the codebase introspects it as a field.Benchmarks (Apple M-series, warm): synthetic 400-resource template 4.8s → 3.7s (pre-regression 1.51.0 baseline: 3.4s); a fleet of 66 large (~250 KB) generated templates 3m55s → 2m42s. Unit test suite and pre-commit hooks pass.
Prepared with AI assistance (Claude Code); human-reviewed before submission.
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.