You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hmm, the two benchmark runs do seem to indicate a significant slowdown in Symfony. Could be overspecialization. Hard to say whether the trade-off is worth it. I'll try benchmarking locally tomorrow to verify.
This is inlined (or should be at least, should be zend_always_inline), so not sure if there's a point to splitting, unless the duplication can be reduced.
The benefit is too small: array_search() gains nothing at all (1.00× in every case), and in_array() only shows a real gain at 500k elements. At 500 elements it's 1.05 ± 0.12× and at 5 it's 1.01 ± 0.23× — both within noise. Arrays with hundreds of thousands of packed elements aren't the common shape for in_array(), so the extreme case isn't very informative.
What I do care about is code size. I compiled ext/standard/array.c at -O2 with and without this change (identical trees otherwise) and the affected functions grow by ~2.4 KB — _php_search_array itself goes from 1484 to 3016 bytes on arm64, and the whole family from 3372 to 5884. Same on x86_64: 3348 to 5713.
The reason is that _php_search_array is outlined (both zif_in_array and zif_array_search tail-call into it), so behavior stays a runtime value and each of the six branches now instantiates three loops instead of one. More code in the hot path means more icache misses, and every caller pays that — including all array_search() users who get no benefit.
I think this is a net loss.
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
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.
Added checks for packed array inside function
_php_search_arraythat is used byin_array()andarray_search().Function
in_array()is used frequently with lists, and this modification improves the performance.For
array_search()in all cases the diference is negligible.For
in_array()bigger the array, faster the execution.Benchmark code
Command I used for comparison:
hyperfine --warmup 3 "sapi/cli/php_master test_in_array.php" "sapi/cli/php test_in_array.php"Must be merged after merging the extended tests: