Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minor optimizations to array_keys()/array_values()
array_values(): When the input is an empty array or a packed array with no gaps, return the original array. array_keys(): When the input is an empty array, return the original array. When the input is a packed array with no holes (and no search key specified), populate the return with a simple range(0, count($input) - 1)
- Loading branch information
Showing
3 changed files
with
127 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
--TEST-- | ||
array_keys() and array_values() w/ packed optimization | ||
--FILE-- | ||
<?php | ||
|
||
$x = [1,2,3]; | ||
unset($x[1]); | ||
|
||
$inputs = [ | ||
[], | ||
[1,2,3], | ||
[0=>1, 1=>2, 2=>3], | ||
[1=>1, 2=>2, 3=>3], | ||
[0=>1, 2=>3], | ||
$x, | ||
]; | ||
|
||
foreach ($inputs as $input) { | ||
print_r(array_keys($input)); | ||
print_r(array_values($input)); | ||
} | ||
--EXPECT-- | ||
Array | ||
( | ||
) | ||
Array | ||
( | ||
) | ||
Array | ||
( | ||
[0] => 0 | ||
[1] => 1 | ||
[2] => 2 | ||
) | ||
Array | ||
( | ||
[0] => 1 | ||
[1] => 2 | ||
[2] => 3 | ||
) | ||
Array | ||
( | ||
[0] => 0 | ||
[1] => 1 | ||
[2] => 2 | ||
) | ||
Array | ||
( | ||
[0] => 1 | ||
[1] => 2 | ||
[2] => 3 | ||
) | ||
Array | ||
( | ||
[0] => 1 | ||
[1] => 2 | ||
[2] => 3 | ||
) | ||
Array | ||
( | ||
[0] => 1 | ||
[1] => 2 | ||
[2] => 3 | ||
) | ||
Array | ||
( | ||
[0] => 0 | ||
[1] => 2 | ||
) | ||
Array | ||
( | ||
[0] => 1 | ||
[1] => 3 | ||
) | ||
Array | ||
( | ||
[0] => 0 | ||
[1] => 2 | ||
) | ||
Array | ||
( | ||
[0] => 1 | ||
[1] => 3 | ||
) |
@sgolemon
This optimization should also be of use for
php_array_merge_or_replace_wrapper
in ext/standard/array.c and_zend_hash_merge
in Zend/zend_hash.c.array_merge
/array_replace
with the second argument being empty is quite often the case as is$arr1 + $arr2
viazend_hash_merge
.In those cases one could even go a bit further by returning the first non-empty array if all other arguments are empty as those are in this regard commutative:
array_merge($arr_empty, $arr_full)
->$arr_full
array_merge($arr_full, $arr_empty)
->$arr_full
array_replace($arr_empty, $arr_full)
->$arr_full
array_replace($arr_full, $arr_empty)
->$arr_full
$arr_empty + $arr_full
->$arr_full
$arr_full + $arr_empty
->$arr_full
Also, the simple non-empty check could be applied to
array_slice
as well.