Forward Resolution Signaling Design
Current Problem
The current implementation calls resolve_value() on EVERY resource config during synthesis, which:
- Recursively checks all values (dicts, lists, nested objects)
- Calls hasattr() on every value to check for _is_runtime_value
- Wastes ~514,000 function calls for 10,000 resources
Proposed Solution: Forward Signaling
Instead of checking every value recursively, we'll track which resources need resolution when RuntimeValues are assigned.
Key Ideas
- Resolution Flag on Resources: Add
_needs_resolutionflag to Resource class - Track on Assignment: When a RuntimeValue is assigned via
__setattr__, set the flag - Selective Resolution: Only resolve resources marked with
_needs_resolution=True - Propagation: When a resource references another resource's output, mark the referencing resource
Implementation Plan
1. Resource Class Changes
class Resource:
def __init__(self, **kwargs):
self._needs_resolution = False # New flag
# ... existing init code ...
def __setattr__(self, name, value):
# Check if value contains RuntimeValues
if self._contains_runtime_values(value):
self.__dict__["_needs_resolution"] = True
super().__setattr__(name, value)
2. Fast Runtime Value Detection
def _contains_runtime_values(value):
"""Fast check if value contains any RuntimeValues."""
value_type = type(value)
# Primitives never contain runtime values
if value_type in _PRIMITIVE_TYPES:
return False
# Direct runtime value
if hasattr(value, "_is_runtime_value"):
return True
# Collections might contain them
if value_type is dict:
return any(_contains_runtime_values(v) for v in value.values())
elif value_type in (list, tuple, set):
return any(_contains_runtime_values(item) for item in value)
return False
3. Optimized Synthesis
def _synthesize_hcl(self, writer):
"""Only resolve if needed."""
config = self._to_config()
if self._needs_resolution:
# Only resolve this resource's config
context = SimpleResolutionContext(self._find_stack())
resolved_config = resolve_value(config, context)
else:
# Skip resolution entirely!
resolved_config = config
# Write HCL...
Benefits
- Massive Performance Gain: Skip resolution for ~90% of resources
- O(1) Tracking: Detection happens at assignment time, not synthesis time
- No Recursive Scanning: We know upfront which resources need resolution
- Backward Compatible: Same external API
Example
# Resource with only primitives - NO resolution needed
bucket1 = S3Bucket(
nf_scope=stack,
nf_id="bucket1",
bucket="my-bucket", # string
versioning=True, # bool
)
# bucket1._needs_resolution = False
# Resource with runtime value - resolution needed
bucket2 = S3Bucket(
nf_scope=stack,
nf_id="bucket2",
bucket=bucket1.output.id, # RuntimeValue!
versioning=True,
)
# bucket2._needs_resolution = True (set automatically)
Edge Cases to Handle
- Late Assignment: Handle runtime values assigned after construction
- Nested Updates: If a dict/list is modified after assignment
- Provider References: May contain runtime values
Expected Performance Impact
- Eliminate ~460,000 resolve_value calls (90% reduction)
- Synthesis time: 26,031 → ~40,000+ resources/second
- Near-zero overhead for primitive-only resources