Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 8.1k
zend_compile: Optimize array_map() with callable convert callback into foreach#20934
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0fb5105
zend_compile: Optimize `array_map()` with callable convert callback i…
TimWolla a0ffff4
zend_compile: Skip `assert(...)` callbacks for array_map() optimization
TimWolla 7d36738
zend_compile: Remove `zend_eval_const_expr()` in array_map optimization
TimWolla 5339948
zend_vm_def: Check simple types without loading the arginfo in ZEND_T…
TimWolla 50645e4
zend_vm_def: Handle references for ZEND_TYPE_ASSERT
TimWolla 635b9d2
zend_compile: Fix handling of constant arrays for `array_map()`
TimWolla cff8da5
zend_compile: Fix leak of unused result in array_map() optimization
TimWolla 2068a9d
zend_compile: Support static methods for `array_map()` optimization
TimWolla 6771dca
Merge branch 'master' into array-map-pfa
TimWolla 625c368
UPGRADING
TimWolla File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
5 changes: 2 additions & 3 deletions
5 Zend/tests/functions/zend_call_function_deprecated_frame.phpt
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
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
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 |
|---|---|---|
| @@ -5027,7 +5027,115 @@ static zend_result zend_compile_func_clone(znode *result, const zend_ast_list *a | ||
| return SUCCESS; | ||
| } | ||
| static zend_result zend_try_compile_special_func_ex(znode *result, zend_string *lcname, zend_ast_list *args, uint32_t type) /* {{{ */ | ||
| static zend_result zend_compile_func_array_map(znode *result, zend_ast_list *args, zend_string *lcname, uint32_t lineno) /* {{{ */ | ||
| { | ||
| /* Bail out if we do not have exactly two parameters. */ | ||
| if (args->children != 2) { | ||
| return FAILURE; | ||
| } | ||
| zend_ast *callback = args->child[0]; | ||
| /* Bail out if the callback is not a FCC/PFA. */ | ||
| zend_ast *args_ast; | ||
| switch (callback->kind) { | ||
| case ZEND_AST_CALL: | ||
| case ZEND_AST_STATIC_CALL: | ||
| args_ast = zend_ast_call_get_args(callback); | ||
| if (args_ast->kind != ZEND_AST_CALLABLE_CONVERT) { | ||
| return FAILURE; | ||
| } | ||
| break; | ||
| default: | ||
| return FAILURE; | ||
| } | ||
| /* Bail out if the callback is assert() due to the AST stringification logic | ||
| * breaking for the generated call. | ||
| */ | ||
| if (callback->kind == ZEND_AST_CALL && zend_string_equals_literal_ci(zend_ast_get_str(callback->child[0]), "assert")) { | ||
| return FAILURE; | ||
| } | ||
| znode value; | ||
| value.op_type = IS_TMP_VAR; | ||
| value.u.op.var = get_temporary_variable(); | ||
| zend_ast_list *callback_args = zend_ast_get_list(((zend_ast_fcc*)args_ast)->args); | ||
| zend_ast *call_args = zend_ast_create_list(0, ZEND_AST_ARG_LIST); | ||
| for (uint32_t i = 0; i < callback_args->children; i++) { | ||
| zend_ast *child = callback_args->child[i]; | ||
| if (child->kind == ZEND_AST_PLACEHOLDER_ARG) { | ||
| call_args = zend_ast_list_add(call_args, zend_ast_create_znode(&value)); | ||
| } else { | ||
| ZEND_ASSERT(0 && "not implemented"); | ||
| call_args = zend_ast_list_add(call_args, child); | ||
| } | ||
| } | ||
| zend_op *opline; | ||
| znode array; | ||
| zend_compile_expr(&array, args->child[1]); | ||
| /* array is an argument to both ZEND_TYPE_ASSERT and to ZEND_FE_RESET_R. */ | ||
| if (array.op_type == IS_CONST) { | ||
| Z_TRY_ADDREF(array.u.constant); | ||
| } | ||
| /* Verify that the input array actually is an array. */ | ||
| znode name; | ||
| name.op_type = IS_CONST; | ||
| ZVAL_STR_COPY(&name.u.constant, lcname); | ||
| opline = zend_emit_op(NULL, ZEND_TYPE_ASSERT, &name, &array); | ||
| opline->lineno = lineno; | ||
| opline->extended_value = (2 << 16) | IS_ARRAY; | ||
| const zval *fbc_zv = zend_hash_find(CG(function_table), lcname); | ||
| const Bucket *fbc_bucket = (const Bucket*)((uintptr_t)fbc_zv - XtOffsetOf(Bucket, val)); | ||
| Z_EXTRA_P(CT_CONSTANT(opline->op1)) = fbc_bucket - CG(function_table)->arData; | ||
| /* Initialize the result array. */ | ||
| zend_emit_op_tmp(result, ZEND_INIT_ARRAY, NULL, NULL); | ||
| /* foreach loop starts here. */ | ||
| znode key; | ||
| uint32_t opnum_reset = get_next_op_number(); | ||
| znode reset_node; | ||
| zend_emit_op(&reset_node, ZEND_FE_RESET_R, &array, NULL); | ||
| zend_begin_loop(ZEND_FE_FREE, &reset_node, false); | ||
| uint32_t opnum_fetch = get_next_op_number(); | ||
| zend_emit_op_tmp(&key, ZEND_FE_FETCH_R, &reset_node, &value); | ||
| /* loop body */ | ||
| znode call_result; | ||
| switch (callback->kind) { | ||
| case ZEND_AST_CALL: | ||
| zend_compile_expr(&call_result, zend_ast_create(ZEND_AST_CALL, callback->child[0], call_args)); | ||
| break; | ||
| case ZEND_AST_STATIC_CALL: | ||
| zend_compile_expr(&call_result, zend_ast_create(ZEND_AST_STATIC_CALL, callback->child[0], callback->child[1], call_args)); | ||
| break; | ||
| } | ||
| opline = zend_emit_op(NULL, ZEND_ADD_ARRAY_ELEMENT, &call_result, &key); | ||
| SET_NODE(opline->result, result); | ||
| /* end loop body */ | ||
| zend_emit_jump(opnum_fetch); | ||
| uint32_t opnum_loop_end = get_next_op_number(); | ||
| opline = &CG(active_op_array)->opcodes[opnum_reset]; | ||
| opline->op2.opline_num = opnum_loop_end; | ||
| opline = &CG(active_op_array)->opcodes[opnum_fetch]; | ||
| opline->extended_value = opnum_loop_end; | ||
| zend_end_loop(opnum_fetch, &reset_node); | ||
| zend_emit_op(NULL, ZEND_FE_FREE, &reset_node, NULL); | ||
TimWolla marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return SUCCESS; | ||
| } | ||
| static zend_result zend_try_compile_special_func_ex(znode *result, zend_string *lcname, zend_ast_list *args, uint32_t type, uint32_t lineno) /* {{{ */ | ||
| { | ||
| if (zend_string_equals_literal(lcname, "strlen")) { | ||
| return zend_compile_func_strlen(result, args); | ||
| @@ -5099,12 +5207,14 @@ static zend_result zend_try_compile_special_func_ex(znode *result, zend_string * | ||
| return zend_compile_func_printf(result, args); | ||
| } else if (zend_string_equals(lcname, ZSTR_KNOWN(ZEND_STR_CLONE))) { | ||
| return zend_compile_func_clone(result, args); | ||
| } else if (zend_string_equals_literal(lcname, "array_map")) { | ||
| return zend_compile_func_array_map(result, args, lcname, lineno); | ||
| } else { | ||
| return FAILURE; | ||
| } | ||
| } | ||
| static zend_result zend_try_compile_special_func(znode *result, zend_string *lcname, zend_ast_list *args, const zend_function *fbc, uint32_t type) /* {{{ */ | ||
| static zend_result zend_try_compile_special_func(znode *result, zend_string *lcname, zend_ast_list *args, const zend_function *fbc, uint32_t type, uint32_t lineno) /* {{{ */ | ||
| { | ||
| if (CG(compiler_options) & ZEND_COMPILE_NO_BUILTINS) { | ||
| return FAILURE; | ||
| @@ -5120,7 +5230,7 @@ static zend_result zend_try_compile_special_func(znode *result, zend_string *lcn | ||
| return FAILURE; | ||
| } | ||
| if (zend_try_compile_special_func_ex(result, lcname, args, type) == SUCCESS) { | ||
| if (zend_try_compile_special_func_ex(result, lcname, args, type, lineno) == SUCCESS) { | ||
| return SUCCESS; | ||
| } | ||
| @@ -5263,7 +5373,7 @@ static void zend_compile_call(znode *result, const zend_ast *ast, uint32_t type) | ||
| if (!is_callable_convert && | ||
| zend_try_compile_special_func(result, lcname, | ||
| zend_ast_get_list(args_ast), fbc, type) == SUCCESS | ||
| zend_ast_get_list(args_ast), fbc, type, ast->lineno) == SUCCESS | ||
| ) { | ||
| zend_string_release_ex(lcname, 0); | ||
| zval_ptr_dtor(&name_node.u.constant); | ||
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
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
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.
For PFAs, each argument can be either of:
...?I think that you could reuse
zend_partial_apply()from https://github.com/php/php-src/pull/20848/changes#diff-85701127596aca0e597bd7961b5d59cdde4f6bb3e2a109a22be859ab7568b4d2R6704 to buildcall_argsfromcallback_args.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.
Yes, my plan was to either adjust this PR if PFA lands first or to adjust the PFA PR if that one lands first, since it is currently unreachable except for
....I hope to follow up with array_filter() and array_find(), which might require multiple arguments to fill in. Perhaps the
zend_partial_apply()could be made variadic to accept any number of values that are then filled into the correct positions if more than one placeholder is specified?