diff --git a/ansible/roles/common/tasks/main.yml b/ansible/roles/common/tasks/main.yml index 52bad1c..0ddba52 100644 --- a/ansible/roles/common/tasks/main.yml +++ b/ansible/roles/common/tasks/main.yml @@ -29,3 +29,4 @@ - htop - strace - tcpdump + - hyperfine diff --git a/php8.6/.gitignore b/php8.6/.gitignore new file mode 100644 index 0000000..fbca225 --- /dev/null +++ b/php8.6/.gitignore @@ -0,0 +1 @@ +results/ diff --git a/php8.6/README.md b/php8.6/README.md new file mode 100644 index 0000000..8a2bd74 --- /dev/null +++ b/php8.6/README.md @@ -0,0 +1,84 @@ +# PHP 8.6 micro benchmarks + +One snippet per function or syntax that PHP 8.6 made faster, measured with +[hyperfine](https://github.com/sharkdp/hyperfine) as described in +[How we use hyperfine to measure PHP Engine performance](https://tideways.com/profiler/blog/how-we-use-hyperfine-to-measure-php-engine-performance). + +## Running + +```bash +./run.sh # all benchmarks, PHP 8.5 vs 8.6 +./run.sh array_reduce array_intersect # selected benchmarks +./run.sh --check # each snippet once per version, compares output +VERSIONS="8.4 8.5 8.6" ./run.sh # more versions, the first is the baseline +JIT=tracing ./run.sh # with the tracing JIT +CPU=3 RUNS=20 ./run.sh # pinned to CPU 3 with taskset, 20 runs +``` + +`run.sh` uses the deb.sury.org binaries `/usr/bin/php` that the Ansible +playbook installs, and runs them like this: + +``` +/usr/bin/php8.6 -n -d opcache.enable_cli=1 -d opcache.jit=disable [-d extension=…] .php +``` + +- `-n` ignores `php.ini` and `conf.d`, so the Tideways extension and any INI + differences between versions don't affect the result. Benchmarks that need + a shared extension declare it in a `// php-extensions: mbstring` comment. +- OPcache is enabled and checked on startup. For PHP < 8.5 it is loaded with + `-d zend_extension=opcache`. +- Before measuring, each snippet runs once per version. It must exit + successfully and print the same result everywhere, otherwise the versions + would be doing different work. +- hyperfine runs with `--shell=none`, `--warmup 2` and `--runs 10`. Results go + to `results/jit-/.{md,json}`, and a combined + `summary.md` gives each version's speedup over the first version. + +On PHP 8.5 every benchmark takes about 0.1–1.2 s. Before taking numbers for +publication, disable turbo boost and set the CPU governor to `performance`. + +## Benchmarks + +| Benchmark | Change in 8.6 | Upstream | +|---|---|---| +| `array_reduce` | Array accumulator no longer copied per callback: O(n²) → O(n) | [#21340](https://github.com/php/php-src/pull/21340) | +| `preg_replace_callback` | Modifying `$matches` in the callback no longer copies it | [#21340](https://github.com/php/php-src/pull/21340) | +| `array_intersect` | Hash-based matching instead of sorting | [#23019](https://github.com/php/php-src/pull/23019) | +| `vm_stack_recursion` | Freed VM stack pages are cached; deep recursion on a fragmented heap (~470 MB) | [#23470](https://github.com/php/php-src/pull/23470) | +| `closure_static` | Stateless closure cache for `static fn` without captures | [RFC](https://wiki.php.net/rfc/closure-optimizations#stateless_closure_caching) | +| `first_class_callable` | `dummy(...)` is cached instead of allocated every time | [#19863](https://github.com/php/php-src/pull/19863) | +| `array_map_fcc` | `array_map(foo(...), $a)` compiled into a `foreach` | [#20934](https://github.com/php/php-src/pull/20934) | +| `array_map_fcc_strict` | Control: the rewrite is disabled under `strict_types=1` | [#23889](https://github.com/php/php-src/pull/23889) | +| `array_map_multiple` | `array_map()` with several arrays | [79adb01bf](https://github.com/php/php-src/commit/79adb01bf) | +| `array_sum_product` | Integer-only fast path | [#21787](https://github.com/php/php-src/pull/21787) | +| `str_pad` | memset / doubling copies | [#19272](https://github.com/php/php-src/pull/19272), [#23661](https://github.com/php/php-src/pull/23661) | +| `mb_str_pad` | Doubling copies | [#23667](https://github.com/php/php-src/pull/23667) | +| `asymmetric_visibility` | Writes to `private(set)` / `protected(set)` properties | [#22709](https://github.com/php/php-src/pull/22709) | +| `new_known_constructor` | `new self(...)` with named args passes args like a static call | [#20259](https://github.com/php/php-src/pull/20259) | +| `printf` | `printf('%s-%d', …)` compiled to interpolation | [#19658](https://github.com/php/php-src/pull/19658) | +| `uri_rfc3986_getters` | Getters on already-normalized `Uri\Rfc3986\Uri` | [#21726](https://github.com/php/php-src/pull/21726) | +| `json_encode` | Arrays/objects and `JSON_PRETTY_PRINT` indentation | [#21474](https://github.com/php/php-src/pull/21474) | +| `json_decode` | Regression check: error locations were added, then moved off the success path | [#20629](https://github.com/php/php-src/pull/20629), [#22487](https://github.com/php/php-src/pull/22487) | +| `stream_copy_to_socket` | `stream_copy_to_stream()` file → socket via `sendfile()` (64 MB × 20, forks a reader) | [#20399](https://github.com/php/php-src/pull/20399) | +| `pathinfo` | Single `PATHINFO_*` component | [#23726](https://github.com/php/php-src/pull/23726) | +| `array_column` | Key normalization moved out of the loop | [#23786](https://github.com/php/php-src/pull/23786) | +| `array_unshift` | Packed array fast path | [#20353](https://github.com/php/php-src/pull/20353) | +| `array_walk` | Less per-element callback overhead | [#20322](https://github.com/php/php-src/pull/20322) | +| `array_fill_keys` | Optimized | [#20347](https://github.com/php/php-src/pull/20347) | +| `sort_single_element` | `sort()`/`usort()`/`asort()` on one element | [#23550](https://github.com/php/php-src/pull/23550) | +| `str_split` | Optimized, especially length 1 | [#20419](https://github.com/php/php-src/pull/20419) | +| `intval_binary` | `intval('0b…', 2)` without copying | [0af87e970](https://github.com/php/php-src/commit/0af87e970) | +| `addcslashes` | Octal escape generation | [#23680](https://github.com/php/php-src/pull/23680) | +| `str_repeat_one` | `str_repeat($s, 1)` returns the input | [#23681](https://github.com/php/php-src/pull/23681) | +| `dom_split_text` | `DOMText::splitText()` | [#20371](https://github.com/php/php-src/pull/20371) | +| `collator_sort_with_sort_keys` | No fixed 2 × 1 MiB buffers per call | [#23504](https://github.com/php/php-src/pull/23504) | +| `zip_add_from_string` | `ZipArchive::addFromString()` without copying | [#20497](https://github.com/php/php-src/pull/20497) | + +## Not covered + +These need a different build or platform, not a snippet: + +- ZTS speedups (FrankenPHP, threaded builds) +- TAILCALL VM changes (Clang builds, Windows clang-cl) +- GD BMP/WBMP/GD output buffering (`php-gd` isn't installed) +- Windows-only `imagegrabscreen()` diff --git a/php8.6/addcslashes.php b/php8.6/addcslashes.php new file mode 100644 index 0000000..ed849bf --- /dev/null +++ b/php8.6/addcslashes.php @@ -0,0 +1,21 @@ + $i, 'name' => "name $i", 'email' => "user$i@example.com"]; + } + + $count = 0; + $i = 20_000; + while ($i--) { + $count += count(array_column($rows, 'name')); + $count += count(array_column($rows, 'email', 'id')); + } + + echo $count, PHP_EOL; +} + +main(); diff --git a/php8.6/array_fill_keys.php b/php8.6/array_fill_keys.php new file mode 100644 index 0000000..6bc8728 --- /dev/null +++ b/php8.6/array_fill_keys.php @@ -0,0 +1,19 @@ + "key_$v", $intKeys); + + $count = 0; + $i = 50_000; + while ($i--) { + $count += count(array_fill_keys($intKeys, 0)); + $count += count(array_fill_keys($stringKeys, null)); + } + + echo $count, PHP_EOL; +} + +main(); diff --git a/php8.6/array_intersect.php b/php8.6/array_intersect.php new file mode 100644 index 0000000..eb1f8f2 --- /dev/null +++ b/php8.6/array_intersect.php @@ -0,0 +1,22 @@ + "value_$v", $a); + $strB = array_map(static fn (int $v): string => "value_$v", $b); + + $count = 0; + $i = 100; + while ($i--) { + $count += count(array_intersect($a, $b)); + $count += count(array_intersect($strA, $strB)); + } + + echo $count, PHP_EOL; +} + +main(); diff --git a/php8.6/array_map_fcc.php b/php8.6/array_map_fcc.php new file mode 100644 index 0000000..d3f95af --- /dev/null +++ b/php8.6/array_map_fcc.php @@ -0,0 +1,26 @@ +userland call overhead. +// Only without strict_types, see array_map_fcc_strict.php. +// https://github.com/php/php-src/pull/20934 + +function increment(int $x): int +{ + return $x + 1; +} + +function main(): void +{ + $array = range(1, 1_000); + $sum = 0; + + $i = 20_000; + while ($i--) { + $result = array_map(increment(...), $array); + $sum += $result[999]; + } + + echo $sum, PHP_EOL; +} + +main(); diff --git a/php8.6/array_map_fcc_strict.php b/php8.6/array_map_fcc_strict.php new file mode 100644 index 0000000..ce46ad6 --- /dev/null +++ b/php8.6/array_map_fcc_strict.php @@ -0,0 +1,27 @@ + $x + $y; + + $sum = 0; + $i = 20_000; + while ($i--) { + $result = array_map($callback, $a, $b); + $sum += $result[999]; + } + + echo $sum, PHP_EOL; +} + +main(); diff --git a/php8.6/array_reduce.php b/php8.6/array_reduce.php new file mode 100644 index 0000000..674c09f --- /dev/null +++ b/php8.6/array_reduce.php @@ -0,0 +1,22 @@ +value = $i; + } + } +} + +class ProtectedSetBase +{ + public protected(set) int $value = 0; +} + +final class ProtectedSetChild extends ProtectedSetBase +{ + public function fill(int $n): void + { + for ($i = 0; $i < $n; $i++) { + $this->value = $i; + } + } +} + +function main(): void +{ + $private = new PrivateSet(); + $protected = new ProtectedSetChild(); + $private->fill(20_000_000); + $protected->fill(20_000_000); + + echo $private->value + $protected->value, PHP_EOL; +} + +main(); diff --git a/php8.6/closure_static.php b/php8.6/closure_static.php new file mode 100644 index 0000000..33ba930 --- /dev/null +++ b/php8.6/closure_static.php @@ -0,0 +1,23 @@ + $x + 1, $i); + } + + echo $sum, PHP_EOL; +} + +main(); diff --git a/php8.6/collator_sort_with_sort_keys.php b/php8.6/collator_sort_with_sort_keys.php new file mode 100644 index 0000000..226167d --- /dev/null +++ b/php8.6/collator_sort_with_sort_keys.php @@ -0,0 +1,23 @@ +sortWithSortKeys($copy); + $count += count($copy); + } + + echo $count, ' ', implode(',', $copy), PHP_EOL; +} + +main(); diff --git a/php8.6/dom_split_text.php b/php8.6/dom_split_text.php new file mode 100644 index 0000000..ede48af --- /dev/null +++ b/php8.6/dom_split_text.php @@ -0,0 +1,25 @@ +appendChild($document->createElement('root')); + + $length = 0; + $i = 2_000; + while ($i--) { + $root->textContent = ''; + $text = $root->appendChild($document->createTextNode(str_repeat('Lorem ipsum dolor sit amet. ', 40))); + while ($text->length > 8) { + $text = $text->splitText(8); + } + $length += $root->childNodes->length; + } + + echo $length, PHP_EOL; +} + +main(); diff --git a/php8.6/first_class_callable.php b/php8.6/first_class_callable.php new file mode 100644 index 0000000..af3bf6d --- /dev/null +++ b/php8.6/first_class_callable.php @@ -0,0 +1,25 @@ + $i, + 'name' => "item_\u{00e9}\u{2603}_$i", + 'active' => $i % 2 === 0, + 'score' => $i * 1.5 + 0.25, + 'tags' => ['alpha', 'beta', "gamma\ndelta", 'quote"inside'], + 'meta' => ['nested' => ['deep' => ['value' => 'text with "quotes" and \\ backslash and /slash']]], + 'note' => null, + 'big' => 123456789012345, + ]; + } + $json = json_encode(['rows' => $rows, 'count' => 30, 'ok' => true, 'ratio' => 3.14159]); + + $count = 0; + $i = 50_000; + while ($i--) { + $count += count(json_decode($json, true)['rows']); + } + + echo $count, PHP_EOL; +} + +main(); diff --git a/php8.6/json_encode.php b/php8.6/json_encode.php new file mode 100644 index 0000000..345ceec --- /dev/null +++ b/php8.6/json_encode.php @@ -0,0 +1,28 @@ + $i, + 'name' => "item $i", + 'tags' => ['alpha', 'beta', 'gamma'], + 'meta' => ['nested' => ['deep' => ['deeper' => ['value' => $i, 'flag' => true]]]], + 'object' => (object) ['a' => 1, 'b' => [1, 2, 3]], + ]; + } + + $length = 0; + $i = 5_000; + while ($i--) { + $length += strlen(json_encode($data)); + $length += strlen(json_encode($data, JSON_PRETTY_PRINT)); + } + + echo $length, PHP_EOL; +} + +main(); diff --git a/php8.6/mb_str_pad.php b/php8.6/mb_str_pad.php new file mode 100644 index 0000000..a50f11c --- /dev/null +++ b/php8.6/mb_str_pad.php @@ -0,0 +1,17 @@ +bar; + } + + echo $sum, PHP_EOL; +} + +main(); diff --git a/php8.6/pathinfo.php b/php8.6/pathinfo.php new file mode 100644 index 0000000..d5443ef --- /dev/null +++ b/php8.6/pathinfo.php @@ -0,0 +1,20 @@ +) on the CLI with OPcache +# enabled, following https://tideways.com/profiler/blog/how-we-use-hyperfine-to-measure-php-engine-performance +# +# Usage: +# ./run.sh run all benchmarks +# ./run.sh array_reduce str_pad run selected benchmarks +# ./run.sh --check run each snippet once per version only, +# compare the output and print timings +# +# Environment: +# VERSIONS="8.5 8.6" PHP versions to compare, the first one is the baseline +# JIT=off off, tracing or function +# WARMUP=2 hyperfine --warmup +# RUNS=10 hyperfine --runs +# CPU= pin the benchmark to this CPU with taskset, e.g. CPU=3 +# RESULTS=results/jit-$JIT where Markdown/JSON exports are written + +set -euo pipefail +cd "$(dirname "$0")" + +VERSIONS=${VERSIONS:-"8.5 8.6"} +JIT=${JIT:-off} +WARMUP=${WARMUP:-2} +RUNS=${RUNS:-10} +CPU=${CPU:-} +RESULTS=${RESULTS:-results/jit-$JIT} + +check_only=0 +if [[ ${1:-} == --check ]]; then + check_only=1 + shift +fi + +if (($# > 0)); then + benchmarks=("${@%.php}") +else + benchmarks=() + for file in *.php; do + benchmarks+=("${file%.php}") + done +fi + +read -r -a versions <<<"$VERSIONS" +declare -A base_command + +for version in "${versions[@]}"; do + binary=/usr/bin/php$version + if [[ ! -x $binary ]]; then + echo "Missing $binary, install php$version-cli from deb.sury.org" >&2 + exit 1 + fi + + # -n: ignore php.ini and conf.d so that no extension (e.g. Tideways, Xdebug) + # and no differing INI defaults distort the comparison between versions. + command="$binary -n -d opcache.enable_cli=1" + if ! "$binary" -n -m | grep -qx 'Zend OPcache'; then + # OPcache is only built in from PHP 8.5 on. + command+=" -d zend_extension=opcache" + fi + case $JIT in + off) command+=" -d opcache.jit=disable" ;; + tracing | function) command+=" -d opcache.jit=$JIT -d opcache.jit_buffer_size=128M" ;; + *) + echo "Unknown JIT=$JIT, use off, tracing or function" >&2 + exit 1 + ;; + esac + if [[ -n $CPU ]]; then + command="taskset -c $CPU $command" + fi + + if ! $command -r 'exit(is_array(opcache_get_status(false)) ? 0 : 1);' 2>/dev/null; then + echo "OPcache is not active for PHP $version: $command" >&2 + exit 1 + fi + + base_command[$version]=$command +done + +php_command() { + local version=$1 benchmark=$2 command extension + command=${base_command[$version]} + for extension in $(sed -n 's#^// php-extensions: ##p' "$benchmark.php"); do + command+=" -d extension=$extension" + done + echo "$command $benchmark.php" +} + +# Every snippet must succeed and print the same result on all versions, +# otherwise the benchmark compares different work. +preflight() { + local benchmark=$1 version output expected="" start end + for version in "${versions[@]}"; do + start=$(date +%s%N) + if ! output=$($(php_command "$version" "$benchmark") 2>&1); then + echo "FAIL $benchmark on PHP $version:" >&2 + echo "$output" >&2 + return 1 + fi + end=$(date +%s%N) + if ((check_only)); then + printf '%-32s PHP %-5s %8d ms %s\n' "$benchmark" "$version" $(((end - start) / 1000000)) "$output" + fi + if [[ -z $expected ]]; then + expected=$output + elif [[ $output != "$expected" ]]; then + echo "WARN $benchmark prints different output on PHP $version: '$output' vs '$expected'" >&2 + fi + done +} + +failed=() +if ((check_only)); then + for benchmark in "${benchmarks[@]}"; do + preflight "$benchmark" || failed+=("$benchmark") + done + if ((${#failed[@]} > 0)); then + echo "Failed: ${failed[*]}" >&2 + exit 1 + fi + exit 0 +fi + +if ! command -v hyperfine >/dev/null; then + echo "hyperfine is not installed: apt install hyperfine" >&2 + exit 1 +fi + +mkdir -p "$RESULTS" + +for benchmark in "${benchmarks[@]}"; do + if ! preflight "$benchmark"; then + failed+=("$benchmark") + continue + fi + + args=( + --warmup "$WARMUP" + --runs "$RUNS" + --shell=none + --export-markdown "$RESULTS/$benchmark.md" + --export-json "$RESULTS/$benchmark.json" + ) + for version in "${versions[@]}"; do + args+=(--command-name "PHP $version" "$(php_command "$version" "$benchmark")") + done + + echo + echo "### $benchmark" + hyperfine "${args[@]}" +done + +# Summary table: mean wall time per version and speedup against the baseline. +"/usr/bin/php${versions[0]}" -n -- "$RESULTS" "${benchmarks[@]}" >"$RESULTS/summary.md" <<'PHP' + $result) { + $versions[$index] = $result['command']; + $cell = sprintf('%.1f ms ± %.1f', $result['mean'] * 1000, $result['stddev'] * 1000); + if ($index > 0) { + $cell .= sprintf(' (**%.2f×**)', $baseline / $result['mean']); + } + $row[] = $cell; + } + $rows[] = $row; +} +echo '| Benchmark | ', implode(' | ', $versions), " |\n"; +echo '|---', str_repeat('|---', count($versions)), "|\n"; +foreach ($rows as $row) { + echo '| ', implode(' | ', $row), " |\n"; +} +PHP + +echo +echo "Summary written to $RESULTS/summary.md (speedup relative to PHP ${versions[0]}):" +echo +cat "$RESULTS/summary.md" + +if ((${#failed[@]} > 0)); then + echo + echo "Failed: ${failed[*]}" >&2 + exit 1 +fi diff --git a/php8.6/sort_single_element.php b/php8.6/sort_single_element.php new file mode 100644 index 0000000..cdce29b --- /dev/null +++ b/php8.6/sort_single_element.php @@ -0,0 +1,23 @@ + $a <=> $b; + + $sum = 0; + $i = 5_000_000; + while ($i--) { + $a = [$i]; + sort($a); + usort($a, $compare); + $h = ['k' => $i]; + asort($h); + $sum += $a[0] + $h['k']; + } + + echo $sum, PHP_EOL; +} + +main(); diff --git a/php8.6/str_pad.php b/php8.6/str_pad.php new file mode 100644 index 0000000..a31446f --- /dev/null +++ b/php8.6/str_pad.php @@ -0,0 +1,18 @@ +getScheme()) + + strlen($uri->getHost()) + + strlen($uri->getPath()) + + strlen($uri->getQuery()) + + strlen($uri->getFragment()) + + strlen($uri->toString()); + } + + echo $length, PHP_EOL; +} + +main(); diff --git a/php8.6/vm_stack_recursion.php b/php8.6/vm_stack_recursion.php new file mode 100644 index 0000000..eabc694 --- /dev/null +++ b/php8.6/vm_stack_recursion.php @@ -0,0 +1,36 @@ +open($file, ZipArchive::CREATE | ZipArchive::OVERWRITE); + for ($j = 0; $j < 200; $j++) { + $zip->addFromString("file-$j.txt", $content); + } + $count += $zip->numFiles; + $zip->unchangeAll(); + $zip->close(); + } + @unlink($file); + + echo $count, PHP_EOL; +} + +main();