1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 16:22:37 +01:00
Files
archived-php-src/ext/opcache/tests/array_map_foreach_optimization_002.phpt
Ilija Tovilo 6173a9a109 VAR|TMP overhaul (GH-20628)
The aim of this PR is twofold:

- Reduce the number of highly similar TMP|VAR handlers
- Avoid ZVAL_DEREF in most of these cases

This is achieved by guaranteeing that all zend_compile_expr() calls, as well as
all other compile calls with BP_VAR_{R,IS}, will result in a TMP variable. This
implies that the result will not contain an IS_INDIRECT or IS_REFERENCE value,
which was mostly already the case, with two exceptions:

- Calls to return-by-reference functions. Because return-by-reference functions
  are quite rare, this is solved by delegating the DEREF to the RETURN_BY_REF
  handler, which will examine the stack to check whether the caller expects a
  VAR or TMP to understand whether the DEREF is needed. Internal functions will
  also need to adjust by calling the zend_return_unwrap_ref() function.

- By-reference assignments, including both $a = &$b, as well as $a = [&$b]. When
  the result of these expressions is used in a BP_VAR_R context, the reference
  is unwrapped via a ZEND_QM_ASSIGN opcode beforehand. This is exceptionally
  rare.

Closes GH-20628
2026-01-31 19:44:56 +01:00

74 lines
1.5 KiB
PHP

--TEST--
array_map(): foreach optimization - Error
--EXTENSIONS--
opcache
--INI--
opcache.enable=1
opcache.enable_cli=1
opcache.opt_debug_level=0x20000
--FILE--
<?php
function plus1($x) {
return $x + 1;
}
$array = \range(1, 10);
$foo = array_map(
plus1(...),
new \ArrayIterator($array)
);
\var_dump($foo);
?>
--EXPECTF--
$_main:
; (lines=%d, args=0, vars=%d, tmps=%d)
; (after optimizer)
; %s
0000 INIT_FCALL 2 %d string("range")
0001 SEND_VAL int(1) 1
0002 SEND_VAL int(10) 2
0003 T2 = DO_ICALL
0004 ASSIGN CV0($array) T2
0005 T5 = NEW 1 string("ArrayIterator")
0006 SEND_VAR%S CV0($array) 1
0007 DO_FCALL
0008 TYPE_ASSERT 131079 string("array_map") T5
0009 T2 = INIT_ARRAY 0 (packed) NEXT
0010 V3 = FE_RESET_R T5 0017
0011 T5 = FE_FETCH_R V3 T4 0017
0012 INIT_FCALL 1 %d string("plus1")
0013 SEND_VAL T4 1
0014 T4 = DO_UCALL
0015 T2 = ADD_ARRAY_ELEMENT T4 T5
0016 JMP 0011
0017 FE_FREE V3
0018 ASSIGN CV1($foo) T2
0019 INIT_FCALL 1 %d string("var_dump")
0020 SEND_VAR CV1($foo) 1
0021 DO_ICALL
0022 RETURN int(1)
LIVE RANGES:
5: 0006 - 0008 (new)
5: 0008 - 0010 (tmp/var)
2: 0010 - 0018 (tmp/var)
3: 0011 - 0017 (loop)
4: 0012 - 0013 (tmp/var)
5: 0012 - 0015 (tmp/var)
plus1:
; (lines=3, args=1, vars=1, tmps=%d)
; (after optimizer)
; %s
0000 CV0($x) = RECV 1
0001 T1 = ADD CV0($x) int(1)
0002 RETURN T1
Fatal error: Uncaught TypeError: array_map(): Argument #2 ($array) must be of type array, ArrayIterator given in %s:9
Stack trace:
#0 {main}
thrown in %s on line 9