Skip to content

Synthesis Performance Analysis

Current Performance Bottlenecks

Profiling Results (10,000 resources)

  • Synthesis time: 7.63 seconds (1,310 resources/sec)
  • Major time consumers:
  • isinstance checks: 5.3s (69% of synthesis time)
  • Resource hashing: 5.1s
  • Cross-stack pre-scanning: ~2s
  • JSON serialization: 0.14s (surprisingly low)

Duplicate Passes Identified

  1. Pre-scan Phase (_pre_scan_cross_stack_references):
  2. Calls resource._to_config() for EVERY resource (10,000 times)
  3. Recursively walks entire config tree looking for runtime values
  4. This is a complete pass through all resources before synthesis even starts

  5. First Synthesis Pass (_to_config_with_cache):

  6. Iterates through all resources again
  7. Calls _get_resource_config_cached() which:
    • Computes hash for each resource (compute_resource_hash)
    • Calls resource._to_config() again if cache miss
  8. Even with caching, hash computation is expensive

  9. Resolution Pass (resolve_value):

  10. Recursively walks entire config tree
  11. Resolves runtime values

  12. Potential Second Pass:

  13. If cross-stack references found, entire process repeats
  14. Another full config build + resolution

Performance Issues

  1. Expensive Type Checking:
  2. isinstance() calls taking 5.3s (69% of time)
  3. Coming from serialize_for_hash() which uses isinstance heavily
  4. Called 2.9M times for 10,000 resources

  5. Redundant Config Generation:

  6. 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
  7. Expensive Hashing:

  8. compute_resource_hash() called 22,002 times for 10,000 resources
  9. Each hash computation serializes entire resource state
  10. serialize_for_hash() is recursive and uses expensive type checks

  11. Inefficient Caching:

  12. Cache checks require computing hash first
  13. Hash computation is almost as expensive as config generation
  14. 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