Analysis: Making bulk_operations the Default Behavior
Current bulk_operations Behavior
- Logging set to WARNING level - No INFO/DEBUG logs during construction
- Batch registration - Constructs registered in groups of 100
- Deferred ID assignment - IDs assigned when batch flushes
Developer Experience Impact
Pros ✅
- Massive Performance Boost
- 74.5% faster for typical workloads
- Near-instant infrastructure definition
-
Better experience for large infrastructures
-
Cleaner Output
- No verbose construction logs by default
- Only see warnings and errors
-
Less noise in CI/CD pipelines
-
Production-Ready
- Most production code doesn't need construction logs
- Aligns with "quiet by default" philosophy
- Similar to how Terraform doesn't log every resource
Cons ❌
- Lost Debugging Visibility
- Can't see which construct is being created
- Harder to debug construction order issues
- No progress indication for large infrastructures
-
Stack traces less informative without context
-
Deferred ID Assignment Issues
# This pattern would break: resource = Resource(stack, "bucket", type="aws_s3_bucket") print(resource._unique_id) # Would print "__pending__" -
Batch Registration Complexity
- Constructs created in different contexts might batch unexpectedly
- Harder to reason about when IDs are assigned
-
Potential for subtle bugs
-
Learning Curve
- New users expect to see what's happening
- Harder to understand construct tree building
- Less educational for learning the framework
Potential Solutions
1. Smart Defaults Based on Environment
class App:
def __init__(self, *args, **kwargs):
# Default to bulk mode in production-like environments
if os.getenv("CI") or not sys.stdout.isatty():
self.default_bulk_mode = True
else:
# Interactive development gets verbose logs
self.default_bulk_mode = False
2. Progress Indicator for Large Operations
class BulkOperationContext:
def __init__(self, stack, show_progress=True):
self.show_progress = show_progress and sys.stdout.isatty()
self._progress_bar = None
def add_construct_for_batch(self, construct):
if self.show_progress and len(self._pending_constructs) % 100 == 0:
print(f"\rCreated {len(self._pending_constructs)} resources...", end="")
3. Configurable Verbosity Levels
class App:
def __init__(self, verbosity="auto"):
# verbosity: "quiet", "normal", "verbose", "auto"
self.verbosity = verbosity
if verbosity == "quiet":
self.default_bulk_mode = True
elif verbosity == "verbose":
self.default_bulk_mode = False
elif verbosity == "auto":
# Smart detection based on environment
pass
4. Keep Explicit Control
# Don't change default, but make it easier to enable
app = App(bulk_mode=True) # Simple flag instead of context manager
A setting would make it a flag and a [tool.nf.config] key too, as every setting is
(see Settings); nothing is read from the environment.
Recommendation
Don't make bulk_operations the default, but instead:
-
Add a simple
bulk_modeparameter to App:app = App(bulk_mode=True) # Applies to entire app -
Make it a setting (a flag and a
[tool.nf.config]key):[tool.nf.config] bulk_mode = true -
Document performance tips prominently:
- Add performance section to README
- Show bulk_mode in examples for large infrastructures
-
Explain the trade-offs clearly
-
Improve logging performance:
- Check log level before formatting strings
- Use lazy formatting:
logger.info("Creating %s", class_name) -
Consider structured logging that's more efficient
-
Add progress indication:
with app.bulk_mode(show_progress=True): # Shows: "Creating resources... 500/1000"
Example Implementation
class App:
def __init__(
self,
outdir: str = "cdk.out",
skip_validation: bool = False,
lazy_validation: bool = False,
bulk_mode: bool | None = None,
):
# Auto-detect bulk mode if not specified
if bulk_mode is None:
bulk_mode = get_config_value("bulk_mode", default=False) or os.getenv("CI") is not None or not sys.stdout.isatty()
self.bulk_mode = bulk_mode
self.lazy_validation = get_config_value("lazy_validation", default=False, app_value=lazy_validation)
# Apply bulk mode to all stacks by default
if self.bulk_mode:
logger.logger.setLevel(logging.WARNING)
Conclusion
Making bulk_operations the default would provide significant performance benefits but at the cost of debugging visibility and developer experience. Instead, we should:
- Make it trivially easy to enable (single parameter or env var)
- Auto-enable in CI/production environments
- Keep verbose logging for interactive development
- Add progress indicators for better UX
- Document the performance benefits prominently
This gives developers the best of both worlds: fast production builds and debuggable development experience.