{
    "slug": "bytecode_vm",
    "term": "Bytecode VMs",
    "category": "compiler",
    "difficulty": "intermediate",
    "short": "Zend Engine (PHP), JVM (Java/Kotlin), CLR (.NET) — all compile source to platform-independent bytecode then interpret or JIT-compile to native code.",
    "long": "Bytecode VMs provide: portability (compile once, run on any OS with the VM), sandboxing (VM controls what bytecode can do), JIT optimisation opportunity (compile hot paths to native code based on runtime profiling). Zend Engine (PHP): stack-based VM, OPcache adds caching and optional JIT. JVM (Java, Kotlin, Scala): .class bytecode, HotSpot JIT with 20+ years of maturity, longer warm-up time. CLR (.NET): CIL bytecode, JIT via RyuJIT. Key difference: JVM and CLR were designed for JIT-heavy long-running servers; Zend Engine serves short-lived request processes where JIT warm-up is often not amortised.",
    "aliases": [
        "Zend Engine",
        "JVM",
        "CLR",
        "bytecode VM",
        "virtual machine"
    ],
    "tags": [
        "compiler",
        "performance",
        "php"
    ],
    "misconception": "PHP's Zend Engine is equivalent to the JVM in JIT performance — JVM's HotSpot JIT has 20+ years of maturity and is optimised for long-running server processes; Zend Engine serves short-lived requests where JIT warm-up time is rarely amortised across enough requests.",
    "why_it_matters": "Understanding bytecode VMs explains JVM warm-up time (JIT not yet profiled), why PHP JIT helps long-running CLI scripts more than short web requests, and why AOT-compiled languages (Go, Rust) are faster for CPU-bound work.",
    "common_mistakes": [
        "Expecting PHP JIT to match JVM HotSpot performance — different maturity levels and workload targets",
        "Not understanding that Zend opcodes are stack-based — variable accesses involve push/pop operations",
        "Thinking the VM prevents all malicious code — open_basedir and disable_functions restrict PHP capabilities",
        "Comparing benchmarks without controlling for workload type — I/O-bound vs CPU-bound gives completely different JIT results"
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "php_compilation_pipeline",
        "aot_vs_jit",
        "jit_compilation_deep",
        "php_roadrunner"
    ],
    "prerequisites": [
        "php_compilation_pipeline",
        "aot_vs_jit",
        "php_jit"
    ],
    "refs": [
        "https://wiki.php.net/rfc/jit"
    ],
    "bad_code": "// Expecting JVM-level JIT speedup from PHP JIT:\n// PHP web app benchmark with JIT enabled:\n// Without JIT: 1000 req/s\n// With JIT:    1020 req/s (2% improvement — I/O bound app)\n// Expected based on JVM knowledge: 2-5x improvement\n// Reality: minimal for typical PHP web apps",
    "good_code": "// PHP JIT meaningful for CPU-bound long-running workers:\n// Image resizing benchmark (CPU-intensive, long-running worker):\n// Without JIT: 10 images/s\n// With JIT (tracing mode): 14 images/s — 40% improvement\n\n// Configure for long-running PHP workers where JIT warms up:\n// php.ini:\n// opcache.jit = tracing          ; Best for loops\n// opcache.jit_buffer_size = 128M ; JIT compiled code cache",
    "quick_fix": "PHP's Zend Engine VM executes opcodes — OPcache eliminates the parse-to-opcode step for cached files; understanding this explains why OPcache alone gives 2-5x speedup",
    "severity": "info",
    "effort": "medium",
    "created": "2026-03-16",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/bytecode_vm",
        "html_url": "https://codeclaritylab.com/glossary/bytecode_vm",
        "json_url": "https://codeclaritylab.com/glossary/bytecode_vm.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Bytecode VMs](https://codeclaritylab.com/glossary/bytecode_vm) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/bytecode_vm"
            }
        }
    }
}