Optimize array_diff() for long values#22648
Open
mehmetcansahin wants to merge 1 commit into
Open
Conversation
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.
Summary
This adds a fast path for
array_diff()when all compared values are integers. Instead of converting values to temporary strings, the implementation builds an integer-keyed exclude map and copies values from the first array when they are not present in that map.Mixed-type inputs still fall back to the existing generic implementation, preserving current comparison behavior.
Benchmark
Before: ~0.12s, after: ~0.024s (~5x faster).
Tests
make -j8./sapi/cli/php run-tests.php -n -q ext/standard/tests/array/array_diff*.phpt(50/50 pass)The new test covers integer values, negative values, min/max integers, duplicate values, key preservation, string fallback cases, multiple exclude arrays, references, and empty results.