Skip to content

Commit d951034

Browse files
committed
Optimize ZEND_COUNT opcodes on arrays in the jit
Avoid the overhead of a call and checking types when the argument is definitely an array. Avoid the overhead of gc when `__destruct` won't get called. This seemed cheap enough to check for in the jit. Because of https://wiki.php.net/rfc/restrict_globals_usage we can be sure in the ZEND_COUNT handler that the array count does not have to be recomputed in php 8.1. The below example took 0.854 seconds before the optimization, and 0.564 seconds after the optimization, giving the same result ```php <?php /** @jit */ function bench_count(int $n): int { $total = 0; $arr = []; for ($i = 0; $i < $n; $i++) { $arr[] = $i; $total += count($arr); } return $total; } function main() { $n = 1000; $iterations = 50000; $start = microtime(true); $result = 0; for ($i = 0; $i < $iterations; $i++) { $result += bench_count($n); } $elapsed = microtime(true) - $start; printf("Total for n=%d, iterations=%d = %d, elapsed=%.3f\n", $n, $iterations, $result, $elapsed); } main(); ``` Before ```asm mov $0x7feb8cf8a858, %r15 mov $ZEND_COUNT_SPEC_CV_UNUSED_HANDLER, %rax call *%rax ``` After ```asm mov 0x70(%r14), %rdi - Copy the count from the `zend_array*` pointer mov %rdi, (%rax) - Store the count in the destination's value mov $0x4, 0x8(%rax) - Store IS_LONG(4) in the destination's type ``` And add tracing jit support Closes GH-5584
1 parent c098952 commit d951034

File tree

4 files changed

+133
-1
lines changed

4 files changed

+133
-1
lines changed

ext/opcache/jit/zend_jit.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3210,6 +3210,15 @@ static int zend_jit(const zend_op_array *op_array, zend_ssa *ssa, const zend_op
32103210
goto jit_failure;
32113211
}
32123212
goto done;
3213+
case ZEND_COUNT:
3214+
op1_info = OP1_INFO();
3215+
if ((op1_info & (MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)) != MAY_BE_ARRAY) {
3216+
break;
3217+
}
3218+
if (!zend_jit_count(&dasm_state, opline, op1_info, OP1_REG_ADDR(), zend_may_throw(opline, ssa_op, op_array, ssa))) {
3219+
goto jit_failure;
3220+
}
3221+
goto done;
32133222
case ZEND_FETCH_THIS:
32143223
if (!zend_jit_fetch_this(&dasm_state, opline, op_array, 0)) {
32153224
goto jit_failure;

ext/opcache/jit/zend_jit_trace.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,7 @@ static zend_ssa *zend_jit_trace_build_tssa(zend_jit_trace_rec *trace_buffer, uin
15811581
/* break missing intentionally */
15821582
case ZEND_ECHO:
15831583
case ZEND_STRLEN:
1584+
case ZEND_COUNT:
15841585
case ZEND_QM_ASSIGN:
15851586
case ZEND_FE_RESET_R:
15861587
case ZEND_FE_FETCH_R:
@@ -5497,6 +5498,28 @@ static const void *zend_jit_trace(zend_jit_trace_rec *trace_buffer, uint32_t par
54975498
goto jit_failure;
54985499
}
54995500
goto done;
5501+
case ZEND_COUNT:
5502+
op1_info = OP1_INFO();
5503+
op1_addr = OP1_REG_ADDR();
5504+
if (orig_op1_type == (IS_TRACE_REFERENCE|IS_ARRAY)) {
5505+
if (!zend_jit_fetch_reference(&dasm_state, opline, orig_op1_type, &op1_info, &op1_addr,
5506+
!ssa->var_info[ssa_op->op1_use].guarded_reference, 1)) {
5507+
goto jit_failure;
5508+
}
5509+
if (opline->op1_type == IS_CV
5510+
&& ssa->vars[ssa_op->op1_use].alias == NO_ALIAS) {
5511+
ssa->var_info[ssa_op->op1_use].guarded_reference = 1;
5512+
}
5513+
} else {
5514+
CHECK_OP1_TRACE_TYPE();
5515+
if ((op1_info & (MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)) != MAY_BE_ARRAY) {
5516+
break;
5517+
}
5518+
}
5519+
if (!zend_jit_count(&dasm_state, opline, op1_info, op1_addr, zend_may_throw(opline, ssa_op, op_array, ssa))) {
5520+
goto jit_failure;
5521+
}
5522+
goto done;
55005523
case ZEND_FETCH_THIS:
55015524
delayed_fetch_this = 0;
55025525
if (ssa_op->result_def >= 0 && opline->result_type != IS_CV) {

ext/opcache/jit/zend_jit_x86.dasc

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9197,7 +9197,7 @@ static int zend_jit_init_fcall(dasm_State **Dst, const zend_op *opline, uint32_t
91979197
#endif
91989198
/* load constant address later */
91999199
} else if (func && op_array == &func->op_array) {
9200-
/* recursive call */
9200+
/* recursive call */
92019201
if (!(func->op_array.fn_flags & ZEND_ACC_IMMUTABLE) ||
92029202
(sizeof(void*) == 8 && !IS_SIGNED_32BIT(func))) {
92039203
| mov r0, EX->func
@@ -14244,6 +14244,38 @@ static int zend_jit_strlen(dasm_State **Dst, const zend_op *opline, uint32_t op1
1424414244
return 1;
1424514245
}
1424614246

14247+
static int zend_jit_count(dasm_State **Dst, const zend_op *opline, uint32_t op1_info, zend_jit_addr op1_addr, int may_throw)
14248+
{
14249+
zend_jit_addr res_addr = RES_ADDR();
14250+
14251+
if (opline->op1_type == IS_CONST) {
14252+
zval *zv;
14253+
zend_long count;
14254+
14255+
zv = RT_CONSTANT(opline, opline->op1);
14256+
ZEND_ASSERT(Z_TYPE_P(zv) == IS_ARRAY);
14257+
count = zend_hash_num_elements(Z_ARRVAL_P(zv));
14258+
14259+
| SET_ZVAL_LVAL res_addr, count
14260+
| SET_ZVAL_TYPE_INFO res_addr, IS_LONG
14261+
} else {
14262+
ZEND_ASSERT((op1_info & (MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)) == MAY_BE_ARRAY);
14263+
// Note: See the implementation of ZEND_COUNT in Zend/zend_vm_def.h - arrays do not contain IS_UNDEF starting in php 8.1+.
14264+
14265+
| GET_ZVAL_PTR r0, op1_addr
14266+
// Sign-extend the 32-bit value to a potentially 64-bit zend_long
14267+
| mov eax, dword [r0 + offsetof(HashTable, nNumOfElements)]
14268+
| SET_ZVAL_LVAL res_addr, r0
14269+
| SET_ZVAL_TYPE_INFO res_addr, IS_LONG
14270+
| FREE_OP opline->op1_type, opline->op1, op1_info, 0, opline
14271+
}
14272+
14273+
if (may_throw) {
14274+
return zend_jit_check_exception(Dst);
14275+
}
14276+
return 1;
14277+
}
14278+
1424714279
static int zend_jit_load_this(dasm_State **Dst, uint32_t var)
1424814280
{
1424914281
zend_jit_addr var_addr = ZEND_ADDR_MEM_ZVAL(ZREG_FP, var);

ext/opcache/tests/jit/count_001.phpt

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
--TEST--
2+
JIT COUNT: 001
3+
--INI--
4+
opcache.enable=1
5+
opcache.enable_cli=1
6+
opcache.file_update_protection=0
7+
opcache.jit_buffer_size=1M
8+
;opcache.jit_debug=1
9+
--SKIPIF--
10+
<?php require_once('skipif.inc'); ?>
11+
--FILE--
12+
<?php
13+
class ThrowsInDestructor {
14+
public function __destruct() {
15+
throw new RuntimeException("In destructor");
16+
}
17+
}
18+
class C {
19+
public static function create_array(int $i): array {
20+
return array_fill(0, $i, new stdClass());
21+
}
22+
23+
public static function foo() {
24+
$x = [self::create_array(5)];
25+
echo count(self::create_array(0)), "\n";
26+
echo count(self::create_array(1)), "\n";
27+
echo count($x[0]), "\n";
28+
$a = [];
29+
for ($i = 0; $i < 4; $i++) {
30+
$a[] = $i;
31+
echo count($a) . "\n";
32+
}
33+
}
34+
public static function count_ref(array &$ref): int {
35+
return count($ref);
36+
}
37+
38+
public static function count_throws(): int {
39+
$result = count([new ThrowsInDestructor()]);
40+
echo "Unreachable\n";
41+
return $result;
42+
}
43+
}
44+
C::foo();
45+
$x = ['x', 'y', 'z', 'a', new stdClass()];
46+
echo C::count_ref($x), "\n";
47+
for ($i = 0; $i < 5; $i++) {
48+
try {
49+
echo C::count_throws(), "\n";
50+
} catch (RuntimeException $e) {
51+
printf("Caught %s\n", $e->getMessage());
52+
}
53+
}
54+
55+
--EXPECT--
56+
0
57+
1
58+
5
59+
1
60+
2
61+
3
62+
4
63+
5
64+
Caught In destructor
65+
Caught In destructor
66+
Caught In destructor
67+
Caught In destructor
68+
Caught In destructor

0 commit comments

Comments
 (0)