refactor(toolkit-lib): compute each resource's property hash once per refactor - #1827
Open
Adityaj0 wants to merge 1 commit into
Open
refactor(toolkit-lib): compute each resource's property hash once per refactor#1827Adityaj0 wants to merge 1 commit into
Adityaj0 wants to merge 1 commit into
Conversation
Adityaj0
requested a deployment
to
integ-approval
August 12, 2026 08:33 — with
GitHub Actions
Waiting
aws-cdk-automation
enabled auto-merge
August 12, 2026 08:33
… refactor A `RefactoringContext` computes digests four times for one environment: `resourceMoves` does the deployed and the local stacks in the 'direct' direction, and `structuralOverrides` repeats both in the 'opposite' direction. Only the dependency part of a digest depends on the direction. The other input — the hash of the resource's own reference-stripped properties, which is where nearly all the work is — is identical in all four passes, and is recomputed from scratch every time. Compute it once per resource and share it across the four passes, via a cache owned by the RefactoringContext and threaded through the call chain. The cache is scoped to a single operation rather than held in module state, so it cannot outlive the templates it was built from. Digests are unchanged; only how often they are computed changes. Measured on synthetic CDK-shaped templates (cold process, min of 7): 33-38% off the digest phase of a refactor. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
auto-merge was automatically disabled
August 12, 2026 08:36
Head branch was pushed to by a user without write access
Adityaj0
force-pushed
the
perf/refactor-digest-reuse-property-hashes
branch
from
August 12, 2026 08:36
cac3b14 to
9e4f3fa
Compare
Adityaj0
requested a deployment
to
integ-approval
August 12, 2026 08:37 — with
GitHub Actions
Waiting
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.
Fixes #1824
Reason for this change
A
RefactoringContextcomputes digests four times for one environment:resourceMoveshandles the deployed and the local stacks in the'direct'direction, thenstructuralOverridesrepeats both in the'opposite'direction.Only the dependency half of a digest depends on the direction. The other input —
hashObject(stripReferences(stripConstructPath(resource), exports)), which is where nearly all the CPU goes because it walks and hashes the resource's whole property tree — is a pure function of the resource and the stacks' exports, and is byte-identical in every pass. It is recomputed from scratch in each of them.Description of changes
Added an optional
PropertyHashCache(aMapkeyed by the resource object) tocomputeResourceDigests, consulted before hashing and populated after.RefactoringContextcreates one in its constructor and threads it throughresourceMoves,structuralOverridesandresourceDigests, so all four passes share it.Two deliberate choices:
Map. Every existing caller ofcomputeResourceDigests— including the tests — behaves exactly as before without changes.Digests are unchanged; only how often they are computed changes.
Description of how you validated changes
New regression test asserting that digests computed with a cache shared across both graph directions are identical to digests computed without one, for both directions.
All 55 tests under
test/api/refactoring/pass.Benchmark — synthetic CDK-shaped templates (nested policy documents, tags,
Ref/Fn::GetAttcross-references,DependsOn,aws:cdk:pathon every resource), timing exactly the fourcomputeResourceDigestscalls aRefactoringContextperforms. Cold node process per measurement, min of 7 runs:Each measurement is a single cold execution, so the cache starts empty exactly as in a real CLI invocation: two passes fill it, two read from it. Timing repeated iterations inside one process would report ~80%, but only because the cache is already warm by then — that number would not reflect what a user sees.
Note: #1823 covers the deep clone in
stripConstructPath, a separate ~20-25% in the same code path. The two changes are independent and compose.Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license