diff options
author | Tyson Andre <tysonandre775@hotmail.com> | 2020-05-16 17:32:16 -0400 |
---|---|---|
committer | Tyson Andre <tysonandre775@hotmail.com> | 2021-02-09 16:09:32 -0500 |
commit | d9510342eeac6dd03fdbad83b6374f6cedfbfecc (patch) | |
tree | 3491c382c5cda0b6d583a8df20d76b310604dbcb /ext/opcache/tests/jit | |
parent | c098952d728a0f73290e1900122139962ed1229c (diff) | |
download | php-git-d9510342eeac6dd03fdbad83b6374f6cedfbfecc.tar.gz |
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
Diffstat (limited to 'ext/opcache/tests/jit')
-rw-r--r-- | ext/opcache/tests/jit/count_001.phpt | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/ext/opcache/tests/jit/count_001.phpt b/ext/opcache/tests/jit/count_001.phpt new file mode 100644 index 0000000000..c76206f3aa --- /dev/null +++ b/ext/opcache/tests/jit/count_001.phpt @@ -0,0 +1,68 @@ +--TEST-- +JIT COUNT: 001 +--INI-- +opcache.enable=1 +opcache.enable_cli=1 +opcache.file_update_protection=0 +opcache.jit_buffer_size=1M +;opcache.jit_debug=1 +--SKIPIF-- +<?php require_once('skipif.inc'); ?> +--FILE-- +<?php +class ThrowsInDestructor { + public function __destruct() { + throw new RuntimeException("In destructor"); + } +} +class C { + public static function create_array(int $i): array { + return array_fill(0, $i, new stdClass()); + } + + public static function foo() { + $x = [self::create_array(5)]; + echo count(self::create_array(0)), "\n"; + echo count(self::create_array(1)), "\n"; + echo count($x[0]), "\n"; + $a = []; + for ($i = 0; $i < 4; $i++) { + $a[] = $i; + echo count($a) . "\n"; + } + } + public static function count_ref(array &$ref): int { + return count($ref); + } + + public static function count_throws(): int { + $result = count([new ThrowsInDestructor()]); + echo "Unreachable\n"; + return $result; + } +} +C::foo(); +$x = ['x', 'y', 'z', 'a', new stdClass()]; +echo C::count_ref($x), "\n"; +for ($i = 0; $i < 5; $i++) { + try { + echo C::count_throws(), "\n"; + } catch (RuntimeException $e) { + printf("Caught %s\n", $e->getMessage()); + } +} + +--EXPECT-- +0 +1 +5 +1 +2 +3 +4 +5 +Caught In destructor +Caught In destructor +Caught In destructor +Caught In destructor +Caught In destructor
\ No newline at end of file |