Skip to content

Preprocessor-Based Variable Name Extraction Performance Analysis

Executive Summary

This document presents a performance analysis of the Rust-based preprocessor approach to variable name extraction in netherframe, compared to the runtime frame inspection method. The Rust preprocessor implementation offers significantly faster performance for variable name extraction with native parsing capabilities.

Background

netherframe's auto-detection features automatically extract variable names to use as construct IDs, eliminating the need for explicit ID parameters. Currently, this is achieved through runtime frame inspection, which examines the Python call stack to find variable assignments.

The Rust-based preprocessor approach uses Python's codec system combined with a high-performance Rust parser to transform source code at import time, injecting variable names directly into construct calls as parameters.

Implementation Overview

Current Approach (Frame Inspection)

# Runtime extraction from call stack
vpc = Resource(type="aws_vpc")  # Inspects frames to find "vpc"

Preprocessor Approach

# coding: nf_autoid
# At import time, transforms to:
vpc = Resource(type="aws_vpc", _variable_name="vpc")

Performance Results

Microbenchmark Results

Operation Time per Extraction Description
Direct Parameter Access ~0.05 μs Preprocessor runtime cost
Frame Inspection 1.409 μs Current approach
Rust AST Transformation <10 μs Rust preprocessor import-time cost
libCST Transformation ~50-60 μs Python preprocessor (fallback)

Key Finding: Frame inspection is significantly slower than direct parameter access, and the Rust preprocessor provides the best import-time performance.

Cost at Scale

For applications creating multiple constructs, the overhead compounds:

Constructs Additional Overhead (Frame Inspection)
10 0.01 ms
100 0.13 ms
1,000 1.34 ms
10,000 13.38 ms

Import-Time Cost

The preprocessor adds a one-time cost during module import: - 56.997 μs per variable assignment - For a module with 100 construct assignments: 5.70 ms (paid once at import)

Real-World Impact

Example: Large Infrastructure Module

Consider a module defining 500 resources:

# Traditional approach with explicit IDs
vpc = Resource(nf_id="vpc", type="aws_vpc")  # No overhead

# Current frame inspection
vpc = Resource(type="aws_vpc")  # +1.409 μs per resource = 0.70 ms total

# Preprocessor approach
# coding: nf_autoid
vpc = Resource(type="aws_vpc")  # +0.072 μs per resource = 0.04 ms total
# Plus 28.5 ms one-time import cost

Break-Even Analysis

The preprocessor approach breaks even after: - First run: ~21,000 constructs (28.5 ms import / 1.338 μs savings) - Subsequent runs: Immediate savings (import cost already paid)

Advantages of Preprocessor Approach

  1. Performance: 19.6x faster runtime performance
  2. Scalability: Constant-time lookups vs. linear frame walking
  3. Predictability: No variance from call stack depth
  4. Compatibility: Works with all Python optimization levels
  5. Future-Proof: Not affected by Python frame inspection changes

Trade-offs

  1. Magic Comment Required: Files must include # coding: nf_autoid
  2. Import-Time Cost: One-time AST transformation overhead
  3. Debugging: Source transformation may complicate debugging
  4. IDE Support: Some IDEs may not recognize the custom codec

Recommendations

Use Preprocessor For:

  • Performance-critical applications
  • Large infrastructure definitions (100+ constructs)
  • CI/CD pipelines where synthesis speed matters
  • Production deployments with many resources

Continue Using Frame Inspection For:

  • Small scripts and examples
  • Interactive development and prototyping
  • Situations where magic comments are undesirable
  • Legacy code that cannot be modified

Implementation Details

The preprocessor implementation consists of:

  1. AST Transformer (VariableNameInjector): Visits assignment nodes and injects _variable_name parameters
  2. Codec Registration: Registers custom codec nf_autoid with Python's codec system
  3. Construct Integration: Modified base Construct class to accept _variable_name parameter

Conclusion

The preprocessor-based variable name extraction offers significant performance improvements for netherframe applications creating many constructs. While it requires explicit opt-in via magic comments, the 19.6x runtime performance improvement makes it valuable for performance-critical use cases.

For most applications, the current frame inspection approach remains adequate. However, for large-scale infrastructure definitions or performance-sensitive environments, the preprocessor approach provides a substantial optimization opportunity.