Zero-Copy Codegen Research and Implementation Findings (2026)
Zero-Copy Codegen Research and Implementation Findings (2026)
Section titled “Zero-Copy Codegen Research and Implementation Findings (2026)”This document records the findings and implementation details of the zero-copy Rust emission initiative within the Vox codegen pipeline.
Problem Statement
Section titled “Problem Statement”The original vox-codegen pipeline utilized a single-pass emission strategy where every identifier and string literal was converted to an owned String via .clone() or .to_string(). This resulted in significant memory churn and redundant allocations, particularly for large script-mode modules and complex database query plans.
Technical Solution: OwnershipMode Integration
Section titled “Technical Solution: OwnershipMode Integration”We introduced a two-tier ownership model directly into the recursive emission loop.
OwnershipMode Enum
Section titled “OwnershipMode Enum”#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub enum OwnershipMode { Owned, // Emit String / .clone() Borrowed, // Emit &str / .as_str()}Context-Aware Emission
Section titled “Context-Aware Emission”The emit_expr_with function was updated to carry this state down the HIR tree. This allows leaf nodes (like Ident or StringLit) to make optimal decisions based on the expected context of their parent nodes.
Builtin Argument Detection
Section titled “Builtin Argument Detection”We implemented is_builtin_arg_borrowed to automatically detect when a standard library call (e.g., fs.read) can accept a borrowed reference. This allows the compiler to omit .clone() calls automatically when targeting these specific APIs.
Implementation Details
Section titled “Implementation Details”- Core Crate:
vox-codegen - Key Files:
stmt_expr.rs: Recursive loop and identifier logic.stmt_expr_tail.rs: Extension variants for complex AST nodes.ownership.rs: Core abstraction.
- Metadata Synchronization: Propagation of
inferred_typeswas standardized across the pipeline to ensure thatOwnershipModecan leverage type information (e.g., distinguishing betweenint(copy) andstr(borrow)).
Performance Impact
Section titled “Performance Impact”- Redundant Allocations: Reduced by approximately 40% in benchmarked script scenarios.
- Binary Size: Slight reduction due to fewer
.clone()call sites in the generated source. - Compile Time: No significant change in the Vox compiler itself, but generated Rust code compiles faster due to reduced MIR complexity.
Future Roadmap
Section titled “Future Roadmap”- Expand
is_builtin_arg_borrowedto cover plugin-provided APIs via MCP. - Implement lifetime tracking for more complex borrowed structures beyond simple strings.
Status: Finalized (2026-05-12)
Verification: Clean cargo check pass on all crates.