Skip to content

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

Merged
merged 2 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 31 additions & 15 deletions Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Copy link
Contributor

@TysonAndre TysonAndre Oct 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

  1. 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
  2. HT_IS_INITIALIZED is a better example for other code and I'd personally strongly recommend that

Seems like it should behave correctly

Copy link
Member Author

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.

Copy link
Contributor

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

zend_hash_extend(result_ht, zend_hash_num_elements(result_ht) + zend_hash_num_elements(ht), 1);
ZEND_HASH_FILL_PACKED(result_ht) {
ZEND_HASH_PACKED_FOREACH_VAL(ht, val) {
if (UNEXPECTED(Z_ISREF_P(val)) &&
UNEXPECTED(Z_REFCOUNT_P(val) == 1)) {
val = Z_REFVAL_P(val);
}
Z_TRY_ADDREF_P(val);
ZEND_HASH_FILL_ADD(val);
} ZEND_HASH_FOREACH_END();
} ZEND_HASH_FILL_END();
} else {
zend_string *key;

ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, val) {
if (Z_ISREF_P(val) && Z_REFCOUNT_P(val) == 1) {
val = Z_REFVAL_P(val);
}
Z_TRY_ADDREF_P(val);
if (key) {
zend_hash_update(result_ht, key, val);
} else {
if (!zend_hash_next_index_insert(result_ht, val)) {
zend_cannot_add_element();
zval_ptr_dtor_nogc(val);
break;
ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, val) {
if (UNEXPECTED(Z_ISREF_P(val)) &&
UNEXPECTED(Z_REFCOUNT_P(val) == 1)) {
val = Z_REFVAL_P(val);
}
}
} ZEND_HASH_FOREACH_END();
Z_TRY_ADDREF_P(val);
if (key) {
zend_hash_update(result_ht, key, val);
} else {
if (!zend_hash_next_index_insert(result_ht, val)) {
zend_cannot_add_element();
zval_ptr_dtor_nogc(val);
break;
}
}
} ZEND_HASH_FOREACH_END();
}
} else if (EXPECTED(Z_TYPE_P(op1) == IS_OBJECT)) {
zend_class_entry *ce = Z_OBJCE_P(op1);
zend_object_iterator *iter;
Expand Down
46 changes: 31 additions & 15 deletions Zend/zend_vm_execute.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.