Synthesis Performance Analysis
Current Performance Bottlenecks
Profiling Results (10,000 resources)
- Synthesis time: 7.63 seconds (1,310 resources/sec)
- Major time consumers:
isinstancechecks: 5.3s (69% of synthesis time)- Resource hashing: 5.1s
- Cross-stack pre-scanning: ~2s
- JSON serialization: 0.14s (surprisingly low)
Duplicate Passes Identified
- Pre-scan Phase (
_pre_scan_cross_stack_references): - Calls
resource._to_config()for EVERY resource (10,000 times) - Recursively walks entire config tree looking for runtime values
-
This is a complete pass through all resources before synthesis even starts
-
First Synthesis Pass (
_to_config_with_cache): - Iterates through all resources again
- Calls
_get_resource_config_cached()which:- Computes hash for each resource (
compute_resource_hash) - Calls
resource._to_config()again if cache miss
- Computes hash for each resource (
-
Even with caching, hash computation is expensive
-
Resolution Pass (
resolve_value): - Recursively walks entire config tree
-
Resolves runtime values
-
Potential Second Pass:
- If cross-stack references found, entire process repeats
- Another full config build + resolution
Performance Issues
- Expensive Type Checking:
isinstance()calls taking 5.3s (69% of time)- Coming from
serialize_for_hash()which uses isinstance heavily -
Called 2.9M times for 10,000 resources
-
Redundant Config Generation:
-
resource._to_config()called multiple times per resource:- Once in pre-scan
- Once in synthesis (if cache miss)
- Potentially again if cross-stack refs found
-
Expensive Hashing:
compute_resource_hash()called 22,002 times for 10,000 resources- Each hash computation serializes entire resource state
-
serialize_for_hash()is recursive and uses expensive type checks -
Inefficient Caching:
- Cache checks require computing hash first
- Hash computation is almost as expensive as config generation
- Cache misses result in redundant work
Optimization Opportunities
1. Eliminate Pre-scan Phase
- Detect cross-stack references during initial config build
- Save ~2 seconds and 10,000
_to_config()calls
2. Optimize Type Checking
- Cache type check results
- Use faster type discrimination (e.g.,
__class__checks) - Batch similar types together
3. Lazy Hashing
- Only compute hashes when resources actually change
- Use dirty flags instead of hash comparison
- Hash only changed attributes, not entire resource
4. Single-pass Synthesis
- Build config and detect cross-stack refs in one pass
- Incrementally update config for cross-stack refs
- Avoid complete rebuild
5. Optimize serialize_for_hash
- Remove expensive isinstance checks
- Use type-specific serializers
- Avoid circular reference checking for immutable types
Expected Improvements
With these optimizations, we should achieve: - Target: 10,000+ resources/sec - Reduction: From 4-5 passes to 1-2 passes - Type checking: 90% reduction in isinstance calls - Hashing: 80% reduction in hash computations