[ext/standard] Specialize min()/max() for long arrays#22124
Closed
mehmetcansahin wants to merge 1 commit into
Closed
[ext/standard] Specialize min()/max() for long arrays#22124mehmetcansahin wants to merge 1 commit into
mehmetcansahin wants to merge 1 commit into
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 specializes the array-form
min()/max()path for arrays whose first value isIS_LONG.The fast path compares
IS_LONGvalues directly. If the array is empty or the first value is notIS_LONG, execution falls back to the existingzend_hash_minmax()path. If a non-IS_LONGvalue is encountered after a long prefix, comparison switches back to the existingphp_data_compare()semantics for the rest of the scan.For arrays that remain all-long, the result is returned directly with
ZVAL_LONG(), avoiding generic comparison dispatch and zval copy/deref overhead on the hot path.Float fast paths were intentionally not added: local benchmarks showed no meaningful win, and adding extra type dispatch would increase complexity for little benefit.
Benchmark
Local CLI build:
Debug Build => no--disable-all --enable-clin=10000min()packed long descmax()packed long ascmin()hash longmin()mixed long-prefixmin()mixed non-long firstmin()floatsmin()stringsAcross the repeated batches, long-array and long-prefix mixed cases were roughly 3x faster. First-non-long, float, and string fallback cases stayed within noise to small overhead.
Testing
./sapi/cli/php run-tests.php -q ext/standard/tests/array/min_max_array_long_fast_path.phpt ext/standard/tests/array/min.phpt ext/standard/tests/array/max.phpt ext/standard/tests/array/min_basic.phpt ext/standard/tests/array/max_basic.phpt ext/standard/tests/array/min_basiclong_64bit.phpt ext/standard/tests/array/max_basiclong_64bit.phpt ext/standard/tests/array/min_int_float_optimisation.phpt ext/standard/tests/array/max_int_float_optimisation.phpt./sapi/cli/php run-tests.php -q ext/standard/tests/arrayFull array test result:
Differential behavior hash against baseline:
2d53aa26b0e8da297a97d311a9cb71c86f195b53b25295541ccc06f4996b9303