Skip to content

Optimize SplFixedArray::toArray() #18190

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

Merged
merged 1 commit into from
Mar 30, 2025
Merged

Optimize SplFixedArray::toArray() #18190

merged 1 commit into from
Mar 30, 2025

Conversation

nielsdos
Copy link
Member

We can use the optimized packed filling code instead of going through all the logic of the zend_hash update APIs.

For this script:

$test = new SplFixedArray(4);
$test[0] = 0;
$test[1] = 1;
$test[2] = 2;
$test[3] = 3;

for ($i = 0 ; $i< 5000000; $i++)
	$test->toArray();

On an i7-4790:

Benchmark 1: ./sapi/cli/php toarray.php
  Time (mean ± σ):     170.0 ms ±   1.8 ms    [User: 167.3 ms, System: 2.2 ms]
  Range (min … max):   166.9 ms … 173.0 ms    17 runs

Benchmark 2: ./sapi/cli/php_old toarray.php
  Time (mean ± σ):     215.7 ms ±   3.6 ms    [User: 211.9 ms, System: 3.0 ms]
  Range (min … max):   211.3 ms … 222.0 ms    13 runs

Summary
  ./sapi/cli/php toarray.php ran
    1.27 ± 0.02 times faster than ./sapi/cli/php_old toarray.php

On an i7-1185G7:


Benchmark 1: ./sapi/cli/php toarray.php
  Time (mean ± σ):     112.6 ms ±   1.4 ms    [User: 109.6 ms, System: 2.9 ms]
  Range (min … max):   111.1 ms … 116.4 ms    25 runs

Benchmark 2: ./sapi/cli/php_old toarray.php
  Time (mean ± σ):     145.3 ms ±   2.8 ms    [User: 141.8 ms, System: 3.4 ms]
  Range (min … max):   142.6 ms … 151.8 ms    20 runs

Summary
  ./sapi/cli/php toarray.php  ran
    1.29 ± 0.03 times faster than ./sapi/cli/php_old toarray.php

We can use the optimized packed filling code instead of going through
all the logic of the zend_hash update APIs.

For this script:
```php
$test = new SplFixedArray(4);
$test[0] = 0;
$test[1] = 1;
$test[2] = 2;
$test[3] = 3;

for ($i = 0 ; $i< 5000000; $i++)
	$test->toArray();
```

On an i7-4790:
```
Benchmark 1: ./sapi/cli/php toarray.php
  Time (mean ± σ):     170.0 ms ±   1.8 ms    [User: 167.3 ms, System: 2.2 ms]
  Range (min … max):   166.9 ms … 173.0 ms    17 runs

Benchmark 2: ./sapi/cli/php_old toarray.php
  Time (mean ± σ):     215.7 ms ±   3.6 ms    [User: 211.9 ms, System: 3.0 ms]
  Range (min … max):   211.3 ms … 222.0 ms    13 runs

Summary
  ./sapi/cli/php toarray.php ran
    1.27 ± 0.02 times faster than ./sapi/cli/php_old toarray.php
```

On an i7-1185G7:
```

Benchmark 1: ./sapi/cli/php toarray.php
  Time (mean ± σ):     112.6 ms ±   1.4 ms    [User: 109.6 ms, System: 2.9 ms]
  Range (min … max):   111.1 ms … 116.4 ms    25 runs

Benchmark 2: ./sapi/cli/php_old toarray.php
  Time (mean ± σ):     145.3 ms ±   2.8 ms    [User: 141.8 ms, System: 3.4 ms]
  Range (min … max):   142.6 ms … 151.8 ms    20 runs

Summary
  ./sapi/cli/php toarray.php  ran
    1.29 ± 0.03 times faster than ./sapi/cli/php_old toarray.php
```
@nielsdos nielsdos merged commit d13d9b3 into php:master Mar 30, 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.

2 participants