[13.x] Cache parent $table override detection in initializeModelAttributes#60009
Closed
olivier-zenchef wants to merge 1 commit intolaravel:13.xfrom
Closed
[13.x] Cache parent $table override detection in initializeModelAttributes#60009olivier-zenchef wants to merge 1 commit intolaravel:13.xfrom
olivier-zenchef wants to merge 1 commit intolaravel:13.xfrom
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 #59701 (v13.6.0, fixing #59698) added child-class
$tableoverride detection so#[Table]attributes on a child override an inherited$tableproperty.However the implementation creates a fresh
ReflectionClass(static::class)on every model construction:The two questions being asked — "does this class declare
$tableitself?" and "does this class have a#[Table]attribute?" — are class-level facts that don't vary between instances. TheReflectionClassPHP wrapper is allocated fresh per call.Reproducible benchmark
laravel-eloquent-bench, N=100,000 constructions, synthetic 120-fillable / 13-cast / 50-relation / 4-trait model on PHP 8.4.14:
On larger fat models with more class metadata (more fillable, relations, traits, larger class file) the delta scales up, because the
ReflectionClasspayload it instantiates per construct grows with the class.Fix
Cache the boolean result via the existing
static::$classAttributesclass-cache pattern (the same cacheresolveClassAttribute()already uses). The cached method is invoked frominitializeModelAttributesand only does theReflectionClasswork once per class:First construction of a given model class still pays the
ReflectionClasscost; every subsequent construction is a singlearray_key_existshash lookup. The behavior of #59701 (and the bug it fixed in #59698) is preserved exactly. The cache is also automatically flushed by the existingModel::clearBootedModels()(which already clearsstatic::$classAttributes).If the maintainers prefer a parallel cache property over the sentinel-key approach, I'm happy to switch to:
— either way the behavior is identical.
Tests
test_table_attribute_override_is_cached_per_classwhich:References