From 7abc66cffd0d291f9274e872d7cffaef84d669e0 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sun, 15 Nov 2026 20:36:41 +0100 Subject: [PATCH 1/2] Add benchmarks for individual function improvements based on UPGRADING and NEWS using hyperfine --- ansible/roles/common/tasks/main.yml | 1 + php8.6/.gitignore | 1 + php8.6/README.md | 84 ++++++++++ php8.6/addcslashes.php | 16 ++ php8.6/array_column.php | 17 ++ php8.6/array_fill_keys.php | 14 ++ php8.6/array_intersect.php | 17 ++ php8.6/array_map_fcc.php | 21 +++ php8.6/array_map_fcc_strict.php | 22 +++ php8.6/array_map_multiple.php | 16 ++ php8.6/array_reduce.php | 17 ++ php8.6/array_sum_product.php | 18 +++ php8.6/array_unshift.php | 14 ++ php8.6/array_walk.php | 15 ++ php8.6/asymmetric_visibility.php | 38 +++++ php8.6/closure_static.php | 18 +++ php8.6/collator_sort_with_sort_keys.php | 18 +++ php8.6/dom_split_text.php | 20 +++ php8.6/first_class_callable.php | 20 +++ php8.6/intval_binary.php | 15 ++ php8.6/json_decode.php | 27 ++++ php8.6/json_encode.php | 23 +++ php8.6/mb_str_pad.php | 12 ++ php8.6/new_known_constructor.php | 27 ++++ php8.6/pathinfo.php | 15 ++ php8.6/preg_replace_callback.php | 19 +++ php8.6/printf.php | 15 ++ php8.6/run.sh | 199 ++++++++++++++++++++++++ php8.6/sort_single_element.php | 18 +++ php8.6/str_pad.php | 13 ++ php8.6/str_repeat_one.php | 13 ++ php8.6/str_split.php | 14 ++ php8.6/stream_copy_to_socket.php | 43 +++++ php8.6/uri_rfc3986_getters.php | 18 +++ php8.6/vm_stack_recursion.php | 31 ++++ php8.6/zip_add_from_string.php | 23 +++ 36 files changed, 912 insertions(+) create mode 100644 php8.6/.gitignore create mode 100644 php8.6/README.md create mode 100644 php8.6/addcslashes.php create mode 100644 php8.6/array_column.php create mode 100644 php8.6/array_fill_keys.php create mode 100644 php8.6/array_intersect.php create mode 100644 php8.6/array_map_fcc.php create mode 100644 php8.6/array_map_fcc_strict.php create mode 100644 php8.6/array_map_multiple.php create mode 100644 php8.6/array_reduce.php create mode 100644 php8.6/array_sum_product.php create mode 100644 php8.6/array_unshift.php create mode 100644 php8.6/array_walk.php create mode 100644 php8.6/asymmetric_visibility.php create mode 100644 php8.6/closure_static.php create mode 100644 php8.6/collator_sort_with_sort_keys.php create mode 100644 php8.6/dom_split_text.php create mode 100644 php8.6/first_class_callable.php create mode 100644 php8.6/intval_binary.php create mode 100644 php8.6/json_decode.php create mode 100644 php8.6/json_encode.php create mode 100644 php8.6/mb_str_pad.php create mode 100644 php8.6/new_known_constructor.php create mode 100644 php8.6/pathinfo.php create mode 100644 php8.6/preg_replace_callback.php create mode 100644 php8.6/printf.php create mode 100755 php8.6/run.sh create mode 100644 php8.6/sort_single_element.php create mode 100644 php8.6/str_pad.php create mode 100644 php8.6/str_repeat_one.php create mode 100644 php8.6/str_split.php create mode 100644 php8.6/stream_copy_to_socket.php create mode 100644 php8.6/uri_rfc3986_getters.php create mode 100644 php8.6/vm_stack_recursion.php create mode 100644 php8.6/zip_add_from_string.php 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..8f7779a --- /dev/null +++ b/php8.6/addcslashes.php @@ -0,0 +1,16 @@ + $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; diff --git a/php8.6/array_fill_keys.php b/php8.6/array_fill_keys.php new file mode 100644 index 0000000..61ac113 --- /dev/null +++ b/php8.6/array_fill_keys.php @@ -0,0 +1,14 @@ + "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; diff --git a/php8.6/array_intersect.php b/php8.6/array_intersect.php new file mode 100644 index 0000000..f289ae3 --- /dev/null +++ b/php8.6/array_intersect.php @@ -0,0 +1,17 @@ + "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; diff --git a/php8.6/array_map_fcc.php b/php8.6/array_map_fcc.php new file mode 100644 index 0000000..797cc97 --- /dev/null +++ b/php8.6/array_map_fcc.php @@ -0,0 +1,21 @@ +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; +} + +$array = range(1, 1_000); +$sum = 0; + +$i = 20_000; +while ($i--) { + $result = array_map(increment(...), $array); + $sum += $result[999]; +} + +echo $sum, PHP_EOL; diff --git a/php8.6/array_map_fcc_strict.php b/php8.6/array_map_fcc_strict.php new file mode 100644 index 0000000..6fcc2ef --- /dev/null +++ b/php8.6/array_map_fcc_strict.php @@ -0,0 +1,22 @@ + $x + $y; + +$sum = 0; +$i = 20_000; +while ($i--) { + $result = array_map($callback, $a, $b); + $sum += $result[999]; +} + +echo $sum, PHP_EOL; diff --git a/php8.6/array_reduce.php b/php8.6/array_reduce.php new file mode 100644 index 0000000..efdb8d2 --- /dev/null +++ b/php8.6/array_reduce.php @@ -0,0 +1,17 @@ +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; + } + } +} + +$private = new PrivateSet(); +$protected = new ProtectedSetChild(); +$private->fill(20_000_000); +$protected->fill(20_000_000); + +echo $private->value + $protected->value, PHP_EOL; diff --git a/php8.6/closure_static.php b/php8.6/closure_static.php new file mode 100644 index 0000000..bffa3ee --- /dev/null +++ b/php8.6/closure_static.php @@ -0,0 +1,18 @@ + $x + 1, $i); +} + +echo $sum, PHP_EOL; 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..5991c15 --- /dev/null +++ b/php8.6/collator_sort_with_sort_keys.php @@ -0,0 +1,18 @@ +sortWithSortKeys($copy); + $count += count($copy); +} + +echo $count, ' ', implode(',', $copy), PHP_EOL; diff --git a/php8.6/dom_split_text.php b/php8.6/dom_split_text.php new file mode 100644 index 0000000..3de0223 --- /dev/null +++ b/php8.6/dom_split_text.php @@ -0,0 +1,20 @@ +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; diff --git a/php8.6/first_class_callable.php b/php8.6/first_class_callable.php new file mode 100644 index 0000000..118457c --- /dev/null +++ b/php8.6/first_class_callable.php @@ -0,0 +1,20 @@ + $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; diff --git a/php8.6/json_encode.php b/php8.6/json_encode.php new file mode 100644 index 0000000..fd871b0 --- /dev/null +++ b/php8.6/json_encode.php @@ -0,0 +1,23 @@ + $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; diff --git a/php8.6/mb_str_pad.php b/php8.6/mb_str_pad.php new file mode 100644 index 0000000..7a3819b --- /dev/null +++ b/php8.6/mb_str_pad.php @@ -0,0 +1,12 @@ +bar; +} + +echo $sum, PHP_EOL; diff --git a/php8.6/pathinfo.php b/php8.6/pathinfo.php new file mode 100644 index 0000000..8963a5d --- /dev/null +++ b/php8.6/pathinfo.php @@ -0,0 +1,15 @@ +) 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..a806368 --- /dev/null +++ b/php8.6/sort_single_element.php @@ -0,0 +1,18 @@ + $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; diff --git a/php8.6/str_pad.php b/php8.6/str_pad.php new file mode 100644 index 0000000..e231308 --- /dev/null +++ b/php8.6/str_pad.php @@ -0,0 +1,13 @@ +getScheme()) + + strlen($uri->getHost()) + + strlen($uri->getPath()) + + strlen($uri->getQuery()) + + strlen($uri->getFragment()) + + strlen($uri->toString()); +} + +echo $length, PHP_EOL; diff --git a/php8.6/vm_stack_recursion.php b/php8.6/vm_stack_recursion.php new file mode 100644 index 0000000..395f8e5 --- /dev/null +++ b/php8.6/vm_stack_recursion.php @@ -0,0 +1,31 @@ +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; From 4f0db911e9e5c3f801fd374eb7cb13979bc41ad2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Mon, 16 Nov 2026 14:16:42 +0100 Subject: [PATCH 2/2] Wrap top-level code in microbenchmarks into a `main()` function --- php8.6/addcslashes.php | 23 +++++---- php8.6/array_column.php | 25 +++++---- php8.6/array_fill_keys.php | 23 +++++---- php8.6/array_intersect.php | 25 +++++---- php8.6/array_map_fcc.php | 19 ++++--- php8.6/array_map_fcc_strict.php | 19 ++++--- php8.6/array_map_multiple.php | 23 +++++---- php8.6/array_reduce.php | 23 +++++---- php8.6/array_sum_product.php | 27 ++++++---- php8.6/array_unshift.php | 19 ++++--- php8.6/array_walk.php | 23 +++++---- php8.6/asymmetric_visibility.php | 15 ++++-- php8.6/closure_static.php | 15 ++++-- php8.6/collator_sort_with_sort_keys.php | 23 +++++---- php8.6/dom_split_text.php | 27 ++++++---- php8.6/first_class_callable.php | 13 +++-- php8.6/intval_binary.php | 19 ++++--- php8.6/json_decode.php | 43 +++++++++------- php8.6/json_encode.php | 37 ++++++++------ php8.6/mb_str_pad.php | 15 ++++-- php8.6/new_known_constructor.php | 13 +++-- php8.6/pathinfo.php | 23 +++++---- php8.6/preg_replace_callback.php | 25 +++++---- php8.6/printf.php | 21 +++++--- php8.6/sort_single_element.php | 27 ++++++---- php8.6/str_pad.php | 17 ++++--- php8.6/str_repeat_one.php | 17 ++++--- php8.6/str_split.php | 19 ++++--- php8.6/stream_copy_to_socket.php | 67 +++++++++++++------------ php8.6/uri_rfc3986_getters.php | 27 ++++++---- php8.6/vm_stack_recursion.php | 39 +++++++------- php8.6/zip_add_from_string.php | 33 ++++++------ 32 files changed, 472 insertions(+), 312 deletions(-) diff --git a/php8.6/addcslashes.php b/php8.6/addcslashes.php index 8f7779a..ed849bf 100644 --- a/php8.6/addcslashes.php +++ b/php8.6/addcslashes.php @@ -2,15 +2,20 @@ // addcslashes() generates octal escapes for non-printable characters without // going through a formatted print. https://github.com/php/php-src/pull/23680 -$binary = ''; -for ($i = 0; $i < 2_000; $i++) { - $binary .= chr($i % 256); -} +function main(): void +{ + $binary = ''; + for ($i = 0; $i < 2_000; $i++) { + $binary .= chr($i % 256); + } + + $length = 0; + $i = 20_000; + while ($i--) { + $length += strlen(addcslashes($binary, "\0..\37!@\177..\377")); + } -$length = 0; -$i = 20_000; -while ($i--) { - $length += strlen(addcslashes($binary, "\0..\37!@\177..\377")); + echo $length, PHP_EOL; } -echo $length, PHP_EOL; +main(); diff --git a/php8.6/array_column.php b/php8.6/array_column.php index b8d9040..11227bc 100644 --- a/php8.6/array_column.php +++ b/php8.6/array_column.php @@ -2,16 +2,21 @@ // array_column() moves key normalization out of the main loop. // https://github.com/php/php-src/pull/23786 -$rows = []; -for ($i = 0; $i < 1_000; $i++) { - $rows[] = ['id' => $i, 'name' => "name $i", 'email' => "user$i@example.com"]; -} +function main(): void +{ + $rows = []; + for ($i = 0; $i < 1_000; $i++) { + $rows[] = ['id' => $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')); + } -$count = 0; -$i = 20_000; -while ($i--) { - $count += count(array_column($rows, 'name')); - $count += count(array_column($rows, 'email', 'id')); + echo $count, PHP_EOL; } -echo $count, PHP_EOL; +main(); diff --git a/php8.6/array_fill_keys.php b/php8.6/array_fill_keys.php index 61ac113..6bc8728 100644 --- a/php8.6/array_fill_keys.php +++ b/php8.6/array_fill_keys.php @@ -1,14 +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)); +function main(): void +{ + $intKeys = range(1, 1_000); + $stringKeys = array_map(static fn (int $v): string => "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; } -echo $count, PHP_EOL; +main(); diff --git a/php8.6/array_intersect.php b/php8.6/array_intersect.php index f289ae3..eb1f8f2 100644 --- a/php8.6/array_intersect.php +++ b/php8.6/array_intersect.php @@ -2,16 +2,21 @@ // array_intersect() uses hash-based matching instead of sorting all arrays. // https://github.com/php/php-src/pull/23019 -$a = range(0, 9_999); -$b = range(5_000, 14_999); -$strA = array_map(static fn (int $v): string => "value_$v", $a); -$strB = array_map(static fn (int $v): string => "value_$v", $b); +function main(): void +{ + $a = range(0, 9_999); + $b = range(5_000, 14_999); + $strA = array_map(static fn (int $v): string => "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)); + $count = 0; + $i = 100; + while ($i--) { + $count += count(array_intersect($a, $b)); + $count += count(array_intersect($strA, $strB)); + } + + echo $count, PHP_EOL; } -echo $count, PHP_EOL; +main(); diff --git a/php8.6/array_map_fcc.php b/php8.6/array_map_fcc.php index 797cc97..d3f95af 100644 --- a/php8.6/array_map_fcc.php +++ b/php8.6/array_map_fcc.php @@ -9,13 +9,18 @@ function increment(int $x): int return $x + 1; } -$array = range(1, 1_000); -$sum = 0; +function main(): void +{ + $array = range(1, 1_000); + $sum = 0; + + $i = 20_000; + while ($i--) { + $result = array_map(increment(...), $array); + $sum += $result[999]; + } -$i = 20_000; -while ($i--) { - $result = array_map(increment(...), $array); - $sum += $result[999]; + echo $sum, PHP_EOL; } -echo $sum, PHP_EOL; +main(); diff --git a/php8.6/array_map_fcc_strict.php b/php8.6/array_map_fcc_strict.php index 6fcc2ef..ce46ad6 100644 --- a/php8.6/array_map_fcc_strict.php +++ b/php8.6/array_map_fcc_strict.php @@ -10,13 +10,18 @@ function increment(int $x): int return $x + 1; } -$array = range(1, 1_000); -$sum = 0; +function main(): void +{ + $array = range(1, 1_000); + $sum = 0; + + $i = 20_000; + while ($i--) { + $result = array_map(increment(...), $array); + $sum += $result[999]; + } -$i = 20_000; -while ($i--) { - $result = array_map(increment(...), $array); - $sum += $result[999]; + echo $sum, PHP_EOL; } -echo $sum, PHP_EOL; +main(); diff --git a/php8.6/array_map_multiple.php b/php8.6/array_map_multiple.php index 56760dc..e7f77c7 100644 --- a/php8.6/array_map_multiple.php +++ b/php8.6/array_map_multiple.php @@ -2,15 +2,20 @@ // array_map() with multiple arrays avoids per-iteration allocations and // refcounted copies. https://github.com/php/php-src/commit/79adb01bf -$a = range(1, 1_000); -$b = range(1_001, 2_000); -$callback = static fn (int $x, int $y): int => $x + $y; +function main(): void +{ + $a = range(1, 1_000); + $b = range(1_001, 2_000); + $callback = static fn (int $x, int $y): int => $x + $y; -$sum = 0; -$i = 20_000; -while ($i--) { - $result = array_map($callback, $a, $b); - $sum += $result[999]; + $sum = 0; + $i = 20_000; + while ($i--) { + $result = array_map($callback, $a, $b); + $sum += $result[999]; + } + + echo $sum, PHP_EOL; } -echo $sum, PHP_EOL; +main(); diff --git a/php8.6/array_reduce.php b/php8.6/array_reduce.php index efdb8d2..674c09f 100644 --- a/php8.6/array_reduce.php +++ b/php8.6/array_reduce.php @@ -3,15 +3,20 @@ // callback invocation, turning the `$carry[...] = ...; return $carry;` pattern // from O(n²) into O(n). https://github.com/php/php-src/pull/21340 -$data = []; -for ($i = 0; $i < 20_000; $i++) { - $data[] = substr(hash('crc32b', (string) $i), 0, 4); -} +function main(): void +{ + $data = []; + for ($i = 0; $i < 20_000; $i++) { + $data[] = substr(hash('crc32b', (string) $i), 0, 4); + } + + $result = array_reduce($data, static function (array $carry, string $item): array { + $carry[$item] = ($carry[$item] ?? 0) + 1; -$result = array_reduce($data, static function (array $carry, string $item): array { - $carry[$item] = ($carry[$item] ?? 0) + 1; + return $carry; + }, []); - return $carry; -}, []); + echo count($result), PHP_EOL; +} -echo count($result), PHP_EOL; +main(); diff --git a/php8.6/array_sum_product.php b/php8.6/array_sum_product.php index 89e1a4b..3ebb5b4 100644 --- a/php8.6/array_sum_product.php +++ b/php8.6/array_sum_product.php @@ -2,17 +2,22 @@ // array_sum() and array_product() have a specialized path for integer-only // arrays. https://github.com/php/php-src/pull/21787 -$ints = range(1, 10_000); -$signs = []; -for ($i = 0; $i < 10_000; $i++) { - $signs[] = $i % 3 === 0 ? -1 : 1; -} +function main(): void +{ + $ints = range(1, 10_000); + $signs = []; + for ($i = 0; $i < 10_000; $i++) { + $signs[] = $i % 3 === 0 ? -1 : 1; + } + + $sum = 0; + $i = 20_000; + while ($i--) { + $sum += array_sum($ints); + $sum += array_product($signs); + } -$sum = 0; -$i = 20_000; -while ($i--) { - $sum += array_sum($ints); - $sum += array_product($signs); + echo $sum, PHP_EOL; } -echo $sum, PHP_EOL; +main(); diff --git a/php8.6/array_unshift.php b/php8.6/array_unshift.php index 0a6f8c5..3d54bbb 100644 --- a/php8.6/array_unshift.php +++ b/php8.6/array_unshift.php @@ -2,13 +2,18 @@ // array_unshift() has a fast path for packed (list) arrays. // https://github.com/php/php-src/pull/20353 -$list = range(1, 100); +function main(): void +{ + $list = range(1, 100); -$count = 0; -$i = 2_000_000; -while ($i--) { - $copy = $list; - $count += array_unshift($copy, 0, -1); + $count = 0; + $i = 2_000_000; + while ($i--) { + $copy = $list; + $count += array_unshift($copy, 0, -1); + } + + echo $count, PHP_EOL; } -echo $count, PHP_EOL; +main(); diff --git a/php8.6/array_walk.php b/php8.6/array_walk.php index 29c6a2a..c0aea77 100644 --- a/php8.6/array_walk.php +++ b/php8.6/array_walk.php @@ -2,14 +2,19 @@ // array_walk() invokes the callback with less overhead per element. // https://github.com/php/php-src/pull/20322 -$array = range(1, 1_000); -$callback = static function (int &$value, int $key): void { - $value += $key; -}; - -$i = 20_000; -while ($i--) { - array_walk($array, $callback); +function main(): void +{ + $array = range(1, 1_000); + $callback = static function (int &$value, int $key): void { + $value += $key; + }; + + $i = 20_000; + while ($i--) { + array_walk($array, $callback); + } + + echo $array[999], PHP_EOL; } -echo $array[999], PHP_EOL; +main(); diff --git a/php8.6/asymmetric_visibility.php b/php8.6/asymmetric_visibility.php index 1ee2a81..c80a294 100644 --- a/php8.6/asymmetric_visibility.php +++ b/php8.6/asymmetric_visibility.php @@ -30,9 +30,14 @@ public function fill(int $n): void } } -$private = new PrivateSet(); -$protected = new ProtectedSetChild(); -$private->fill(20_000_000); -$protected->fill(20_000_000); +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; +} -echo $private->value + $protected->value, PHP_EOL; +main(); diff --git a/php8.6/closure_static.php b/php8.6/closure_static.php index bffa3ee..33ba930 100644 --- a/php8.6/closure_static.php +++ b/php8.6/closure_static.php @@ -9,10 +9,15 @@ function apply(callable $callback, int $value): int return $callback($value); } -$sum = 0; -$i = 10_000_000; -while ($i--) { - $sum += apply(static fn (int $x): int => $x + 1, $i); +function main(): void +{ + $sum = 0; + $i = 10_000_000; + while ($i--) { + $sum += apply(static fn (int $x): int => $x + 1, $i); + } + + echo $sum, PHP_EOL; } -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 index 5991c15..226167d 100644 --- a/php8.6/collator_sort_with_sort_keys.php +++ b/php8.6/collator_sort_with_sort_keys.php @@ -4,15 +4,20 @@ // instead of allocating 2 x 1 MiB on every call. // https://github.com/php/php-src/pull/23504 -$collator = new Collator('de_DE'); -$words = ['Äpfel', 'Birnen', 'apfel', 'Zitrone', 'Öl', 'Ober', 'Banane', 'über']; +function main(): void +{ + $collator = new Collator('de_DE'); + $words = ['Äpfel', 'Birnen', 'apfel', 'Zitrone', 'Öl', 'Ober', 'Banane', 'über']; -$count = 0; -$i = 100_000; -while ($i--) { - $copy = $words; - $collator->sortWithSortKeys($copy); - $count += count($copy); + $count = 0; + $i = 100_000; + while ($i--) { + $copy = $words; + $collator->sortWithSortKeys($copy); + $count += count($copy); + } + + echo $count, ' ', implode(',', $copy), PHP_EOL; } -echo $count, ' ', implode(',', $copy), PHP_EOL; +main(); diff --git a/php8.6/dom_split_text.php b/php8.6/dom_split_text.php index 3de0223..ede48af 100644 --- a/php8.6/dom_split_text.php +++ b/php8.6/dom_split_text.php @@ -3,18 +3,23 @@ // DOMText::splitText() is faster and uses less memory. // https://github.com/php/php-src/pull/20371 -$document = new DOMDocument(); -$root = $document->appendChild($document->createElement('root')); +function main(): void +{ + $document = new DOMDocument(); + $root = $document->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 = 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; } - $length += $root->childNodes->length; + + echo $length, PHP_EOL; } -echo $length, PHP_EOL; +main(); diff --git a/php8.6/first_class_callable.php b/php8.6/first_class_callable.php index 118457c..af3bf6d 100644 --- a/php8.6/first_class_callable.php +++ b/php8.6/first_class_callable.php @@ -12,9 +12,14 @@ function encodeToString(callable $writerFunc): void $writerFunc(); } -$i = 20_000_000; -while ($i--) { - encodeToString(dummy(...)); +function main(): void +{ + $i = 20_000_000; + while ($i--) { + encodeToString(dummy(...)); + } + + echo 'done', PHP_EOL; } -echo 'done', PHP_EOL; +main(); diff --git a/php8.6/intval_binary.php b/php8.6/intval_binary.php index 6162d9f..0823cae 100644 --- a/php8.6/intval_binary.php +++ b/php8.6/intval_binary.php @@ -2,14 +2,19 @@ // intval('0b...', 2) and intval('+0b...', 2) no longer copy the string to strip // the prefix. https://github.com/php/php-src/commit/0af87e970 -$values = ['0b101010', '+0b11110000', '0B1', '0b1111111111111111111111111111111']; +function main(): void +{ + $values = ['0b101010', '+0b11110000', '0B1', '0b1111111111111111111111111111111']; -$sum = 0; -$i = 5_000_000; -while ($i--) { - foreach ($values as $value) { - $sum += intval($value, 2); + $sum = 0; + $i = 5_000_000; + while ($i--) { + foreach ($values as $value) { + $sum += intval($value, 2); + } } + + echo $sum, PHP_EOL; } -echo $sum, PHP_EOL; +main(); diff --git a/php8.6/json_decode.php b/php8.6/json_decode.php index b2fb748..e285f4c 100644 --- a/php8.6/json_decode.php +++ b/php8.6/json_decode.php @@ -3,25 +3,30 @@ // moved off the success path again, this checks for a remaining regression. // https://github.com/php/php-src/pull/20629 https://github.com/php/php-src/pull/22487 -$rows = []; -for ($i = 0; $i < 30; $i++) { - $rows[] = [ - 'id' => $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]); +function main(): void +{ + $rows = []; + for ($i = 0; $i < 30; $i++) { + $rows[] = [ + 'id' => $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']); + } -$count = 0; -$i = 50_000; -while ($i--) { - $count += count(json_decode($json, true)['rows']); + echo $count, PHP_EOL; } -echo $count, PHP_EOL; +main(); diff --git a/php8.6/json_encode.php b/php8.6/json_encode.php index fd871b0..345ceec 100644 --- a/php8.6/json_encode.php +++ b/php8.6/json_encode.php @@ -2,22 +2,27 @@ // json_encode() is faster for arrays and objects, and JSON_PRETTY_PRINT // generates indentation faster. https://github.com/php/php-src/pull/21474 -$data = []; -for ($i = 0; $i < 200; $i++) { - $data[] = [ - 'id' => $i, - 'name' => "item $i", - 'tags' => ['alpha', 'beta', 'gamma'], - 'meta' => ['nested' => ['deep' => ['deeper' => ['value' => $i, 'flag' => true]]]], - 'object' => (object) ['a' => 1, 'b' => [1, 2, 3]], - ]; -} +function main(): void +{ + $data = []; + for ($i = 0; $i < 200; $i++) { + $data[] = [ + 'id' => $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)); + } -$length = 0; -$i = 5_000; -while ($i--) { - $length += strlen(json_encode($data)); - $length += strlen(json_encode($data, JSON_PRETTY_PRINT)); + echo $length, PHP_EOL; } -echo $length, PHP_EOL; +main(); diff --git a/php8.6/mb_str_pad.php b/php8.6/mb_str_pad.php index 7a3819b..a50f11c 100644 --- a/php8.6/mb_str_pad.php +++ b/php8.6/mb_str_pad.php @@ -3,10 +3,15 @@ // mb_str_pad() pads by doubling copies instead of appending the pad string in // a loop. https://github.com/php/php-src/pull/23667 -$length = 0; -$i = 300_000; -while ($i--) { - $length += strlen(mb_str_pad('Grüße', 2_000, '–·', STR_PAD_BOTH, 'UTF-8')); +function main(): void +{ + $length = 0; + $i = 300_000; + while ($i--) { + $length += strlen(mb_str_pad('Grüße', 2_000, '–·', STR_PAD_BOTH, 'UTF-8')); + } + + echo $length, PHP_EOL; } -echo $length, PHP_EOL; +main(); diff --git a/php8.6/new_known_constructor.php b/php8.6/new_known_constructor.php index d0b46ed..65dc0b9 100644 --- a/php8.6/new_known_constructor.php +++ b/php8.6/new_known_constructor.php @@ -19,9 +19,14 @@ public static function new(int $bar): self } } -$sum = 0; -for ($i = 0; $i < 3_000_000; $i++) { - $sum += MyClass::new($i)->bar; +function main(): void +{ + $sum = 0; + for ($i = 0; $i < 3_000_000; $i++) { + $sum += MyClass::new($i)->bar; + } + + echo $sum, PHP_EOL; } -echo $sum, PHP_EOL; +main(); diff --git a/php8.6/pathinfo.php b/php8.6/pathinfo.php index 8963a5d..d5443ef 100644 --- a/php8.6/pathinfo.php +++ b/php8.6/pathinfo.php @@ -2,14 +2,19 @@ // pathinfo() with a single PATHINFO_* flag reuses the basename and skips work // for components that were not requested. https://github.com/php/php-src/pull/23726 -$path = '/var/www/project/vendor/package/src/Component/SomeClass.php'; - -$length = 0; -$i = 5_000_000; -while ($i--) { - $length += strlen(pathinfo($path, PATHINFO_EXTENSION)); - $length += strlen(pathinfo($path, PATHINFO_FILENAME)); - $length += strlen(pathinfo($path, PATHINFO_BASENAME)); +function main(): void +{ + $path = '/var/www/project/vendor/package/src/Component/SomeClass.php'; + + $length = 0; + $i = 5_000_000; + while ($i--) { + $length += strlen(pathinfo($path, PATHINFO_EXTENSION)); + $length += strlen(pathinfo($path, PATHINFO_FILENAME)); + $length += strlen(pathinfo($path, PATHINFO_BASENAME)); + } + + echo $length, PHP_EOL; } -echo $length, PHP_EOL; +main(); diff --git a/php8.6/preg_replace_callback.php b/php8.6/preg_replace_callback.php index a2fe125..1cac796 100644 --- a/php8.6/preg_replace_callback.php +++ b/php8.6/preg_replace_callback.php @@ -3,17 +3,22 @@ // holding an extra reference, so modifying $matches no longer copies it. // https://github.com/php/php-src/pull/21340 -$text = str_repeat('lorem ipsum dolor sit amet consectetur adipiscing elit ', 2_000); -$length = 0; +function main(): void +{ + $text = str_repeat('lorem ipsum dolor sit amet consectetur adipiscing elit ', 2_000); + $length = 0; -$i = 200; -while ($i--) { - $result = preg_replace_callback('/(\w)(\w*)/', static function (array $m): string { - $m[1] = strtoupper($m[1]); + $i = 200; + while ($i--) { + $result = preg_replace_callback('/(\w)(\w*)/', static function (array $m): string { + $m[1] = strtoupper($m[1]); - return $m[1] . $m[2]; - }, $text); - $length += strlen($result); + return $m[1] . $m[2]; + }, $text); + $length += strlen($result); + } + + echo $length, PHP_EOL; } -echo $length, PHP_EOL; +main(); diff --git a/php8.6/printf.php b/php8.6/printf.php index 6dd757e..32761bb 100644 --- a/php8.6/printf.php +++ b/php8.6/printf.php @@ -2,14 +2,19 @@ // printf() with only %s and %d is compiled into string interpolation, like // sprintf() since PHP 8.4. https://github.com/php/php-src/pull/19658 -$a = 'foo'; -$b = 42; +function main(): void +{ + $a = 'foo'; + $b = 42; -ob_start(); -for ($i = 0; $i < 20_000_000; $i++) { - printf('%s-%d', $a, $b); - ob_clean(); + ob_start(); + for ($i = 0; $i < 20_000_000; $i++) { + printf('%s-%d', $a, $b); + ob_clean(); + } + ob_end_clean(); + + echo 'done', PHP_EOL; } -ob_end_clean(); -echo 'done', PHP_EOL; +main(); diff --git a/php8.6/sort_single_element.php b/php8.6/sort_single_element.php index a806368..cdce29b 100644 --- a/php8.6/sort_single_element.php +++ b/php8.6/sort_single_element.php @@ -2,17 +2,22 @@ // Sorting single-element arrays skips unpacking and the comparator entirely. // https://github.com/php/php-src/pull/23550 -$compare = static fn (int $a, int $b): int => $a <=> $b; +function main(): void +{ + $compare = static fn (int $a, int $b): int => $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']; + $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; } -echo $sum, PHP_EOL; +main(); diff --git a/php8.6/str_pad.php b/php8.6/str_pad.php index e231308..a31446f 100644 --- a/php8.6/str_pad.php +++ b/php8.6/str_pad.php @@ -3,11 +3,16 @@ // doubling copies for multi-character pad strings. // https://github.com/php/php-src/pull/19272 https://github.com/php/php-src/pull/23661 -$length = 0; -$i = 500_000; -while ($i--) { - $length += strlen(str_pad('hello', 2_000, ' ', STR_PAD_RIGHT)); - $length += strlen(str_pad('hello', 2_000, '-=', STR_PAD_LEFT)); +function main(): void +{ + $length = 0; + $i = 500_000; + while ($i--) { + $length += strlen(str_pad('hello', 2_000, ' ', STR_PAD_RIGHT)); + $length += strlen(str_pad('hello', 2_000, '-=', STR_PAD_LEFT)); + } + + echo $length, PHP_EOL; } -echo $length, PHP_EOL; +main(); diff --git a/php8.6/str_repeat_one.php b/php8.6/str_repeat_one.php index fbc4967..4619708 100644 --- a/php8.6/str_repeat_one.php +++ b/php8.6/str_repeat_one.php @@ -2,12 +2,17 @@ // str_repeat($string, 1) returns the input string without copying it. // https://github.com/php/php-src/pull/23681 -$string = str_repeat('x', 100_000); +function main(): void +{ + $string = str_repeat('x', 100_000); -$length = 0; -$i = 1_000_000; -while ($i--) { - $length += strlen(str_repeat($string, 1)); + $length = 0; + $i = 1_000_000; + while ($i--) { + $length += strlen(str_repeat($string, 1)); + } + + echo $length, PHP_EOL; } -echo $length, PHP_EOL; +main(); diff --git a/php8.6/str_split.php b/php8.6/str_split.php index 89b0f87..ad0977b 100644 --- a/php8.6/str_split.php +++ b/php8.6/str_split.php @@ -2,13 +2,18 @@ // str_split() was optimized, especially for a split length of 1. // https://github.com/php/php-src/pull/20419 -$string = str_repeat('abcdefghijklmnopqrstuvwxyz', 40); +function main(): void +{ + $string = str_repeat('abcdefghijklmnopqrstuvwxyz', 40); -$count = 0; -$i = 100_000; -while ($i--) { - $count += count(str_split($string)); - $count += count(str_split($string, 4)); + $count = 0; + $i = 100_000; + while ($i--) { + $count += count(str_split($string)); + $count += count(str_split($string, 4)); + } + + echo $count, PHP_EOL; } -echo $count, PHP_EOL; +main(); diff --git a/php8.6/stream_copy_to_socket.php b/php8.6/stream_copy_to_socket.php index 1f73b66..7e84330 100644 --- a/php8.6/stream_copy_to_socket.php +++ b/php8.6/stream_copy_to_socket.php @@ -3,41 +3,46 @@ // uses copy_file_range(), instead of a userspace read/write loop. // https://github.com/php/php-src/pull/20399 -$size = 64 * 1024 * 1024; -$file = tempnam(sys_get_temp_dir(), 'bench'); -$fp = fopen($file, 'w'); -$chunk = random_bytes(1024 * 1024); -for ($i = 0; $i < $size / strlen($chunk); $i++) { - fwrite($fp, $chunk); -} -fclose($fp); +function main(): void +{ + $size = 64 * 1024 * 1024; + $file = tempnam(sys_get_temp_dir(), 'bench'); + $fp = fopen($file, 'w'); + $chunk = random_bytes(1024 * 1024); + for ($i = 0; $i < $size / strlen($chunk); $i++) { + fwrite($fp, $chunk); + } + fclose($fp); -[$writer, $reader] = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP); -$rounds = 20; + [$writer, $reader] = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP); + $rounds = 20; -$pid = pcntl_fork(); -if ($pid === 0) { - fclose($writer); - $received = 0; - while (!feof($reader)) { - $received += strlen(fread($reader, 1024 * 1024)); + $pid = pcntl_fork(); + if ($pid === 0) { + fclose($writer); + $received = 0; + while (!feof($reader)) { + $received += strlen(fread($reader, 1024 * 1024)); + } + exit($received === $size * $rounds ? 0 : 1); } - exit($received === $size * $rounds ? 0 : 1); -} -fclose($reader); + fclose($reader); -$copied = 0; -for ($i = 0; $i < $rounds; $i++) { - $in = fopen($file, 'r'); - $copied += stream_copy_to_stream($in, $writer); - fclose($in); -} -fclose($writer); -pcntl_waitpid($pid, $status); -unlink($file); + $copied = 0; + for ($i = 0; $i < $rounds; $i++) { + $in = fopen($file, 'r'); + $copied += stream_copy_to_stream($in, $writer); + fclose($in); + } + fclose($writer); + pcntl_waitpid($pid, $status); + unlink($file); + + if (pcntl_wexitstatus($status) !== 0) { + throw new RuntimeException('Reader did not receive all bytes'); + } -if (pcntl_wexitstatus($status) !== 0) { - throw new RuntimeException('Reader did not receive all bytes'); + echo $copied, PHP_EOL; } -echo $copied, PHP_EOL; +main(); diff --git a/php8.6/uri_rfc3986_getters.php b/php8.6/uri_rfc3986_getters.php index c32f840..aa25f51 100644 --- a/php8.6/uri_rfc3986_getters.php +++ b/php8.6/uri_rfc3986_getters.php @@ -2,17 +2,22 @@ // Normalizing getters on already-normalized Uri\Rfc3986\Uri instances no longer // copy and re-normalize the URI. https://github.com/php/php-src/pull/21726 -$uri = Uri\Rfc3986\Uri::parse('https://user@example.com:8080/path/to/resource?query=value&x=1#fragment'); +function main(): void +{ + $uri = Uri\Rfc3986\Uri::parse('https://user@example.com:8080/path/to/resource?query=value&x=1#fragment'); -$length = 0; -$i = 2_000_000; -while ($i--) { - $length += strlen($uri->getScheme()) - + strlen($uri->getHost()) - + strlen($uri->getPath()) - + strlen($uri->getQuery()) - + strlen($uri->getFragment()) - + strlen($uri->toString()); + $length = 0; + $i = 2_000_000; + while ($i--) { + $length += strlen($uri->getScheme()) + + strlen($uri->getHost()) + + strlen($uri->getPath()) + + strlen($uri->getQuery()) + + strlen($uri->getFragment()) + + strlen($uri->toString()); + } + + echo $length, PHP_EOL; } -echo $length, PHP_EOL; +main(); diff --git a/php8.6/vm_stack_recursion.php b/php8.6/vm_stack_recursion.php index 395f8e5..eabc694 100644 --- a/php8.6/vm_stack_recursion.php +++ b/php8.6/vm_stack_recursion.php @@ -4,28 +4,33 @@ // full zend_mm_alloc_pages() search on every oscillation. // https://github.com/php/php-src/pull/23470 -ini_set('memory_limit', '-1'); -gc_disable(); - function recurse(int $depth): int { return $depth === 0 ? 0 : recurse($depth - 1) + 1; } -// Fragment the heap: allocate ~5.5KB blocks and free every other one, leaving -// only 2-page holes in every chunk, like a long-running process would. -$blocks = []; -for ($i = 0; $i < 60_000; $i++) { - $blocks[] = str_repeat(chr(65 + $i % 26), 5_500); -} -for ($i = 0; $i < 60_000; $i += 2) { - unset($blocks[$i]); -} +function main(): void +{ + ini_set('memory_limit', '-1'); + gc_disable(); + + // Fragment the heap: allocate ~5.5KB blocks and free every other one, leaving + // only 2-page holes in every chunk, like a long-running process would. + $blocks = []; + for ($i = 0; $i < 60_000; $i++) { + $blocks[] = str_repeat(chr(65 + $i % 26), 5_500); + } + for ($i = 0; $i < 60_000; $i += 2) { + unset($blocks[$i]); + } + + $sum = 0; + $i = 4_000; + while ($i--) { + $sum += recurse(6_000); + } -$sum = 0; -$i = 4_000; -while ($i--) { - $sum += recurse(6_000); + echo $sum, ' ', count($blocks), PHP_EOL; } -echo $sum, ' ', count($blocks), PHP_EOL; +main(); diff --git a/php8.6/zip_add_from_string.php b/php8.6/zip_add_from_string.php index 1cfa56f..fc01eab 100644 --- a/php8.6/zip_add_from_string.php +++ b/php8.6/zip_add_from_string.php @@ -3,21 +3,26 @@ // ZipArchive::addFromString() no longer copies the string contents. // https://github.com/php/php-src/pull/20497 -$file = tempnam(sys_get_temp_dir(), 'bench'); -$content = str_repeat('0123456789abcdef', 16 * 1024); +function main(): void +{ + $file = tempnam(sys_get_temp_dir(), 'bench'); + $content = str_repeat('0123456789abcdef', 16 * 1024); -$count = 0; -$i = 50; -while ($i--) { - $zip = new ZipArchive(); - $zip->open($file, ZipArchive::CREATE | ZipArchive::OVERWRITE); - for ($j = 0; $j < 200; $j++) { - $zip->addFromString("file-$j.txt", $content); + $count = 0; + $i = 50; + while ($i--) { + $zip = new ZipArchive(); + $zip->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(); } - $count += $zip->numFiles; - $zip->unchangeAll(); - $zip->close(); + @unlink($file); + + echo $count, PHP_EOL; } -@unlink($file); -echo $count, PHP_EOL; +main();