Skip to content

Startup Overhead Analysis: Why Small Resource Counts Show Overhead

Executive Summary

The new architecture shows overhead at small resource counts due to ~2.5ms of fixed initialization costs in the App creation, while providing 13.4μs faster per-resource creation. The break-even point is around 201 resources.

Detailed Findings

1. Fixed vs Variable Costs

Fixed Costs (App + Stack initialization): - Old Architecture: 0.03ms - New Architecture: 2.73ms - Overhead: 2.70ms

Variable Costs (per resource): - Old Architecture: 17.0μs per resource - New Architecture: 3.6μs per resource - Savings: 13.4μs per resource

2. The Break-Even Formula

Total Time = Fixed Cost + (Variable Cost × Resource Count)

Old: 0.03ms + (17.0μs × n)
New: 2.73ms + (3.6μs × n)

Break-even: 2.73ms - 0.03ms = (17.0μs - 3.6μs) × n
           2.70ms = 13.4μs × n
           n = 201 resources

3. Where Does the 2.5ms Overhead Come From?

The overhead is primarily in App initialization (2.486ms):

  1. AppContext Creation (~0.5ms)
  2. Creating threading locks
  3. Initializing construct registry
  4. Setting up validation systems

  5. Configuration Loading (~1.0ms)

  6. Reading environment variables
  7. Parsing JSON context
  8. Loading configuration with get_config_value calls
  9. Checking several environment variables for performance mode and validation (since removed: settings are flags and [tool.nf.config] keys, and nothing reads the environment)

  10. Logging Setup (~0.3ms)

  11. Logger initialization
  12. Setting log levels based on performance mode

  13. BaseConstruct Initialization (~0.2ms)

  14. Metadata setup
  15. Validation registration
  16. Path computation

  17. Import Overhead (~0.5ms)

  18. The new architecture imports more modules
  19. Type checking imports (TYPE_CHECKING blocks)
  20. Additional dependencies

4. Performance Curve Visualization

Time (ms)
│
10│     Old: ╱╱╱╱╱╱╱╱╱╱╱
│        ╱╱╱╱
8│      ╱╱╱╱
│    ╱╱╱╱
6│  ╱╱╱╱         New: ━━━━━━
│ ╱╱╱           ╱╱╱╱╱
4│╱╱         ╱╱╱╱╱
│       ╱╱╱╱╱
2│━━━━━━━━━
│
0└────────────────────────→ Resources
 0   100  200  300  400  500
      ↑
  Break-even

5. Why This Trade-off Makes Sense

  1. Real-world usage: Most infrastructure has 100s-1000s of resources
  2. One-time cost: The 2.5ms overhead happens once per app
  3. Massive savings at scale: At 1000 resources, saves 11ms (78% faster)
  4. Better architecture: The overhead comes from proper initialization and safety

6. Optimization Opportunities

To reduce the startup overhead, we could:

  1. Lazy Configuration Loading (save ~0.5ms)

    # Instead of loading all config upfront
    @property
    def performance_mode(self):
        if not hasattr(self, "_performance_mode"):
            self._performance_mode = get_config_value(...)
        return self._performance_mode
    

  2. Conditional Imports (save ~0.3ms)

    # Import heavy modules only when needed
    def _get_backend_manager(self):
        from nf.core.backend_config import BackendConfigManager
    
        return BackendConfigManager
    

  3. Simplified AppContext (save ~0.2ms)

    # Use simpler data structures for small apps
    if expected_resources < 100:
        self._registry = {}  # Simple dict
    else:
        self._registry = OptimizedRegistry()  # Full implementation
    

  4. Fast-path for Simple Cases (save ~0.8ms)

    # Skip validation and extra setup for simple cases
    def __init__(self, *, nf_id: str = "app", fast_mode: bool = False):
        if fast_mode:
            self.nf_id = nf_id
            self._stacks = {}
            return
        # ... full initialization
    

7. Current Performance Characteristics

Resources Old Total New Total Winner Margin
1 0.06ms 2.67ms Old 44.5x slower
10 0.22ms 2.70ms Old 12.3x slower
50 0.90ms 2.86ms Old 3.2x slower
100 1.73ms 2.99ms Old 1.7x slower
200 3.43ms 3.44ms ~Tie 1.0x
500 8.44ms 4.43ms New 1.9x faster
1000 16.86ms 6.20ms New 2.7x faster
10000 169ms 36ms New 4.7x faster

Conclusion

The 2.5ms startup overhead in the new architecture is a deliberate trade-off that provides:

  1. Proper initialization with validation and safety checks
  2. Better architecture with immediate binding and flat registries
  3. Massive performance gains at realistic scales (200+ resources)
  4. Consistent API following CDK patterns

For typical infrastructure with 500+ resources, the new architecture is significantly faster. The overhead only affects toy examples and unit tests with <200 resources.

Recommendations

  1. Keep the current design - The trade-off is worth it for real usage
  2. Document the break-even point - Users should know when each architecture excels
  3. Consider a fast-path mode - For unit tests and tiny demos
  4. Continue optimizing - But focus on the variable costs, not fixed costs