Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid pointless refcounting in array_reduce #18180

Merged
merged 1 commit into from
Mar 29, 2025

Conversation

nielsdos
Copy link
Member

@nielsdos nielsdos commented Mar 29, 2025

For this script:

for ($i=0;$i < 100; $i++)
    array_reduce(range(1, 100000), fn ($a,$b)=>$a+$b,1);

On an i7-4790:

Benchmark 1: ./sapi/cli/php reduce_bench.php
  Time (mean ± σ):     272.0 ms ±   3.7 ms    [User: 268.9 ms, System: 2.1 ms]
  Range (min … max):   268.9 ms … 281.3 ms    11 runs

Benchmark 2: ./sapi/cli/php_old reduce_bench.php
  Time (mean ± σ):     288.2 ms ±   3.5 ms    [User: 284.5 ms, System: 2.7 ms]
  Range (min … max):   285.0 ms … 295.9 ms    10 runs

Summary
  ./sapi/cli/php reduce_bench.php  ran
    1.06 ± 0.02 times faster than ./sapi/cli/php_old reduce_bench.php

On an i7-1185G7:

Benchmark 1: ./sapi/cli/php test.php
  Time (mean ± σ):     189.6 ms ± 3.5 ms [User: 178.5 ms, System: 10.7 ms]
  Range (min … max):   187.3 ms … 201.6 ms 15 runs

Benchmark 2: ./sapi/cli/php_old test.php
  Time (mean ± σ):     204.2 ms ± 2.9 ms [User: 190.1 ms, System: 13.6 ms]
  Range (min … max):   200.6 ms … 210.2 ms 14 runs

Summary
  ./sapi/cli/php test.php ran
    1.08 ± 0.02 times faster than ./sapi/cli/php_old test.php

For this script:
```php
for ($i=0;$i < 100; $i++)
    array_reduce(range(1, 100000), fn ($a,$b)=>$a+$b,1);
```

On an i7-4790:
```
Benchmark 1: ./sapi/cli/php reduce_bench.php
  Time (mean ± σ):     272.0 ms ±   3.7 ms    [User: 268.9 ms, System: 2.1 ms]
  Range (min … max):   268.9 ms … 281.3 ms    11 runs

Benchmark 2: ./sapi/cli/php_old reduce_bench.php
  Time (mean ± σ):     288.2 ms ±   3.5 ms    [User: 284.5 ms, System: 2.7 ms]
  Range (min … max):   285.0 ms … 295.9 ms    10 runs

Summary
  ./sapi/cli/php reduce_bench.php  ran
    1.06 ± 0.02 times faster than ./sapi/cli/php_old reduce_bench.php
```

On an i7-1185G7:
```
Benchmark 1: ./sapi/cli/php test.php
  Time (mean ± σ): 189.6 ms ± 3.5 ms [User: 178.5 ms, System: 10.7 ms]
  Range (min … max): 187.3 ms … 201.6 ms 15 runs

Benchmark 2: ./sapi/cli/php_old test.php
  Time (mean ± σ): 204.2 ms ± 2.9 ms [User: 190.1 ms, System: 13.6 ms]
  Range (min … max): 200.6 ms … 210.2 ms 14 runs

Summary
  ./sapi/cli/php test.php ran
    1.08 ± 0.02 times faster than ./sapi/cli/php_old test.php
```
@nielsdos nielsdos marked this pull request as ready for review March 29, 2025 14:11
@nielsdos nielsdos requested a review from bukka as a code owner March 29, 2025 14:11
@TimWolla
Copy link
Member

TimWolla commented Mar 29, 2025

At this point it would probably be appropriate to mention this in UPGRADING:

14. Performance Improvements

All of the callback-based array functions got performance improvements now, no?

@nielsdos
Copy link
Member Author

All of the callback-based array functions got performance improvements now, no?

Don't know if I missed any important one, but I guess so.

@nielsdos nielsdos merged commit 73c2e3c into php:master Mar 29, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants