Skip to content

[12.x] Cache Arr::undot() result in Rule::compile() for repeated calls#59210

Closed
SanderMuller wants to merge 1 commit intolaravel:12.xfrom
SanderMuller:feature/cache-rule-compile-undot
Closed

[12.x] Cache Arr::undot() result in Rule::compile() for repeated calls#59210
SanderMuller wants to merge 1 commit intolaravel:12.xfrom
SanderMuller:feature/cache-rule-compile-undot

Conversation

@SanderMuller
Copy link
Copy Markdown
Contributor

@SanderMuller SanderMuller commented Mar 15, 2026

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 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

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%)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants