[12.x] Cache Arr::undot() result in Rule::compile() for repeated calls#59210
Closed
SanderMuller wants to merge 1 commit intolaravel:12.xfrom
Closed
[12.x] Cache Arr::undot() result in Rule::compile() for repeated calls#59210SanderMuller wants to merge 1 commit intolaravel:12.xfrom
Arr::undot() result in Rule::compile() for repeated calls#59210SanderMuller wants to merge 1 commit intolaravel:12.xfrom
Conversation
When using Rule::forEach with large arrays, Rule::compile() is called once per array item. Each call runs Arr::undot() on the same flattened data array, which is O(n) per call. For 500 items with 2000 flattened entries, this produces ~381ms of redundant work. Cache the undotted result and reuse it when the same data array is passed to consecutive compile() calls. Before: 473ms for 500 items with Rule::forEach After: 59ms for 500 items with Rule::forEach (-88%)
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.
When using
Rule::forEachwith large arrays,Rule::compile()is called once per array item. Each call runsArr::undot()on the same flattened data array, which is O(n) per call. For 500 items with 2000 flattened entries, this produces ~381ms of redundant work.Cache the undotted result and reuse it when the same data array is passed to consecutive
compile()calls.Before: 473ms for 500 items with
Rule::forEachAfter: 59ms for 500 items with
Rule::forEach(-88%)This PR addresses a remaining bottleneck from #49375
Edit:
This PR is part of a series of PRs with the focus on improving validation performance. One of the repositories I work on does big exports/imports of which 75%+ of the time is spent validating despite doing 100s of insert queries.
The performance improvements are made using the autoresearch principle followed by cherry-picking parts of the performance findings into small PRs like this one.
The test failure will be fixed once #59207 is merged
I am open to targeting these improvements to 13.x as well, if we don't want to release them as part of 12.x