--- id: PLN-0033 ticket: perf-vm-allocation-and-copy-pressure title: Plan - VM Hot Path Ownership and String Copy Pressure status: done created: 2026-04-20 completed: 2026-04-20 tags: [perf, runtime, vm, memory, strings] --- ## Briefing Implement the runtime-core portion of `DEC-0018` by reducing repeated string payload copies and hot-path allocation pressure without changing guest-visible semantics. ## Decisions de Origem - `DEC-0018` - VM Allocation and Copy Pressure Baseline ## Alvo Change the VM core so that hot paths over numeric values and already-materialized values converge toward zero allocation, while preserving the public semantics of string values and `GET_GLOBAL`. ## Escopo ### Included - VM value representation and ownership audit for hot paths. - `GET_GLOBAL` copy-pressure reduction without semantic drift. - String-producing opcode hot-path changes, especially `ADD`. - Unit and focused integration coverage for unchanged semantics. ### Excluded - Public ABI changes. - Certification or host-facing telemetry policy changes. - Broad speculative string interning or copy-on-write rollout unless directly required by a concrete implementation step. ## Fora de Escopo - Cartridge language changes. - Host debugger protocol changes. - General fault/log path rewrites beyond keeping them isolated from hot-path work. ## Plano de Execucao ### Step 1 - Audit current hot-path ownership **What:** Identify the exact VM operations that still clone or allocate on already-materialized hot paths. **How:** Review `Value`, `GET_GLOBAL`, `ADD`, related stack helpers, and any nearby string coercion paths; classify each site as unavoidable first materialization versus avoidable repeated copy. **File(s):** `crates/console/prometeu-bytecode/src/value.rs`, `crates/console/prometeu-vm/src/virtual_machine.rs` ### Step 2 - Rework internal representation/ownership for expensive payloads **What:** Introduce the smallest internal representation change needed to avoid repeated payload copies for expensive values. **How:** Prefer internal ownership changes that preserve guest-visible semantics. If strings or other expensive payloads need to move behind handles or equivalent internal storage, keep the public value meaning unchanged and constrain the change to runtime-core internals. **File(s):** `crates/console/prometeu-bytecode/src/value.rs`, `crates/console/prometeu-vm/src/heap.rs`, `crates/console/prometeu-vm/src/object.rs`, `crates/console/prometeu-vm/src/virtual_machine.rs` ### Step 3 - Fix `GET_GLOBAL` without semantic change **What:** Remove avoidable repeated copy pressure from `GET_GLOBAL`. **How:** Keep the observable contract identical while changing retrieval/storage internals so already-materialized expensive payloads are not recopied on each hot-path access. **File(s):** `crates/console/prometeu-vm/src/virtual_machine.rs` ### Step 4 - Optimize string-producing opcode paths **What:** Reduce repeated allocation/copy pressure in string-producing operations, starting with `ADD`. **How:** Separate true new-string creation from repeated cloning of existing payloads. Ensure any operation that semantically creates a new string still does so correctly, while avoiding extra transient allocations around existing materialized values. **File(s):** `crates/console/prometeu-vm/src/virtual_machine.rs`, related helper modules if introduced ### Step 5 - Preserve semantic coverage **What:** Prove semantics did not change while ownership internals did. **How:** Add/update tests for constants, dynamic strings, globals, arithmetic/string `ADD`, and edge cases where strings cross stack/global boundaries. **File(s):** `crates/console/prometeu-vm/src/virtual_machine.rs`, VM test modules ## Criterios de Aceite - `GET_GLOBAL` keeps its public semantics unchanged. - Hot-path access to already-materialized expensive values no longer performs avoidable repeated payload copies. - Numeric hot paths remain allocation-free. - String operations that truly create a new string remain correct and deterministic. - VM tests cover constant-pool strings, runtime-created strings, and globals under the new ownership model. ## Tests / Validacao ### Unit Tests - Value/ownership tests for expensive payload movement or referencing. - VM opcode tests for `GET_GLOBAL`, `SET_GLOBAL`, and string `ADD`. ### Integration Tests - Small bytecode programs that read globals repeatedly and mix numeric/string paths. ### Manual Verification - Review allocation-sensitive hotspots in the final implementation to confirm repeated copies were removed rather than merely relocated. ## Riscos - Internal representation changes may ripple into GC/root traversal. - A partial fix may hide copies in helper methods rather than eliminate them. - Over-optimizing strings could accidentally change guest-visible behavior if semantic boundaries are not defended by tests. ## Dependencies - `DEC-0018` - Existing VM heap/object/root traversal contracts