{
    "slug": "aot_vs_jit",
    "term": "Ahead-of-Time vs Just-in-Time Compilation",
    "category": "compiler",
    "difficulty": "intermediate",
    "short": "AOT compiles all code before execution (C, Go, Rust — fast startup, predictable performance). JIT compiles hot paths at runtime (PHP 8+, JVM — adapts to actual usage patterns).",
    "long": "AOT (Ahead-of-Time): the entire program is compiled to machine code before running. Advantages: fast startup, predictable performance, no runtime compilation overhead. Languages: C, C++, Go, Rust. Disadvantage: cannot optimise based on runtime data. JIT (Just-in-Time): compiles frequently executed code paths to machine code while running. Advantages: can use runtime profiling to optimise hot paths, supports dynamic languages. Disadvantages: warm-up time (startup slower), runtime overhead for compilation. PHP 8's JIT uses Opcache as the foundation — compiles hot opcodes to native code. Impact on PHP: significant for CPU-bound code (image processing, cryptography), negligible for typical I/O-bound web requests.",
    "aliases": [
        "AOT compilation",
        "JIT compilation",
        "PHP JIT",
        "compile time"
    ],
    "tags": [
        "compiler",
        "performance",
        "php"
    ],
    "misconception": "PHP 8's JIT always makes PHP faster — PHP's JIT primarily benefits CPU-bound code. Typical web applications spend 90% of time in I/O (database, network) — the JIT has minimal impact on these and can even add slight overhead.",
    "why_it_matters": "Enabling PHP's JIT for a typical web application and expecting 2x speedup is a common misunderstanding — profiling first identifies whether the bottleneck is CPU or I/O before enabling JIT.",
    "common_mistakes": [
        "Enabling PHP JIT expecting speedup for I/O-bound web apps — measure first.",
        "Not understanding JIT warmup — first requests are slower while code is being profiled and compiled.",
        "Comparing PHP JIT to Go/Rust AOT — AOT languages have no warmup and generally faster peak performance.",
        "Not testing JIT stability — JIT is complex; some code paths may have JIT bugs."
    ],
    "when_to_use": [
        "Prefer AOT (Go, Rust, C) for CLI tools, containers, and serverless where cold-start time and binary portability matter.",
        "Use JIT (PHP 8+ opcache.jit, JVM) for long-running CPU-bound workloads where runtime profiling improves hot-path throughput.",
        "Enable PHP JIT specifically for number-crunching, image processing, or ML inference — not for typical I/O-bound web requests."
    ],
    "avoid_when": [
        "Do not enable PHP JIT for typical CRUD web apps — the warm-up cost and memory overhead outweigh gains for I/O-bound code.",
        "Avoid JIT in short-lived PHP scripts (CLI one-shots) where the compilation cost exceeds total runtime.",
        "Do not assume JIT improves all code paths equally — profile first; unoptimised JIT can regress some patterns."
    ],
    "related": [
        "jit_compilation_deep",
        "php_compilation_pipeline",
        "profiling",
        "bytecode_vm"
    ],
    "prerequisites": [
        "php_jit",
        "php_compilation_pipeline",
        "bytecode_vm"
    ],
    "refs": [
        "https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.jit"
    ],
    "bad_code": "; Enabling JIT everywhere without measuring:\n; php.ini:\nopcache.jit_buffer_size = 100M\nopcache.jit = tracing\n; Benchmark result: 0% improvement for DB-heavy Laravel app\n; JIT overhead: 50MB memory, slower startup\n; Wrong tool for the problem",
    "good_code": "; First: profile to confirm CPU-bound bottleneck\n; $ php -d xdebug.mode=profile script.php\n; Result: 80% time in image resize — CPU bound!\n\n; Now enable JIT for targeted benefit:\nopcache.jit_buffer_size = 64M\nopcache.jit = tracing  ; Best for loops\n; Benchmark: 40% faster image processing\n; Web endpoints: 2% faster (within noise)\n; Worth it for image service, not general app",
    "example_note": "The bad config enables tracing JIT globally without measurement; the good example profiles first, confirms a CPU bottleneck, then targets JIT at the specific slow path.",
    "quick_fix": "PHP uses JIT (compile hot paths at runtime) not AOT (compile everything upfront) — JIT benefits CPU-bound long-running workers, not typical I/O-bound web requests",
    "severity": "info",
    "effort": "low",
    "created": "2026-03-16",
    "updated": "2026-03-31",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/aot_vs_jit",
        "html_url": "https://codeclaritylab.com/glossary/aot_vs_jit",
        "json_url": "https://codeclaritylab.com/glossary/aot_vs_jit.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": "[Ahead-of-Time vs Just-in-Time Compilation](https://codeclaritylab.com/glossary/aot_vs_jit) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/aot_vs_jit"
            }
        }
    }
}