-
Notifications
You must be signed in to change notification settings - Fork 8k
Add consumed_args callback optimization and apply to array_reduce() for carry to improve performance #21340
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
base: master
Are you sure you want to change the base?
Add consumed_args callback optimization and apply to array_reduce() for carry to improve performance #21340
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -49,6 +49,7 @@ typedef struct _zend_fcall_info { | |||||
| zval *params; | ||||||
| zend_object *object; | ||||||
| uint32_t param_count; | ||||||
| uint32_t consumed_args; | ||||||
| /* This hashtable can also contain positional arguments (with integer keys), | ||||||
| * which will be appended to the normal params[]. This makes it easier to | ||||||
| * integrate APIs like call_user_func_array(). The usual restriction that | ||||||
|
|
@@ -340,6 +341,13 @@ typedef struct _zend_fcall_info_cache { | |||||
| #define ZEND_FCI_INITIALIZED(fci) ((fci).size != 0) | ||||||
| #define ZEND_FCC_INITIALIZED(fcc) ((fcc).function_handler != NULL) | ||||||
|
|
||||||
| static zend_always_inline uint32_t zend_fci_consumed_arg(uint32_t arg_index) { | ||||||
| return arg_index < 32 ? (1u << arg_index) : 0u; | ||||||
| } | ||||||
| static zend_always_inline bool zend_fci_is_consumed_arg(uint32_t consumed_args, uint32_t arg_index) { | ||||||
| return arg_index < 32 && (consumed_args & (1u << arg_index)); | ||||||
|
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.
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| ZEND_API int zend_next_free_module(void); | ||||||
|
|
||||||
| BEGIN_EXTERN_C() | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| --TEST-- | ||
| array_reduce(): accumulator refcount stays low during callback | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| $result = array_reduce([1, 2, 3], function ($acc, $val) { | ||
| debug_zval_dump($acc); | ||
| $acc[] = $val; | ||
| return $acc; | ||
| }, []); | ||
|
|
||
| debug_zval_dump($result); | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| array(0) interned { | ||
| } | ||
| array(1) packed refcount(2){ | ||
| [0]=> | ||
| int(1) | ||
| } | ||
| array(2) packed refcount(2){ | ||
| [0]=> | ||
| int(1) | ||
| [1]=> | ||
| int(2) | ||
| } | ||
| array(3) packed refcount(2){ | ||
| [0]=> | ||
| int(1) | ||
| [1]=> | ||
| int(2) | ||
| [2]=> | ||
| int(3) | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
|
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. I see this one does fail.
Author
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. Yes, I noticed that, and I changed it already to |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| --TEST-- | ||
| zend_test_call_with_consumed_args(): consume a non-reference arg | ||
| --EXTENSIONS-- | ||
| zend_test | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| $result = zend_test_call_with_consumed_args( | ||
| static function ($a, $b) { | ||
| debug_zval_dump($a); | ||
| return [$a, $b]; | ||
| }, | ||
| [range(1, 3), "x"], | ||
| 1, | ||
| ); | ||
|
|
||
| var_dump($result["consumed_args"]); | ||
| var_dump($result["retval"]); | ||
|
|
||
| ?> | ||
| --EXPECTF-- | ||
| array(3) %srefcount(3){ | ||
| [0]=> | ||
| int(1) | ||
| [1]=> | ||
| int(2) | ||
| [2]=> | ||
| int(3) | ||
| } | ||
| int(1) | ||
| array(2) { | ||
| [0]=> | ||
| array(3) { | ||
| [0]=> | ||
| int(1) | ||
| [1]=> | ||
| int(2) | ||
| [2]=> | ||
| int(3) | ||
| } | ||
| [1]=> | ||
| string(1) "x" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| --TEST-- | ||
| zend_test_call_with_consumed_args(): do not consume reference args | ||
| --EXTENSIONS-- | ||
| zend_test | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| $value = [10]; | ||
|
|
||
| $result = zend_test_call_with_consumed_args( | ||
| static function ($a) { | ||
| $a[] = 20; | ||
| return $a; | ||
| }, | ||
| [&$value], | ||
| 1, | ||
| ); | ||
|
|
||
| var_dump($result["consumed_args"]); | ||
| var_dump($result["retval"]); | ||
| var_dump($value); | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| int(0) | ||
| array(2) { | ||
| [0]=> | ||
| int(10) | ||
| [1]=> | ||
| int(20) | ||
| } | ||
| array(1) { | ||
| [0]=> | ||
| int(10) | ||
| } |
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.