Repository navigation
Reuse loaded schema cache in long-running processes - #2804
adilKhan72 wants to merge 4 commits into
Conversation
Under Laravel Octane, a fresh ASTBuilder is resolved for every request, so ASTCache required the schema cache file each time. Without OPcache in the worker, this compiled the file and allocated the whole array again on every request, while the previous copy stayed alive until the next cycle collection. Keep the loaded cache file contents in memory for the lifetime of the process and reload them only when the file changes. Resolves nuwave#2436
Within opcache.revalidate_freq, requiring a replaced cache file can return the previously compiled array. Keeping that in memory under the new file fingerprint would serve the old schema until the worker restarts. Ask OPcache to recompile the file when it is newer than the cached script, and always when this process already loaded an older version.
|
Good catch from the review, that was a real gap. If the cache file gets replaced within Fixed in bc1ac63: before loading the file again we call |
There was a problem hiding this comment.
🟡 Changes recommended
Non-forced OPcache invalidation can miss a same-mtime file replacement after the in-memory entry is reset, allowing a stale AST to be cached indefinitely under the new file’s fingerprint.
1 open issue
1 resolved since last review
Review effort: Balanced
| // OPcache may still hold a previous version of the file within opcache.revalidate_freq, | ||
| // which would then be kept in memory under the fingerprint of the new file | ||
| if (function_exists('opcache_invalidate')) { | ||
| opcache_invalidate($path, force: $loaded !== null); |

Resolves #2436
Changes
In long-running processes such as Laravel Octane, a fresh
ASTBuilderis resolved for every request, soASTCache::fromCacheOrBuild()required the schema cache file on every request. Without OPcache in the worker (opcache.enable_cli=0is the default for Swoole/RoadRunner), PHP compiled the file and allocated the whole AST array again each time. Because that array was only reachable through the request's app copy, Octane's GC listener could not free it until the following request, so two copies were alive at once.ASTCachenow keeps the loaded cache file contents in memory for the lifetime of the process and reuses them while the file is unchanged (inode, mtime and size, afterclearstatcache()).set(),clear()and missing or invalid files reset it, andASTCache::forgetLoaded()resets everything. The raw array is kept rather than theDocumentAST, so each request still gets its ownDocumentASTinstance.Measured with an 18 MB schema cache through Octane's
FakeWorker, 200 requests, OPcache off:With OPcache enabled, nothing changes (118 MB, 6–10 ms), since the stored array is the immutable one from shared memory.
I couldn't reproduce an unbounded per-request growth; the numbers above level off after ~20 requests, but the double copy plus compile overhead is enough to hit a 512 MB limit with a large schema, which matches the fatal error in the issue (raised while requiring
lighthouse-schema.php).Breaking changes
None.
Changelog
Fixed