[ext/standard] Specialize min()/max() for numeric arrays#22127
Open
mehmetcansahin wants to merge 3 commits into
Open
[ext/standard] Specialize min()/max() for numeric arrays#22127mehmetcansahin wants to merge 3 commits into
mehmetcansahin wants to merge 3 commits into
Conversation
Contributor
|
You may need to add an entry to the UPGRADING file, Performance Improvements section. |
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 specializes the array-form
min()/max()path for numeric arrays.The fast path compares homogeneous
IS_LONGandIS_DOUBLEvalues directly. If the array is empty or the first value is not numeric, execution falls back to the existingzend_hash_minmax()path. If a mixed value is encountered after a numeric prefix, comparison switches back to the existingphp_data_compare()semantics for the rest of the scan. NaN-sensitive double cases are handed to the generic comparison path to preserve existing ordering behavior.For arrays that remain all-long or all-double, the result is returned directly with
ZVAL_LONG()orZVAL_DOUBLE(), avoiding generic comparison dispatch and zval copy/deref overhead on the hot path.An
UPGRADINGentry was added under Performance Improvements.Benchmark
Local CLI build:
Debug Build => no--disable-all --enable-clin=100000700min()+max()iterationsorigin/masterFallback / non-numeric cases:
origin/masterAcross repeated samples, integer-only arrays stay roughly 3x faster and float-only arrays are roughly 4.8x-5x faster. Generic fallback cases stay within noise to small overhead.
Testing
git diff --checkgit diff --cached --checkTEST_PHP_EXECUTABLE=/Users/mehmetcan/work/php/php-src/sapi/cli/php ./sapi/cli/php -n run-tests.php -n -q ext/standard/tests/array/min_max_array_fast_path.phptResult: