{
    "slug": "opcode_optimisation",
    "term": "Opcode Optimisation",
    "category": "compiler",
    "difficulty": "advanced",
    "short": "PHP's compiler applies optimisation passes to the opcode array before caching — constant folding, dead code elimination, and pass-through minimisation reduce instruction count.",
    "long": "PHP's Optimizer (part of OPcache) applies multiple optimisation passes to compiled opcodes: constant folding (2 + 3 compiled to 5), dead code elimination (if (false) { } removed), array/string merging (concatenated literals merged), pass-through optimisation (unnecessary RETURN opcodes removed), and live variable analysis (unused variables eliminated). PHP 7+ uses type inference during optimisation, enabling more aggressive dead code elimination with typed code. These optimisations happen once at compile time and are cached — they have zero runtime overhead once in OPcache.",
    "aliases": [
        "opcache optimizer",
        "constant folding",
        "dead code elimination",
        "PHP optimizer"
    ],
    "tags": [
        "compiler",
        "performance",
        "php",
        "opcache"
    ],
    "misconception": "Manual micro-optimisations like pre-computing constants improve PHP performance — OPcache's constant folding computes 60 * 60 * 24 at compile time; manual pre-computation adds clutter with zero performance benefit.",
    "why_it_matters": "Understanding OPcache optimisations explains why premature micro-optimisations are pointless in PHP (the compiler already does them) and where real performance gains come from (I/O, algorithms, caching).",
    "common_mistakes": [
        "Pre-computing constants manually — PHP optimiser does this at compile time.",
        "Avoiding clear code for micro-optimisation — the optimiser handles constant expressions.",
        "Not enabling OPcache — without it, no optimisations apply on any request.",
        "Thinking opcache.optimization_level controls all passes — different passes have different levels."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "php_opcache_tuning",
        "php_compilation_pipeline",
        "aot_vs_jit",
        "profiling"
    ],
    "prerequisites": [
        "bytecode_vm",
        "php_jit",
        "opcache"
    ],
    "refs": [
        "https://www.php.net/manual/en/opcache.configuration.php"
    ],
    "bad_code": "// Unnecessary manual micro-optimisation:\n$secsPerDay    = 86400;          // Pre-computed manually\n$secsPerWeek   = 604800;         // Clutter — compiler does this\n$bytesPerMB    = 1048576;\n\n// Complicated null check to avoid function call:\nif (isset($arr['key']) && $arr['key'] !== null) { }  // Premature",
    "good_code": "// Let the compiler optimise:\n$secsPerDay  = 60 * 60 * 24;     // Compiler folds to 86400\n$secsPerWeek = 60 * 60 * 24 * 7; // Compiler folds to 604800\n$bytesPerMB  = 1024 * 1024;       // Compiler folds to 1048576\n\n// Readable null check:\nif (isset($arr['key'])) { }  // Clear intent, same performance\n\n// View opcache stats:\n$status = opcache_get_status();\n// Check: ['opcache_statistics']['hits'] vs ['misses']\n// High miss rate = too many files or opcache.max_accelerated_files too low",
    "quick_fix": "Enable OPcache with opcache.optimization_level=0x7FFFBFFF (all optimisations) — the optimiser eliminates dead code, constant folds, and simplifies expressions before caching",
    "severity": "medium",
    "effort": "low",
    "created": "2026-03-16",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/opcode_optimisation",
        "html_url": "https://codeclaritylab.com/glossary/opcode_optimisation",
        "json_url": "https://codeclaritylab.com/glossary/opcode_optimisation.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": "[Opcode Optimisation](https://codeclaritylab.com/glossary/opcode_optimisation) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/opcode_optimisation"
            }
        }
    }
}