PeaBox Lab › PeaLang › Learn › Chapter 32 › Lesson 1

Part V — The Modern Experience Layer · Chapter 32 · Lesson 1 of 2

Constant folding, loop unrolling and bytecode v2

Chapter 12 teaches the VM as of v0.63 — operand stack, constant pool, code objects. Later releases add opt-in layers on top: bytecode v2 encoding (v2.0.0), the deterministic bytecode optimizer (stable in 3.1.38 as runtime.bytecode_optimizer_v3) and the fast interpreter (stable in 3.1.39 as runtime.fast_interpreter_v3). Here is what each one actually does.

Constant folding and unrolling

let hints = analysis.OptimizationHints()
hints.apply({"constant_folding": true, "loop_unrolling": false})
print hints.to_dict()

let folded = analysis.constant_fold("2 + 3 * 4")   # "14"
let advice = analysis.loop_unroll_hint(4, 8)       # should_unroll: true

OptimizationHints() starts with all five hints enabled and apply(dict) flips the named fields. constant_fold(expr) is a small expression-string helper; loop_unroll_hint(iterations, threshold=8) returns a suggestion record. Neither rewrites your bytecode. The real passes work on a compiled BytecodeModule:

let optimized = analysis.BytecodeOptimizer(module)
let report = analysis.BytecodeOptimizerReport(module)
let parity = analysis.VerifyOptimization(module, optimized)
print parity["ok"]

Despite the name, analysis.BytecodeOptimizer(module) is the optimize_bytecode function: it returns a deep copy after constant folding, dead-push/pop removal and unreachable-code removal. BytecodeOptimizerReport(module) maps to optimize_report and returns per-pass counts plus a digest; VerifyOptimization(before, after) maps to verify_optimization and runs both modules on the VM to compare output and result. The Python-side helpers bytecode_optimizer_gate(report), fold_constants, remove_dead_pushes and remove_unreachable are available through analysis.BytecodeOptimizerNamespace(). From the CLI: pea optimize file.peac --verify --no-constant-folding --no-dead-push-pop --no-unreachable-code. Verify parity before you trust a speed claim.

Bytecode v2

let data = analysis.encode_bytecode_v2(instructions)
let decoded = analysis.decode_bytecode_v2(data)
print analysis.inspect_bytecode_v2(data)

v2 is a register-based encoding: encode_bytecode_v2(instructions) takes a list of instruction dicts and returns bytes with a PEACv2 header, a JSON body and a SHA-256 checksum. decode_bytecode_v2(data) returns version, instructions and checksum; inspect_bytecode_v2(data) returns metadata — version, instruction_count, checksum, size_bytes. It is a metadata inspector, not a disassembler; use pea dis file.peac for a listing. This serialization layer is separate from the 3.1.38 optimizer, which rewrites a deep copy of a BytecodeModule's code objects.

Package index and incremental loading

analysis.PackageIndex(index_dir) is a small JSON-backed registry. It offers publish(name, version, metadata), resolve(name, version), search(query), list_packages(), remove(name) and security_scan(name). Do not assume content hashing or automatic startup skipping: it loads and writes an index.json under the directory you choose, but nothing hooks it into VM startup. Wire it into your own build or loader if you want warm-start behavior.

Measuring: benchmark_vm

let report = analysis.benchmark_vm(source, 200)
print report["mean_ms"], report["p95_ms"]

benchmark_vm(source, iterations=100) returns wall-clock timings in milliseconds: min_ms, max_ms, mean_ms, median_ms and p95_ms. There is no ips or regression field — compare runs yourself. The fast interpreter adds its own checks: analysis.VerifyFastParity(module) requires identical output and result from VM and fast execution, analysis.FastInterpreterReport(module) returns version, module_digest, mode, fast_path_supported, output and result. The full fast namespace from analysis.FastInterpreterNamespace() includes benchmark_fast(module, iterations=25), compile_fast_program(module, output=None) and fast_interpreter_gate(report). The fast path covers a fixed instruction subset that includes PRINT, and unsupported modules fall back to the VM. CLI equivalents: pea fast file.peac --verify --benchmark --optimize.

Security-baseline changes since v0.63

Guided practice

← Previous Next →