{
    "slug": "excessive_file_io",
    "term": "Excessive File I/O",
    "category": "performance",
    "difficulty": "intermediate",
    "short": "Reading the same file multiple times inside a loop or across repeated calls — avoidable with simple in-memory caching.",
    "long": "File I/O is orders of magnitude slower than memory access. Reading a file once per request is fine; reading the same file inside a loop that runs 100 times per request multiplies the I/O cost 100×. The standard fix is a static cache: static $cache = []; if (!isset($cache[$key])) { $cache[$key] = file_get_contents($path); } return $cache[$key]; The first read hits disk; subsequent reads return the cached value instantly.",
    "aliases": [
        "file I/O bottleneck",
        "disk I/O performance",
        "file read in loop"
    ],
    "tags": [
        "performance",
        "php",
        "filesystem"
    ],
    "misconception": "File I/O is fast enough that it rarely causes performance issues in PHP. Reading files inside loops, stat() calls on every request, and repeated fopen/fclose cycles are common PHP bottlenecks — a single file_get_contents() or APC cache read replacing repeated disk access provides significant gains.",
    "why_it_matters": "File I/O is orders of magnitude slower than memory access — reading a config file, log, or template on every request adds latency that compounds under load.",
    "common_mistakes": [
        "Reading configuration files on every request instead of loading once at startup.",
        "Not caching file_get_contents() results for files that rarely change.",
        "Writing to log files synchronously on every request — use async logging or a buffer.",
        "Checking file_exists() in loops — each call is a filesystem syscall."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "n_plus_one",
        "regex_in_loop"
    ],
    "prerequisites": [
        "profiling",
        "caching",
        "apc_apcu"
    ],
    "refs": [
        "https://www.php.net/manual/en/function.file-get-contents.php",
        "https://www.php.net/manual/en/function.opcache-get-status.php"
    ],
    "bad_code": "// Reading the same file on every request inside a loop\nforeach ($userIds as $id) {\n    $config = file_get_contents('/etc/app/config.json'); // disk hit every iteration\n    $cfg    = json_decode($config, true);\n    process($id, $cfg);\n}",
    "good_code": "// Read once, reuse in memory\n$cfg = json_decode(file_get_contents('/etc/app/config.json'), true);\nforeach ($userIds as $id) {\n    process($id, $cfg);\n}\n\n// For cross-request reuse: OPcache can cache PHP arrays returned from files\n// config/cache.php → return [...]; and use opcache_invalidate() on change",
    "quick_fix": "Cache file reads in APCu or Redis after the first request — reading the same config or CSV file on every request wastes disk I/O; measure with strace to quantify real I/O calls",
    "severity": "high",
    "effort": "medium",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/excessive_file_io",
        "html_url": "https://codeclaritylab.com/glossary/excessive_file_io",
        "json_url": "https://codeclaritylab.com/glossary/excessive_file_io.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": "[Excessive File I/O](https://codeclaritylab.com/glossary/excessive_file_io) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/excessive_file_io"
            }
        }
    }
}