{
    "slug": "object_pooling",
    "term": "Object Pooling",
    "category": "performance",
    "difficulty": "advanced",
    "short": "Reusing a fixed set of pre-initialised expensive objects rather than creating and destroying them on every request — reducing allocation overhead.",
    "long": "Object pooling maintains a pool of reusable objects (database connections, HTTP clients, worker threads) that are checked out, used, and returned rather than instantiated fresh each time. In traditional PHP request-per-process model, object pools within a single request have limited value — the process dies anyway. They become highly relevant in long-running PHP contexts: Swoole coroutines, RoadRunner workers, or CLI queue workers that handle many tasks. Database connection pools (pgBouncer, ProxySQL) operate at the infrastructure level and benefit all PHP architectures. In-process pools in PHP-FPM should be evaluated carefully — memory is not shared between workers.",
    "aliases": [
        "object pool pattern",
        "resource pool",
        "instance pool"
    ],
    "tags": [
        "performance",
        "design-pattern",
        "memory",
        "creational"
    ],
    "misconception": "Object pooling is only relevant in languages with expensive object creation. In PHP long-running processes (Swoole, RoadRunner, workers) reuse expensive objects like HTTP clients, parser instances, and compiled templates via pools — eliminating repeated initialisation overhead across requests.",
    "why_it_matters": "Object pooling reuses expensive-to-create objects (database connections, HTTP clients, thread contexts) — eliminating repeated construction overhead in high-throughput PHP-FPM or async workloads.",
    "common_mistakes": [
        "Not resetting object state before returning to pool — a 'dirty' object from one request affects the next.",
        "Pool size not matched to concurrency — too small causes wait queues; too large wastes memory.",
        "Pooling cheap objects — the pool management overhead exceeds the construction savings for simple objects.",
        "Not handling exceptions that leave an object in an invalid state — the broken object must not return to the pool."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "connection_pooling",
        "connection_pool_sizing",
        "async_processing",
        "memory_leak"
    ],
    "prerequisites": [
        "memory_management",
        "connection_pooling",
        "php_fpm"
    ],
    "refs": [
        "https://en.wikipedia.org/wiki/Object_pool_pattern"
    ],
    "bad_code": "// Creating a new HTTP client on every request — expensive:\nfunction callApi(string $url): array {\n    $client = new GuzzleHttp\\Client(['timeout' => 5]); // New client each call\n    return $client->get($url)->json();\n    // Guzzle clients are reusable — inject a shared instance instead\n}",
    "good_code": "// Reuse expensive objects rather than constructing and destroying repeatedly\nclass PdfRendererPool {\n    private \\SplStack $pool;\n\n    public function __construct(private int $size = 4) {\n        $this->pool = new \\SplStack();\n        for ($i = 0; $i < $size; $i++) {\n            $this->pool->push(new PdfRenderer()); // pre-warm\n        }\n    }\n\n    public function acquire(): PdfRenderer {\n        return $this->pool->isEmpty()\n            ? new PdfRenderer()  // overflow — create temporary\n            : $this->pool->pop();\n    }\n\n    public function release(PdfRenderer $r): void {\n        if ($this->pool->count() < $this->size) {\n            $this->pool->push($r); // return to pool\n        }\n        // else let it GC — pool is full\n    }\n}",
    "quick_fix": "PHP-FPM resets state between requests so object pooling within a request is rarely worthwhile — in Swoole/RoadRunner persistent workers, pool expensive-to-create objects (DB connections, HTTP clients)",
    "severity": "info",
    "effort": "medium",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/object_pooling",
        "html_url": "https://codeclaritylab.com/glossary/object_pooling",
        "json_url": "https://codeclaritylab.com/glossary/object_pooling.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": "[Object Pooling](https://codeclaritylab.com/glossary/object_pooling) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/object_pooling"
            }
        }
    }
}