Skip to content

AWS Resource Performance Analysis

Executive Summary

Performance testing of AWS resource creation (specifically S3Bucket) reveals that: - Resource creation is fast: ~0.04ms per resource - Synthesis is the bottleneck: ~0.51ms per resource (12.5x slower than creation) - Memory usage is reasonable: ~12KB per resource - Complex resources have 3x overhead compared to minimal resources

Detailed Findings

1. Performance Breakdown (1000 S3 Buckets)

Phase Time Per Resource % of Total
Import 0.01ms - <0.01%
App/Stack Setup 1.58ms - 0.3%
Resource Creation 40.98ms 0.04ms 7.4%
Synthesis 510.60ms 0.51ms 92.3%
Total 553.16ms 0.55ms 100%

2. Resource Creation Overhead

Comparing minimal vs full resource initialization: - Minimal Resource: 0.01ms per resource - Full S3 Bucket: 0.04ms per resource
- Overhead Factor: 3.0x

The overhead comes from: 1. Multiple attribute assignments (20-30 per resource) 2. Nested dataclass instantiation (Versioning, LifecycleRule, etc.) 3. __setattr__ hooks for config tracking

3. Memory Usage

  • Peak memory: 11.7 MB for 1000 resources
  • Per resource: ~12 KB
  • Main allocations:
  • __setattr__ tracking: 1.5 MB
  • Config dictionaries: 750 KB
  • Object instances: 313 KB

4. Profiling Insights

Top time-consuming operations during resource creation: 1. __setattr__ method calls (called 83 times per S3Bucket) 2. Construct registration and child tracking 3. Logging checks (even when disabled) 4. Dataclass instantiation

Optimization Recommendations

1. Use __slots__ for Memory Efficiency

class Resource:
    __slots__ = ["_config", "_provider", "_resource_type", "_runtime_cache", ...]
- Reduces memory by 40-60% - Faster attribute access - Prevents dynamic attribute addition

2. Batch Attribute Assignment

Instead of:

self.bucket = bucket
self.acl = acl
self.tags = tags
# ... 20 more assignments

Use:

# Single dict update
self._config.update(
    {
        "bucket": bucket,
        "acl": acl,
        "tags": tags,
        # ... all attributes
    }
)

3. Lazy Dataclass Initialization

Replace:

versioning: S3Bucket.Versioning(enabled=True)

With:

versioning: {"enabled": True}  # Convert to dataclass only when needed

4. Cache synthesize_attributes Results

def synthesize_attributes(self):
    if self._synthesized_cache is None:
        self._synthesized_cache = self._compute_attributes()
    return self._synthesized_cache

5. Optimize Synthesis Phase

Since synthesis takes 92% of total time: - Use faster JSON serializer (orjson/ujson) - Batch file writes - Parallel synthesis for independent stacks - Pre-compute resource paths

6. Reduce setattr Overhead

def __init__(self, *args, **kwargs):
    # Bypass __setattr__ during init
    object.__setattr__(self, "_initializing", True)
    # ... assignments ...
    object.__setattr__(self, "_initializing", False)

Implementation Priority

  1. High Impact, Low Effort:
  2. Cache synthesize_attributes (10-20% improvement)
  3. Batch attribute assignments (5-10% improvement)

  4. High Impact, Medium Effort:

  5. Optimize synthesis phase (50-70% improvement)
  6. Use slots (30-40% memory reduction)

  7. Medium Impact, Low Effort:

  8. Lazy dataclass initialization (10-15% improvement)
  9. Reduce setattr calls (5-10% improvement)

Conclusion

The main performance bottleneck is not in resource creation but in the synthesis phase. While optimizing resource initialization will help, the biggest gains will come from optimizing how resources are serialized and written to disk during synthesis.

For applications creating thousands of resources, focus on: 1. Synthesis optimization (JSON serialization, file I/O) 2. Memory efficiency (using slots) 3. Lazy evaluation of complex nested structures