[8.x] Use duplicate instead of createFromBase to clone request when routes are cached #42420
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.
Closes #42403
When routes are compiled, the
CompiledRouteCollection@requestWithoutTrailingSlash
method uses the static methodRequest@createFromBase
to clone aIlluminate\Http\Request
.When the request is a JSON request, this call causes the request's JSON payload to be parsed twice:
CompiledRouteCollection@requestWithoutTrailingSlash
is called, which, as noticed on issue [9.x] memory exhausted because JSON payload is unwanted parsed twice by Illuminate\Http\Request::createFromBase #42403 , can cause a noticeable increase on memory usage when processing large JSON payloads.Actually, issue #42403 describes a memory exhaust scenario with default PHP configuration.
The doc block on
CompiledRouteCollection@requestWithoutTrailingSlash
states this methodGet a cloned instance of the given request without any trailing slash on the URI
, so as the original$request
is already aIlluminate\Http\Request
, e.g. it is already bootstrapped, we can safely change this call to use theRequest@duplicate
method, which internally actually clones the request object, avoiding the doublejson_encode
parsing.This PR:
CompiledRouteCollection@requestWithoutTrailingSlash
to callRequest@duplicate
instead ofRequest@captureFromBase
, to clone the current request.Notes:
This behavior only happens when routes are cached.
I didn't add any tests, as I would need to mock the
Illuminate\Http\Request
and assert its static methodcreateFromBase
was not called twice.I don't know how to properly mock static methods. If someone can guide me on how to do it, I will appreciate.