Repository navigation
Conversation
|
Hi there, @gr8man! 👋 It looks like this pull request does not follow our template:
Please update the description to follow the template. The |
Caches parsed cast rules (handler class, extracted parameters) on demand per unique type in DataCaster. This eliminates repeated regex pattern matching (preg_match), string manipulation (explode, trim), and reflection interface checks (is_subclass_of) for every field on every row hydrated during Entity and Model operations. Benchmark on 900,000 castAs() calls: - Before: ~6,440 ms (7.16 us/call) - After: ~2,943 ms (3.27 us/call) - 2.18x speedup (-54% time, >305k casts/s)
gr8man
force-pushed
the
perf/datacaster-type-cache
branch
from
November 1, 2026 23:15
4ab52ef to
7777bf0
Compare
This branch has not been deployed
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
This PR optimizes
DataCaster::castAs()by compiling and caching type cast rules on demand per unique type inDataCaster::$compiledTypes.Motivation and Context
In
DataCaster::castAs(), hydrating model rows and entity attributes repeatedly triggered the following operations for every single field of every hydrated row:preg_match('/\A(.+)\[(.+)\]\z/', $type, $matches)to detect array/parameterized cast definitions (e.g.,json[array],datetime[Y-m-d]).explode(',', ...)andarray_map(trim(...), ...)to parse parameters.is_subclass_of($handler, CastInterface::class)andis_subclass_of($handler, EntityCastInterface::class)when$strict === false(the default mode used by Entity casting).For a query fetching 1,000 rows with 10 fields per row, this resulted in 10,000 regex executions and 20,000 reflection checks.
By caching the compiled
[$handler, $params]keyed by$type:int,bool,csv) share the compiled rule across the lifetime of the caster.preg_match,explode,trim, andis_subclass_ofexecute only once per unique cast type.$type = $this->types[$field] ?? nullprovides a single array lookup fast path.setTypes(array $types)preserves the compiled cache when called with identical types (asEntity::castAs()does on every field), while safely invalidating$compiledTypesif types are modified dynamically.Benchmark & Performance Impact
Microbenchmark executing 900,000
castAs()calls with a mix of types (int,bool,float,csv,json[array],?int):Backward Compatibility (100% BC)
DataCasteris afinalclass with private properties; no public or protected API signatures have changed.?type) immediately returnsnull.InvalidArgumentException('Field "..." is not nullable, but null was passed.')before checking handler validity.CastException::forInvalidMethod()upfront.Checklist