Repository navigation
Fix GH-25379: Preserve array_map() semantics in foreach optimization - #25546
Open
prateekbhujel wants to merge 1 commit into
Open
prateekbhujel wants to merge 1 commit into
prateekbhujel wants to merge 1 commit into
Conversation
Member
Not entirely sure about this point, can this be tried ? --TEST--
GH-25379: array_map() foreach optimization with by-ref callee unknown at compile time
--FILE--
<?php
function test(string $name, Closure $c) {
echo $name, ': ';
try {
echo json_encode($c()), PHP_EOL;
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
}
class P {
public static function m(&$x) {
return ++$x;
}
}
class C extends P {
public static function viaStatic() {
return array_map(static::m(...), [1]);
}
public static function viaParent() {
return array_map(parent::m(...), [1]);
}
public static function viaSelf() {
return array_map(self::m(...), [1]);
}
}
function rec(&$x) {
static $depth = 0;
if ($depth++) {
return ++$x;
}
return array_map(rec(...), [1]);
}
test('declared later', fn() => array_map(later(...), [1]));
test('dynamic name', function () {
$f = 'sort';
return array_map($f(...), [[3, 1]]);
});
test('static', fn() => C::viaStatic());
test('parent', fn() => C::viaParent());
test('inherited self', fn() => C::viaSelf());
test('recursive', function () {
$v = 1;
return rec($v);
});
function later(&$x) {
return ++$x;
}
?>
--EXPECTF--
declared later:
Warning: later(): Argument #1 ($x) must be passed by reference, value given in %s on line %d
[2]
dynamic name:
Warning: sort(): Argument #1 ($array) must be passed by reference, v
[true]
static:
Warning: P::m(): Argument #1 ($x) must be passed by reference, value given in %s on line %d
[2]
parent:
Warning: P::m(): Argument #1 ($x) must be passed by reference, value
[2]
inherited self:
Warning: P::m(): Argument #1 ($x) must be passed by reference, value given in %s on line %d
[2]
recursive:
Warning: rec(): Argument #1 ($x) must be passed by reference, value
[2] |
prateekbhujel
force-pushed
the
fix/gh25379-array-map-foreach-optimization
branch
from
November 18, 2026 07:20
0916f35 to
146a683
Compare
Contributor
Author
|
@devnexen Good catch. All 6 cases were still throwing I updated the PR in a204fc4 to handle this in the emitted send opcode instead:
All 6 of your cases plus a late named by-ref placeholder are now covered in |
prateekbhujel
force-pushed
the
fix/gh25379-array-map-foreach-optimization
branch
from
November 18, 2026 08:01
146a683 to
a204fc4
Compare
Member
|
There seems to be still some issues, "semantic" ones. here a test --TEST--
GH-25379: array_map() foreach optimization with by-ref callback and reference elements
--FILE--
<?php
function known(&$x) {
return ++$x;
}
function test(string $name, Closure $c) {
echo $name, ': ';
try {
echo json_encode($c()), PHP_EOL;
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
}
test('known callee', function () {
$a = [1, 2];
$r = &$a[0];
$res = array_map(known(...), $a);
return [$res, $a];
});
test('callee declared later', function () {
$a = [1, 2];
$r = &$a[0];
$res = array_map(later(...), $a);
return [$res, $a];
});
function later(&$x) {
return ++$x;
}
?>
--EXPECTF--
known callee:
Warning: known(): Argument #1 ($x) must be passed by reference, value given in %s on line %d
[[2,3],[2,2]]
callee declared later:
Warning: later(): Argument #1 ($x) must be passed by reference, value given in %s on line %d
[[2,3],[2,2]] |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes the compiler and runtime regressions reported in #25379 inside
zend_compile_func_array_map():zend_is_pipe_optimizable_callable_name()andzend_partial_apply()constraints (assert, multiple placeholders, named args before a variadic placeholder) before compilingclass_nodeandfunc_node.arrayargument is anIS_CONSTnon-array. Whenclass_nodeorfunc_nodealready emitted runtime opcodes intoIS_TMP_VAR, fall back in place to a regulararray_map()call reusing those already-evaluated nodes instead of leaving orphaned opcodes/TMPs and compiling the callee twice.zend_compile_func_array_map(), rewrite the loop placeholder'sZEND_SEND_VAL_EXintoZEND_SEND_USERwhen the callee's send mode is not statically known to accept by value, so by-reference parameters warn and wrap the element in a reference instead of throwingError(covering functions declared later, dynamic callables,static::,parent::, inheritedself::, recursive calls, and known by-ref functions/methods).ZEND_SEND_USER(Zend/zend_vm_def.h) to acceptCONST|UNUSED|NUMforop2so named placeholders work with unknown callees as well, and updateZend/Optimizer/compact_literals.cto account for its 2 cache slots when recomputingcache_size.Zend/Optimizer/optimize_func_calls.csoZEND_SEND_USERon aCONST/TMPoperand with a known positional non-ref parameter is promoted back toZEND_SEND_VAL.ZEND_ACC2_FORBID_DYN_CALLS):self/parentmethod carriesZEND_ACC2_FORBID_DYN_CALLS(extract,compact,func_get_arg,func_get_args,func_num_args,get_defined_vars), preservingCannot call X() dynamically.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 add closure/lookup overhead on dynamic/static calls, 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 ASan + Debug + ZTS checks (with Opcache JIT):
Zend/tests/array_map_foreach_optimization_gh25379.phptpasses withopcache.enable_cli=0,opcache.enable_cli=1,opcache.jit=tracing, andopcache.jit=function.Zend/tests/*array_map*,ext/opcache/tests/*array_map*, andext/standard/tests/array/*array_map*: 39 passed, 0 failed.ext/opcache/tests/: 1,028 passed, 45 skipped, 2 expected fail, 0 failed.