Synthesis Performance Optimization Results
Executive Summary
Through a series of targeted optimizations, we improved netherframe's synthesis performance from 2,989 resources/second to 66,672 resources/second - a 22.3x improvement.
Optimization Timeline
Baseline Performance
- Initial: 2,989 resources/second (10,000 resources in 3.35s)
- Bottlenecks: Hash computation, cross-stack scanning, excessive function calls
Optimization 1: Forward-Store Cross-Stack References
- Implementation: Track cross-stack references at creation time instead of scanning
- Result: 12,838 r/s → 4.3x improvement
- Impact: Eliminated 514,000 recursive scans
Optimization 2: Primitive Type Checking
- Implementation: Early return for primitive types in resolve_value
- Result: 25,445 r/s → 8.5x cumulative improvement
- Impact: Reduced hasattr() calls by 49%
Optimization 3: Skip Hash for Performance Mode
- Implementation: Added App parameter to skip hash computation
- Result: 28,804 r/s → 9.6x cumulative improvement
- Impact: Eliminated 381ms of hash computation
Optimization 4: Module-Level Constants
- Implementation: Compute UNALLOWED_NAMES once at module level
- Result: 58,739 r/s → 19.6x cumulative improvement
- Impact: Saved ~0.1ms per resource in _to_config()
Optimization 5: Provider Code Generation
- Implementation: Applied _to_config optimization to all generated providers
- Result: 66,672 r/s → 22.3x cumulative improvement
- Impact: Optimized synthesize_hcl_attributes across thousands of resource types
Final Performance Metrics
Resource creation: 0.43 seconds (23,256 resources/second)
Synthesis: 0.15 seconds (66,672 resources/second)
Total: 0.58 seconds (17,271 resources/second overall)
Key Optimizations Applied
- Forward-store pattern for cross-stack reference tracking
- Primitive type checking with frozenset for O(1) lookups
- Skip hash computation for one-shot synthesis scenarios
- Module-level constants to avoid repeated computation
- Code generation updates to propagate optimizations
Bottlenecks Addressed
| Component | Before | After | Improvement |
|---|---|---|---|
| Cross-stack scanning | 514k calls | 0 calls | Eliminated |
| Hash computation | 381ms | 0ms (skipped) | 100% |
| hasattr() calls | 1.5M | ~765k | 49% reduction |
| _to_config() | 10.0ms/1k | 5.8ms/1k | 42% faster |
| Overall synthesis | 3.35s | 0.15s | 95.5% reduction |
Lessons Learned
- Profile first: cProfile revealed unexpected bottlenecks
- Forward is faster: Tracking state at creation beats scanning later
- Module constants matter: Even small repeated computations add up
- Generated code matters: Optimizations must extend to generated files
- Skip when possible: Optional features (like hashing) should be skippable
Next Steps
Potential areas for further optimization: - Streaming JSON serialization for very large configurations - Parallel resource synthesis for multi-stack applications - Memory pool for resource allocation - JIT compilation for hot paths