Part V — The Modern Experience Layer · Chapter 34 · Lesson 2 of 2
A numerics fast path you can actually use
The JIT chapter showed the skeleton; this lesson is judgment. PeaLang 3.1.39 does not ship an automatic numeric JIT with guaranteed speedups. There are three separate, measurable paths: conservative bytecode optimization, a limited fast interpreter, and a small register-level instruction rewriter. Choose them by measurement.
What actually gets faster
- Deterministic bytecode peepholes —
analysis.BytecodeOptimizerfolds numeric constants, removes literal pushes followed byPOP, and drops unreachable code. It works on a deep copy and can be checked withanalysis.VerifyOptimization. - Fast-interpreter subset —
analysis.FastInterpreterpre-binds literals, loads/stores, arithmetic, comparisons,printand jumps to closures. Unsupported instructions fall back to the standard VM, so useanalysis.VerifyFastParityto confirm identical output and result. - Limited register-level rewrites —
analysis.OptimizeInstructionscan fold a narrow constant pattern, add anIC_CHECKmarker beforeGETATTR, rewritePOWwhenarg1 == 2, and rewriteDIVwhenarg2 == 2. It does not implement loop hoisting or a full inline cache. - Vectorized data plans —
analysis.create_query_planbuilds lazy plans over rows.analysis.backend_info()reports whether DuckDB, Polars and PyArrow are installed, but only the DuckDB backend is wired into plan execution; other preferences fall back toPythonBackend.
No part of the 3.1.39 source supports a blanket “two to ten times faster” claim. Report the number you measured on your artifact.
What does not
- I/O waits, network and disk. No optimizer speeds up a slow database, remote API or unindexed query.
- Calls, imports, exceptions, iterators and object access on the fast-interpreter path; those stay on the standard VM.
- Megamorphic dispatch. The
IC_CHECKinserted byOptimizeInstructionsis a marker, not a runtime cache that collapses receiver types. - Loop-invariant hoisting. The
loop_invariantspass is currently a no-op. - SQL strings passed to
analysis.create_query_plan. It builds a plan from data and builder methods; it does not parse SQL.
The bigger lever for analytics: Arrow and DuckDB (v1.6.0)
let rows = [
{"city": "North", "amount": 10},
{"city": "South", "amount": 20}
]
let info = analysis.backend_info()
let backend = "python"
if info.duckdb_available {
backend = "duckdb"
}
let plan = analysis.create_query_plan(rows, backend)
let projected = plan.select(["city", "amount"])
let limited = projected.limit(10)
let result = limited.collect()
print result
print info
The registered Arrow helpers are preferred_backend, available_backends, backend_info and create_query_plan. A plan exposes filter, select, limit, sort, aggregate, collect, explain and optimize; filter expects a Python callable, so keep that step in host tooling unless you have a bridge. Only "duckdb" selects DuckDBBackend during execution; any other value uses PythonBackend. This is a lazy plan over rows, not a SQL parser. Use it for data-shaped work, and use the bytecode or fast-interpreter paths for scalar code.
Measurement discipline
let baseline_source = read("baseline.pea")
let candidate_source = read("candidate.pea")
let baseline = analysis.benchmark_vm(baseline_source, 100)
let candidate = analysis.benchmark_vm(candidate_source, 100)
print baseline.median_ms
print baseline.p95_ms
print candidate.median_ms
analysis.benchmark_vm(source, iterations=100) returns iterations, min_ms, max_ms, mean_ms, median_ms and p95_ms. There is no analysis.benchmark_compare: the Python-level vm_optimizer.benchmark_compare is not registered in analysis and returns a modeled 0.8x estimate with a fixed 1.25 speedup, so it is not a measured comparison. Archive baselines so regressions surface as deltas. If a benchmark or parity call fails, PeaLang errors use pealang-error-v1; inspect the ErrorValue record, the code, cause and context fields on PeaError, or the record returned by build_error(), rather than string-matching.
Version note
Version note. The local source is PeaLang 3.1.39. bytecode_optimizer.py declares OPTIMIZER_VERSION = "3.1.38" and CORE_RELEASE_FROZEN_AT = "3.1.37". jit_compiler.py is still a skeleton; its docstring describes a future v2.2 native emitter, but emit_native returns a marker blob. analysis.loop_unroll_hint is a suggestion helper from vm_optimizer.py: it reports should_unroll and an estimated_speedup, and does not rewrite code. OptimizationHints.loop_unrolling is not read by OptimizeInstructions.
Where this leaves the 1.0-era performance story
- Interpreter/VM first for fidelity; bytecode optimizer when parity passes; fast interpreter when parity passes and the subset applies; JIT skeleton for experiments; Arrow plans for data-shaped work. Each layer is opt-in and reversible.
- Parity is explicit:
analysis.VerifyOptimizationcompares before/after output and return value, andanalysis.VerifyFastParitycompares VM and fast-interpreter execution. Gate helpers live in the dicts returned byanalysis.BytecodeOptimizerNamespace()andanalysis.FastInterpreterNamespace(), not as top-levelanalysis.*names.
Guided practice
- Identify one scalar hot path and one analytics-shaped path in your project; benchmark the baseline with
analysis.benchmark_vmbefore changing either. - Run
analysis.BytecodeOptimizeron a compiled module andanalysis.VerifyOptimizationagainst the original; keep the rewrite only whenokis true. - Build a plan with
analysis.create_query_plan(rows, "duckdb")whenanalysis.backend_info().duckdb_availableis true, otherwise use"python"; record theexplain()output with the result. - Write the dataset size and shape next to every number. Delete any speedup claim you cannot reproduce from the archived source and command.