[8.x] Use duplicate instead of createFromBase to clone request when routes are cached#42420
Merged
Conversation
Member
|
Thanks @rodrigopedra |
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.
Closes #42403
When routes are compiled, the
CompiledRouteCollection@requestWithoutTrailingSlashmethod uses the static methodRequest@createFromBaseto clone aIlluminate\Http\Request.When the request is a JSON request, this call causes the request's JSON payload to be parsed twice:
CompiledRouteCollection@requestWithoutTrailingSlashis 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@requestWithoutTrailingSlashstates this methodGet a cloned instance of the given request without any trailing slash on the URI, so as the original$requestis already aIlluminate\Http\Request, e.g. it is already bootstrapped, we can safely change this call to use theRequest@duplicatemethod, which internally actually clones the request object, avoiding the doublejson_encodeparsing.This PR:
CompiledRouteCollection@requestWithoutTrailingSlashto callRequest@duplicateinstead 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\Requestand assert its static methodcreateFromBasewas 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.