Final Optimization Summary
Journey from 2,989 to 28,422 Resources/Second
1. Initial Baseline: 2,989 r/s
- Recursive resolve_value() on every resource
- Deep checking of all values
2. First Optimizations: 4,843 r/s (62% improvement)
- Eliminated pre-scan phase
- Optimized type checking
- Lazy hashing
- Version-based caching
3. Forward-Store Pattern: 24,893 r/s (833% total improvement)
- Eliminated 514,000 recursive scans
- Forward-stored cross-stack references when created
- No scanning at synthesis time
4. Primitive Type Optimization: 26,031 r/s (871% total improvement)
- Added _PRIMITIVE_TYPES set for early returns
- Reduced hasattr() calls by 49%
- Skip checking None, str, int, etc.
5. Forward Resolution Signaling: 28,422 r/s (951% total improvement)
- Only resolve resources that contain RuntimeValues
- Skip resolution for ~50% of resources (primitives only)
- 9.2% improvement over previous best
Key Insights
- Avoid Recursive Checking: Every level of recursion multiplies function calls
- Early Returns for Primitives: Most config values are strings, bools, None
- Forward Signaling: Track state when created, not when used
- Selective Resolution: Only process what needs processing
Architecture Changes
Before
# Every resource, every value, recursively
for resource in resources:
resolved = resolve_value(resource.config) # 514k calls!
After
# Only resources with RuntimeValues, no recursion
for resource in resources:
if resource._needs_resolution: # Set when RuntimeValue assigned
resolved = resolve_value(resource.config)
- 10,000 resources synthesis: 0.352s (was 3.34s)
- Resources/second: 28,422 (was 2,989)
- Total improvement: 951%
- Bottleneck removed: Resolution overhead
Remaining Opportunities
- Logging Overhead (41.5% of time) - Could disable during synthesis
- JSON Serialization - Could optimize with faster library
- Parallel Synthesis - Multiple stacks could synthesize concurrently