-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Optimize spread operator for packed arrays (fix GH-9794) #9796
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
Conversation
The failed AppVeyor CI looks not related to this PR. |
@@ -6047,23 +6047,39 @@ ZEND_VM_C_LABEL(add_unpack_again): | |||
if (EXPECTED(Z_TYPE_P(op1) == IS_ARRAY)) { | |||
HashTable *ht = Z_ARRVAL_P(op1); | |||
zval *val; | |||
zend_string *key; | |||
|
|||
if (HT_IS_PACKED(ht) && (zend_hash_num_elements(result_ht) == 0 || HT_IS_PACKED(result_ht))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (HT_IS_PACKED(ht) && (zend_hash_num_elements(result_ht) == 0 || HT_IS_PACKED(result_ht))) { | |
if (HT_IS_PACKED(ht) && (!HT_IS_INITIALIZED(result_ht) || HT_IS_PACKED(result_ht))) { |
I'm pretty sure there can be hash tables that are initialized but associative(not packed), e.g. $globalXyz = ['a' => 'val']; unset($globalXyz['a']);
has zend_hash_num_elements === 0
- oh, right, this is a temporary array being constructed and only being added to, that suggestion isn't technically needed here, but I expect my suggestion to be be faster anyway since both macros are checking HT_FLAGS from the same location in memory and the compiler could optimize that
- HT_IS_INITIALIZED is a better example for other code and I'd personally strongly recommend that
Seems like it should behave correctly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah this is just to check if the temp array needs to be mark as packed and/or extend to necessary size. zend_hash_num_elements
is for new array.
I'll do a benchmark later to see there is any performance improvement, although I won't expect much difference. zend_hash_num_elements
is inline and simple.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since both macros are checking HT_FLAGS from the same location in memory and the compiler could optimize that
And following up, unsurprisingly, the compiler does optimize that (one less branch to predict, so I'd expect it to be better, though for it to be that noticeable you'd use a random mix of lists of length 0 and 1)
https://godbolt.org/z/sdaPEf8z9
void xyz(int n) {
if ((n & (1<<2)) || !(n&(1<<3))) {
puts("Matches one flag and not the other");
}
}
e.g. the condition compiles to (x&12) != 8
and edi, 12
cmp edi, 8
jne .LBB0_2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EDIT: I mixed up the packed check for result_ht and ht again, this still looks good.
Things that are correct but I double checked (no action is required):
UNEXPECTED
macro seems reasonable enough to addresult_ht != ht
is guaranteed by the fact that[$x, $y, ...$tmp]
has a top level that's not yet assigned to value (the original code relies on the same fact)- zend_hash_extend is correct and doesn't overflow for checking the sum of lengths of 2 valid arrays
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks correct to me.
Is this something that other maintainers think would be a good idea to include in php 8.2 as well? (merged into 8.3-dev and realized this later) |
Current implementation doesn't optimize for packed arrays as point out in #9794, and this PR adds the optimization.
I used the benchmark script provided in #9794 (with small modification).
Before patching:
array unpack:0.3077449798584
array_merge:0.13217878341675
After patching:
array unpack:0.12740397453308
array_merge:0.13788986206055