Targeted Resolution Design
Current Problem
We mark the entire resource for resolution even if only one attribute has a RuntimeValue. Then resolve_value() recursively checks ALL attributes again.
Solution: Track Specific Attributes
class Resource:
def __init__(self, **config):
self._runtime_attributes = set() # Track which attributes need resolution
# ... rest of init ...
def __setattr__(self, name, value):
super().__setattr__(name, value)
# Check if this attribute needs resolution
if self._contains_runtime_value(value):
self._runtime_attributes.add(name)
def _to_config_with_resolution(self, context):
"""Build config, only resolving marked attributes."""
config = {}
for key, value in self.__dict__.items():
if not key.startswith("_") and not key.startswith("nf_"):
if key in self._runtime_attributes:
# Only resolve attributes we know have RuntimeValues
config[key] = resolve_value(value, context)
else:
# Skip resolution for clean attributes
config[key] = value
return config
Benefits
- No Double Checking: We check once during assignment, not again during resolution
- Targeted Resolution: Only resolve the specific attributes that need it
- Massive Speedup: For a resource with 30 attributes where only 1 has a RuntimeValue, we skip checking 29 attributes
Example
bucket = S3Bucket(
nf_scope=stack,
nf_id="example",
bucket=other.output.id, # RuntimeValue - marked
force_destroy=True, # Primitive - not marked
tags={"env": "prod"}, # Static dict - not marked
lifecycle_rule=[{...}], # Complex but static - not marked
cors_rule=[{...}], # Complex but static - not marked
# ... 20+ more attributes that are static
)
# Only bucket._runtime_attributes = {"bucket"}
# During synthesis, only the "bucket" attribute gets resolved!
Implementation Options
Option 1: Set of Attribute Names
self._runtime_attributes = {"bucket", "tags"} # Simple set
Option 2: Dict with Resolution Hints
self._runtime_attributes = {
"bucket": "direct", # Direct RuntimeValue
"tags": "dict_values", # RuntimeValue in dict values
"cors_rule": "nested_list", # RuntimeValue in nested structure
}
Option 3: Lazy Resolution Paths
self._resolution_paths = [
("bucket",), # Direct attribute
("tags", "source"), # tags["source"]
("cors_rule", 0, "allowed_origins", 0), # cors_rule[0]["allowed_origins"][0]
]
Recommendation: Option 1 (Simple Set)
Start with the simplest approach - just track which top-level attributes need resolution. This alone would give significant speedup without complexity.