[13.x] Skip allocation in mergeFillable/Appends/Hidden/Visible when input is empty#60008
Merged
taylorotwell merged 1 commit intolaravel:13.xfrom May 7, 2026
Conversation
|
Thanks for submitting a PR! Note that draft PRs are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface. Pull requests that are abandoned in draft may be closed due to inactivity. |
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.
Problem
PR #59404 (released v13.3.0, fixing trait initializer collision) replaced
if (empty($this->fillable)) { $this->fillable = ... }(and the analogous empty-checks forappends,hidden,visible) with calls to$this->mergeFillable(...)/mergeAppends(...)/mergeHidden(...)/mergeVisible(...).The collision fix is correct and necessary.
However, those
merge*methods are now invoked on every model construction, including when the input array is empty (the common case — most models don't declare#[Appends], sostatic::resolveClassAttribute(Appends::class, 'columns') ?? []evaluates to[]).The current implementation:
For empty
$fillable, this still allocates three transient arrays per call (array_merge,array_unique,array_values) — and the same formergeAppends,mergeHidden,mergeVisible. At high construction rates the cumulative peak-memory cost is significant.Reproducible benchmark
laravel-eloquent-bench — pinned point-release skeletons, identical synthetic 120-fillable / 13-cast / 50-relation / 4-trait model, N=100,000 constructions on PHP 8.4.14:
Memory recovers to the v13.1.1 baseline (148 MB) when the four
merge*calls short-circuit on empty input. At scale on applications that hydrate many models per request, this delta is enough to exceed PHP's defaultmemory_limit.Fix
Add an early return when the input is empty. Four guards:
…and the same pattern for
mergeAppends,mergeHidden,mergeVisible.Behavior is preserved:
array_values(array_unique(array_merge($x, [])))re-normalizes$x. Skipping that re-normalization when there's nothing to merge is a no-op for any developer-set$fillable/$appends/$hidden/$visible(which are conventionally already clean arrays). The trait-initializer-collision fix that motivated #59404 is preserved — it only matters when there's something to merge, which the guard explicitly skips.Tests
Added four
test_merge_*_with_empty_array_is_noopcases intests/Database/DatabaseEloquentModelAttributesTest.phpasserting:merge*method with[]returns$this.Existing tests for the trait-initializer-collision fix from #59404 continue to pass.
References