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

@prateekbhujel prateekbhujel commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

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

  1. Single evaluation and no TMP leak on bailout:
    • Move zend_is_pipe_optimizable_callable_name() and zend_partial_apply() constraints (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 leaving orphaned opcodes/TMPs and compiling the callee twice.
  2. By-reference callbacks (both known and unknown at compile time):
    • In zend_compile_func_array_map(), rewrite the loop placeholder's ZEND_SEND_VAL_EX into ZEND_SEND_USER when 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 throwing Error (covering functions declared later, dynamic callables, static::, parent::, inherited self::, recursive calls, and known by-ref functions/methods).
    • Extend ZEND_SEND_USER (Zend/zend_vm_def.h) to accept CONST|UNUSED|NUM for op2 so named placeholders work with unknown callees as well, and update Zend/Optimizer/compact_literals.c to account for its 2 cache slots when recomputing cache_size.
    • Update Zend/Optimizer/optimize_func_calls.c so ZEND_SEND_USER on a CONST/TMP operand with a known positional non-ref parameter is promoted back to ZEND_SEND_VAL.
  3. Functions forbidding dynamic calls (ZEND_ACC2_FORBID_DYN_CALLS):
    • Bail out when the resolved function (including unqualified calls in namespaces falling back to global functions) or static/self/parent method carries ZEND_ACC2_FORBID_DYN_CALLS (extract, compact, func_get_arg, func_get_args, func_num_args, get_defined_vars), preserving Cannot 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.phpt passes with opcache.enable_cli=0, opcache.enable_cli=1, opcache.jit=tracing, and opcache.jit=function.
  • Zend/tests/*array_map*, ext/opcache/tests/*array_map*, and ext/standard/tests/array/*array_map*: 39 passed, 0 failed.
  • ext/opcache/tests/: 1,028 passed, 45 skipped, 2 expected fail, 0 failed.

@devnexen

Copy link
Copy Markdown
Member

By-reference callbacks

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

prateekbhujel commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor Author

@devnexen Good catch. All 6 cases were still throwing Error with the compile-time-only check.

I updated the PR in a204fc4 to handle this in the emitted send opcode instead:

  • Rewrite the loop placeholder's ZEND_SEND_VAL_EX into ZEND_SEND_USER in zend_compile_func_array_map() when the callee's send mode isn't statically known by-value, so by-ref callees warn and wrap like regular array_map() (known by-value callees keep ZEND_SEND_VAL).
  • Extend ZEND_SEND_USER op2 to CONST|UNUSED|NUM (and update compact_literals.c cache-slot accounting) so named placeholders also work for late/dynamic callees, and let optimize_func_calls.c promote ZEND_SEND_USER back to ZEND_SEND_VAL when OPcache resolves a positional non-ref parameter.

All 6 of your cases plus a late named by-ref placeholder are now covered in Zend/tests/array_map_foreach_optimization_gh25379.phpt and pass under ASan + Debug + ZTS with opcache off, on, and JIT enabled.

@prateekbhujel
prateekbhujel force-pushed the fix/gh25379-array-map-foreach-optimization branch from 146a683 to a204fc4 Compare November 18, 2026 08:01
@devnexen

Copy link
Copy Markdown
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]]

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.

2 participants