[12.x] Store the compiled route cache as a serialized payload#60516
Open
jbidad wants to merge 1 commit into
Open
[12.x] Store the compiled route cache as a serialized payload#60516jbidad wants to merge 1 commit into
jbidad wants to merge 1 commit into
Conversation
Replace the var_export()ed PHP array in the route cache file with a serialized payload that is rehydrated via unserialize(). The cache file remains a require()-able PHP file, so previously generated caches and any tooling that loads it directly keep working. The route service provider now loads serialized payloads natively through Router::setCompiledRoutes() and falls back to require() for legacy PHP cache files, keeping full backward compatibility for applications that upgrade without re-running route:cache. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What & why
route:cachecurrently dumps the compiled route collection into the cache file as avar_export()ed PHP array. For large route tables this produces a multi-megabyte PHP file that has to be parsed and compiled on the first request after every deploy / opcache reset.This PR keeps the cache file a
require()-able PHP file, but stores the compiled route data as a serialized payload that is rehydrated withunserialize():Backward compatibility
Fully backward compatible:
require()-able PHP file, so tooling that loads it directly (e.g. Testbench'sdefineCacheRoutes()) is unaffected.RouteServiceProvider::loadCachedRoutes()loads serialized payloads throughRouter::setCompiledRoutes()and falls back torequire()for legacyvar_export()cache files, so applications that upgrade without re-runningroute:cachekeep booting.unserialize()runs on the application's own generated cache file — the same trust boundary asrequire()-ing it does today.Tests
RouteCachingTest::testCachedRouteFileStoresASerializedPayload— runs the realroute:cachecommand and asserts the generated file is arequire()-able wrapper around anunserialize()d payload, and that the routes still resolve.RouteCacheLoadingTest(new) — asserts routes resolve when loaded from the new serialized-payload file, a plain serialized file, and a legacyvar_export()file (backward compatibility).Benchmarks (before / after)
Reproducible script below. PHP 8.3, three route-table sizes. Cold = first load in a fresh
php -nprocess (no opcache — i.e. the first request after a deploy / opcache reset / serverless cold start), process-startup subtracted. Warm = steady-state per-request cost once opcache has the file compiled.var_exportserializevar_exportserializevar_exportserializeHonest read of the numbers:
serialize()makes the cache file ~40% smaller and the cold / first load 2–3× faster. Under a long-lived opcache process, however,var_export()'s constant array is interned by the Zend engine and returned for ~0 µs/request, whileserialize()paysunserialize()on every request — sovar_export()repays its cold-start cost within ~5 requests and is ahead in steady state. The change is therefore most compelling for cold-start-dominated deployments (serverless / containers, opcache disabled, or very frequent deploys) and for shrinking the cache file on disk.Reproducible benchmark script