Repository navigation
Zend: add_function_array() fast paths for an empty operand. #24247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
devnexen
wants to merge
1
commit into
php:master
Choose a base branch
from
devnexen:add_function_array_optim
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+153
−0
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| --TEST-- | ||
| Array addition with an empty operand | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| function show(string $label, array $result): void | ||
| { | ||
| $current = key($result); | ||
| $result[-5] = 'negative'; | ||
| $result[] = 'appended'; | ||
| echo $label, ': current=', json_encode($current), ' keys=', json_encode(array_keys($result)), PHP_EOL; | ||
| } | ||
|
|
||
| $empty = []; | ||
|
|
||
| echo '-- empty right operand', PHP_EOL; | ||
| $hash = ['x' => 1, 'y' => 2]; | ||
| show('hash', $hash + $empty); | ||
| next($hash); | ||
| show('hash, moved pointer', $hash + $empty); | ||
| next($hash); | ||
| show('hash, exhausted pointer', $hash + $empty); | ||
| $list = [1, 2, 3]; | ||
| unset($list[2]); | ||
| show('list, stale next index', $list + $empty); | ||
| $emptied = [1]; | ||
| unset($emptied[0]); | ||
| show('emptied list', $emptied + $empty); | ||
|
|
||
| echo '-- empty left operand', PHP_EOL; | ||
| $hash = ['x' => 1, 'y' => 2]; | ||
| show('hash', $empty + $hash); | ||
| next($hash); | ||
| show('hash, moved pointer', $empty + $hash); | ||
| $hash = ['x' => 1, 5 => 2]; | ||
| show('hash, integer key', $empty + $hash); | ||
| unset($hash[5]); | ||
| show('hash, removed integer key', $empty + $hash); | ||
| $list = [1, 2, 3]; | ||
| show('list', $empty + $list); | ||
| next($list); | ||
| show('list, moved pointer', $empty + $list); | ||
| unset($list[2]); | ||
| show('list, stale next index', $empty + $list); | ||
| $list = [1, 2, 3]; | ||
| unset($list[0]); | ||
| show('list, leading hole', $empty + $list); | ||
| show('emptied list on the left', $emptied + ['x' => 1]); | ||
|
|
||
| echo '-- both empty', PHP_EOL; | ||
| $popped = [1]; | ||
| array_pop($popped); | ||
| show('empty + popped', $empty + $popped); | ||
| show('popped + empty', $popped + $empty); | ||
| show('empty + emptied', $empty + $emptied); | ||
| show('emptied + empty', $emptied + $empty); | ||
|
|
||
| echo '-- compound assignment', PHP_EOL; | ||
| $target = []; | ||
| $target += $popped; | ||
| show('empty += popped', $target); | ||
| $target = []; | ||
| $target += ['x' => 1, 'y' => 2]; | ||
| show('empty += hash', $target); | ||
| $target = ['x' => 1]; | ||
| $target += $empty; | ||
| show('hash += empty', $target); | ||
|
|
||
| echo '-- the operands are left untouched', PHP_EOL; | ||
| const DEFAULTS = ['a' => 1, 'b' => 2]; | ||
| $result = $empty + DEFAULTS; | ||
| $result['c'] = 3; | ||
| $other = DEFAULTS + $empty; | ||
| $other['c'] = 3; | ||
| $target = []; | ||
| $target += DEFAULTS; | ||
| $target['c'] = 3; | ||
| echo json_encode(DEFAULTS), ' ', json_encode($result), ' ', json_encode($other), ' ', json_encode($target), PHP_EOL; | ||
|
|
||
| $source = ['k' => 'v']; | ||
| $source['l'] = 'w'; | ||
| $copy = $source; | ||
| $source += $empty; | ||
| $source['m'] = 'x'; | ||
| $result = $empty + $copy; | ||
| $result['k'] = 'changed'; | ||
| echo json_encode($copy), ' ', json_encode($source), ' ', json_encode($result), PHP_EOL; | ||
|
|
||
| $value = 1; | ||
| $withReference = ['r' => &$value]; | ||
| $result = $empty + $withReference; | ||
| $result['r'] = 2; | ||
| echo $value, PHP_EOL; | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| -- empty right operand | ||
| hash: current="x" keys=["x","y",-5,-4] | ||
| hash, moved pointer: current="y" keys=["x","y",-5,-4] | ||
| hash, exhausted pointer: current="x" keys=["x","y",-5,-4] | ||
| list, stale next index: current=0 keys=[0,1,-5,3] | ||
| emptied list: current=null keys=[-5,1] | ||
| -- empty left operand | ||
| hash: current="x" keys=["x","y",-5,-4] | ||
| hash, moved pointer: current="x" keys=["x","y",-5,-4] | ||
| hash, integer key: current="x" keys=["x",5,-5,6] | ||
| hash, removed integer key: current="x" keys=["x",-5,-4] | ||
| list: current=0 keys=[0,1,2,-5,3] | ||
| list, moved pointer: current=0 keys=[0,1,2,-5,3] | ||
| list, stale next index: current=0 keys=[0,1,-5,2] | ||
| list, leading hole: current=1 keys=[1,2,-5,3] | ||
| emptied list on the left: current="x" keys=["x",-5,1] | ||
| -- both empty | ||
| empty + popped: current=null keys=[-5,-4] | ||
| popped + empty: current=null keys=[-5,0] | ||
| empty + emptied: current=null keys=[-5,-4] | ||
| emptied + empty: current=null keys=[-5,1] | ||
| -- compound assignment | ||
| empty += popped: current=null keys=[-5,-4] | ||
| empty += hash: current="x" keys=["x","y",-5,-4] | ||
| hash += empty: current="x" keys=["x",-5,-4] | ||
| -- the operands are left untouched | ||
| {"a":1,"b":2} {"a":1,"b":2,"c":3} {"a":1,"b":2,"c":3} {"a":1,"b":2,"c":3} | ||
| {"k":"v","l":"w"} {"k":"v","l":"w","m":"x"} {"k":"changed","l":"w"} | ||
| 2 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1198,6 +1198,34 @@ static zend_never_inline void ZEND_FASTCALL add_function_array(zval *result, con | |
| /* $a += $a */ | ||
| return; | ||
| } | ||
| if (zend_hash_num_elements(Z_ARRVAL_P(op2)) == 0 && | ||
| Z_ARRVAL_P(op1)->nInternalPointer < Z_ARRVAL_P(op1)->nNumUsed) { | ||
| /* $a += [] or $a + [], unless the internal pointer is past the | ||
| * end as zend_array_dup() would reset it */ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These comments make it sound like the internal pointer is the only thing that changes, which is misleading |
||
| if (result != op1) { | ||
| ZVAL_COPY(result, op1); | ||
| } | ||
| return; | ||
| } | ||
| if (zend_hash_num_elements(Z_ARRVAL_P(op1)) == 0 && | ||
| zend_hash_num_elements(Z_ARRVAL_P(op2)) != 0) { | ||
| /* [] + $a or $a += $b with an empty $a, $b is shared only if a | ||
| * merge would give the same array: op1 never had an integer key, | ||
| * $b has its pointer on the first element and its next free | ||
| * index is not stale */ | ||
| const HashTable *src = Z_ARRVAL_P(op2); | ||
| zend_long next_free = HT_IS_PACKED(src) ? (zend_long) src->nNumUsed : ZEND_LONG_MIN; | ||
| if (Z_ARRVAL_P(op1)->nNextFreeElement == ZEND_LONG_MIN && | ||
| src->nInternalPointer == 0 && | ||
| src->nNextFreeElement == next_free) { | ||
| zend_array *old = result == op1 ? Z_ARR_P(result) : NULL; | ||
| ZVAL_COPY(result, op2); | ||
| if (old) { | ||
| zend_array_release(old); | ||
| } | ||
| return; | ||
| } | ||
| } | ||
| if (result != op1) { | ||
| ZVAL_ARR(result, zend_array_dup(Z_ARR_P(op1))); | ||
| } else { | ||
|
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.