[12.x] Fix memory leak in Arr::dot()#58458
Merged
taylorotwell merged 2 commits intolaravel:12.xfrom Jan 22, 2026
Merged
Conversation
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.
Description
In #55495 speed performance has been improved on the
Arr::dotfunction, however it also introduced a memory leak within in the function. The$flattenvariable, which referenced in its own closure is not garbage collected immediately after the function is finished. This results in reserved memory that persist after each call to theArr::dotfunction. It is a small amount, but if called enough times the process can eventually run out of memory. An example of this happening is when a large request needs to be validated with a large amount of rules asArr::dotis called many times during that validation.The same problem has been raised in #57660, where the context was when running Octane, however this problem is not limited to Octane, but can provoke out of memory error on a single request as well. That same solution has been proposed in a comment in #57660 however it has not been reviewed.
This PR remove the reference to the
$flattenclosure after it has been used, so it can be garbage collected immediately.Expected results
Memory used before and after the
Arr::dotfunction should remain the same.Actual results
Memory is increased for each call to
Arr::dotSteps to reproduce
Benchmark
Using the above step to reproduce the problem with different size of array and number of function calls in the three implementations.
Changing the two parameters: array size and function calls only have an effect on memory when used together with the current implementation, while the others are unaffected. All of them are of course affected in their runtime however it looks like the PR also increase speed by a small amount. The most significant change is the memory usage. Keep in mind the array used is incredibly simple and more elaborate arrays will consume more memory.
Conclusion
The
Arr::dotfunction causes a memory leak after #55495 has been implemented in exchange for much faster execution. This PR eliminate the memory leak while still maintaining the same speed performance previously implemented.