Skip to content

Optimize SplFixedArray::fromArray() for packed arrays #18196

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

Conversation

nielsdos
Copy link
Member

If the array is packed, then we don't have to loop to get the highest index.

For this script:

$array = range(1, 100);
for ($i=0;$i<1000000;$i++) {
    SplFixedArray::fromArray($array);
}

On an i7-4790:

Benchmark 1: ./sapi/cli/php spl.php
  Time (mean ± σ):     376.5 ms ±   2.0 ms    [User: 372.1 ms, System: 2.6 ms]
  Range (min … max):   373.7 ms … 379.5 ms    10 runs

Benchmark 2: ./sapi/cli/php_old spl.php
  Time (mean ± σ):     511.6 ms ±   1.9 ms    [User: 508.0 ms, System: 2.3 ms]
  Range (min … max):   509.2 ms … 515.1 ms    10 runs

Summary
  ./sapi/cli/php spl.php  ran
    1.36 ± 0.01 times faster than ./sapi/cli/php_old spl.php

On an i7-1185G7:

Benchmark 1: ./sapi/cli/php spl.php
  Time (mean ± σ):     250.4 ms ±   3.5 ms    [User: 246.6 ms, System: 2.6 ms]
  Range (min … max):   247.0 ms … 258.5 ms    11 runs

Benchmark 2: ./sapi/cli/php_old spl.php
  Time (mean ± σ):     328.4 ms ±   1.0 ms    [User: 324.4 ms, System: 3.8 ms]
  Range (min … max):   327.5 ms … 331.0 ms    10 runs

Summary
  ./sapi/cli/php spl.php  ran
    1.31 ± 0.02 times faster than ./sapi/cli/php_old spl.php

Bonus: this also decreases the code size of the function.

If the array is packed, then we don't have to loop to get the highest
index.

For this script:
```php
$array = range(1, 100);
for ($i=0;$i<1000000;$i++) {
    SplFixedArray::fromArray($array);
}
```

On an i7-4790:
```
Benchmark 1: ./sapi/cli/php spl.php
  Time (mean ± σ):     376.5 ms ±   2.0 ms    [User: 372.1 ms, System: 2.6 ms]
  Range (min … max):   373.7 ms … 379.5 ms    10 runs

Benchmark 2: ./sapi/cli/php_old spl.php
  Time (mean ± σ):     511.6 ms ±   1.9 ms    [User: 508.0 ms, System: 2.3 ms]
  Range (min … max):   509.2 ms … 515.1 ms    10 runs

Summary
  ./sapi/cli/php spl.php  ran
    1.36 ± 0.01 times faster than ./sapi/cli/php_old spl.php
```

On an i7-1185G7:
```
Benchmark 1: ./sapi/cli/php spl.php
  Time (mean ± σ):     250.4 ms ±   3.5 ms    [User: 246.6 ms, System: 2.6 ms]
  Range (min … max):   247.0 ms … 258.5 ms    11 runs

Benchmark 2: ./sapi/cli/php_old spl.php
  Time (mean ± σ):     328.4 ms ±   1.0 ms    [User: 324.4 ms, System: 3.8 ms]
  Range (min … max):   327.5 ms … 331.0 ms    10 runs

Summary
  ./sapi/cli/php spl.php  ran
    1.31 ± 0.02 times faster than ./sapi/cli/php_old spl.php
```

Bonus: this also decreases the code size of the function.
@nielsdos nielsdos merged commit e034b69 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