Skip to content

Fix GH-25379: Preserve array_map() semantics in foreach optimization - #25546

Open
prateekbhujel wants to merge 1 commit into
php:PHP-8.6from
prateekbhujel:fix/gh25379-array-map-foreach-optimization
Open

prateekbhujel wants to merge 1 commit into
php:PHP-8.6from
prateekbhujel:fix/gh25379-array-map-foreach-optimization

Conversation

@prateekbhujel

Copy link
Copy Markdown
Contributor

Fixes the compiler regressions reported in #25379 inside zend_compile_func_array_map():

  1. Single evaluation and no TMP leak on bailout:
    • Move zend_partial_apply() / zend_is_pipe_optimizable_callable_name() checks (e.g. assert, multiple placeholders, named args before a variadic placeholder) before compiling class_node and func_node.
    • Defer allocating the loop value temporary until after checking whether the compiled array argument is an IS_CONST non-array. When class_node or func_node already emitted runtime opcodes into IS_TMP_VAR, fall back in place to a regular array_map() call reusing those already-evaluated nodes instead of emitting orphaned opcodes/TMPs and compiling the callee twice.
  2. By-reference callbacks:
    • Bail out when the statically known target function or method has ARG_MUST_BE_SENT_BY_REF for the placeholder parameter (including variadic by-ref parameters and named placeholders), so calls like inc(...), sort(...), and inc2(?, 2) continue to emit a warning and wrap the value instead of throwing Error: Argument could not be passed by reference.
  3. Functions forbidding dynamic calls (ZEND_ACC2_FORBID_DYN_CALLS):
    • Bail out when the resolved function or static method carries ZEND_ACC2_FORBID_DYN_CALLS (such as extract, compact, func_get_arg, func_get_args, func_num_args, and get_defined_vars, including unqualified calls in namespaces that fall back to global internal functions), preserving Cannot call X() dynamically instead of running in the caller scope.

Note on #25379 part 3 (late/empty-array callback validation such as nope(...) on []): intentionally left untouched here, as pre-validating unknown callbacks before the loop would break the closure-elision opcodes tested by array_map_foreach_optimization_006.phpt / 012.phpt, and bailing out on unknown classes/functions at compile time would disable the optimization for autoloaded static methods and functions declared later in a file.

Refs GH-25379.

Local debug build checks:

  • Zend/tests/array_map_foreach_optimization_gh25379.phpt passes with both opcache.enable_cli=0 and opcache.enable_cli=1 (0 memory leaks).
  • ext/opcache/tests/ and ext/standard/tests/array/: 1,845 passed, 0 failed (58 skipped, 2 expected fail).
  • Zend/tests/: 5,562 passed, 178 skipped, 1 expected fail (only Zend/tests/new_oom.phpt failed locally due to an unquoted space in the local checkout path).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant