Skip to content

Optimize array intersection functions for empty operands#22831

Open
mehmetcansahin wants to merge 1 commit into
php:masterfrom
mehmetcansahin:optimize-array-intersect-empty-operands
Open

Optimize array intersection functions for empty operands#22831
mehmetcansahin wants to merge 1 commit into
php:masterfrom
mehmetcansahin:optimize-array-intersect-empty-operands

Conversation

@mehmetcansahin

Copy link
Copy Markdown
Contributor

Avoid unnecessary iteration when an empty operand guarantees an empty result for array_intersect_key(), array_intersect_assoc(), and array_uintersect_assoc().

Includes regression tests covering validation and observable callback/warning behavior.

@kamil-tekiela

Copy link
Copy Markdown
Member

How does it affect performance for the negative case when none of the arrays is empty?

@mehmetcansahin

mehmetcansahin commented Jul 21, 2026

Copy link
Copy Markdown
Contributor Author

@kamil-tekiela Thanks for the review. I reran the non-empty cases using 15 paired processes per scenario and repeated the measurements twice.

For very small arrays, the overhead ranged from roughly 0.5% to 5%, corresponding to only a few nanoseconds per call. With 100 or 10,000 elements, the differences were generally around 1% or less and were not consistent between runs.

Overall, the shortcut adds a small fixed cost to non-empty calls, which becomes negligible as the arrays grow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants