Repository navigation
Fix GH-25379: Preserve array_map() semantics in foreach optimization - #25546
prateekbhujel wants to merge 6 commits into
Conversation
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] |
0916f35 to
146a683
Compare
|
@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 |
146a683 to
a204fc4
Compare
|
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]] |
|
Good catch on the reference semantics, @devnexen. Preserving native Rather than trying to work around that at runtime with opcode tweaks, I reverted the VM/optimizer changes and added Your test cases, along with tests for static methods and functions declared later in the file, are added in Mind taking another look when you get a chance? |
|
I do think you re getting there but will have a better look this week end |
|
what about this one ? --TEST--
GH-25379: array_map() foreach optimization with by-ref parameter added by the child method
--FILE--
<?php
class P {
public static function m($x) {
return $x + 1;
}
}
trait T {
public static function m($y, &...$x) {
return ++$x[0];
}
}
class DeclaredLater extends P {
public static function positional() {
$a = [1, 2];
$r = &$a[0];
$res = array_map(self::m(0, ?), $a);
return [$res, $a];
}
public static function named() {
$a = [1, 2];
$r = &$a[0];
$res = array_map(self::m(0, x: ?), $a);
return [$res, $a];
}
public static function m($y, &$x = null) {
return ++$x;
}
}
class FromTrait extends P {
use T;
public static function variadic() {
$a = [1, 2];
$r = &$a[0];
$res = array_map(self::m(0, ?), $a);
return [$res, $a];
}
}
function test(string $name, Closure $c) {
echo $name, ':', PHP_EOL;
try {
echo json_encode($c()), PHP_EOL;
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
}
}
test('declared later, positional', DeclaredLater::positional(...));
test('declared later, named', DeclaredLater::named(...));
test('from trait, variadic', FromTrait::variadic(...));
?>
--EXPECTF--
declared later, positional:
Warning: DeclaredLater::{closure:pfa:DeclaredLater::positional():%d}(): Argument #1 ($x) must be passed by reference, value given in %s on line %d
[[2,3],[2,2]]
declared later, named:
Warning: DeclaredLater::{closure:pfa:DeclaredLater::named():%d}(): Argument #1 ($x) must be passed by reference, value given in %s on line %d
[[2,3],[2,2]]
from trait, variadic:
Warning: FromTrait::{closure:pfa:FromTrait::variadic():%d}(): Argument #1 ($x0) must be passed by reference, value given in %s on line %d
[[2,3],[2,2]] |
|
Another great catch, @devnexen. Walking up In 1bf9f93, I removed the parent walk so we only look up directly on Your cases and the extra edge cases are in |
|
definitely looks better however I want @TimWolla to have a look at it too. |
Fixes the compiler and runtime regressions reported in GH-25379 inside
zend_compile_func_array_map():Statically known by-value callbacks only (
zend_array_map_callback_is_optimizable()):Because
FE_FETCH_Rdereferences elements whilearray_map()preserves references passed to callbacks, only inline calls whose target function or method is statically known (without walking unlinked parents, since traits or later declarations in the active class can override them) and takes the mapped argument by value. Unresolved, dynamic, abstract,ZEND_ACC2_FORBID_DYN_CALLS, invalid non-static, or by-reference callbacks fall back cleanly to nativearray_map()before emitting any opcodes.Clean bailout and evaluation order:
Validate the callback and PFA arguments before emitting any bytecode, and defer allocating the loop temporary until after checking whether the compiled
arrayargument is anIS_CONSTnon-array so bailouts never leak temporaries or evaluate subexpressions twice.Closes GH-25379.