Skip to content

Optimize SplFixedArray dimension performance #18184

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

Conversation

nielsdos
Copy link
Member

This patch optimizes reading and writing from SplFixedArray with the dimension operators. It accomplishes this due to the following optimizations:

  • Fast-path for long keys (inlined).
  • Optimization hints (UNEXPECTED + assertion)
  • Using an unsigned index so we can do a single length comparison

For the following script:

$test = new SplFixedArray(4);

for ($i = 0 ; $i< 5000000; $i++)
	$test[1] += $i;

On an i7-4790:

Benchmark 1: ./sapi/cli/php x.php
  Time (mean ± σ):      95.4 ms ±   1.6 ms    [User: 91.5 ms, System: 3.2 ms]
  Range (min … max):    93.7 ms … 100.8 ms    31 runs

Benchmark 2: ./sapi/cli/php_old x.php
  Time (mean ± σ):     119.1 ms ±   1.3 ms    [User: 114.7 ms, System: 3.6 ms]
  Range (min … max):   117.6 ms … 123.1 ms    24 runs

Summary
  ./sapi/cli/php x.php ran
    1.25 ± 0.03 times faster than ./sapi/cli/php_old x.php

On an i7-1185G7:

Benchmark 1: ./sapi/cli/php x.php
  Time (mean ± σ):      67.9 ms ±   1.1 ms    [User: 64.8 ms, System: 3.2 ms]
  Range (min … max):    66.6 ms …  72.8 ms    43 runs

Benchmark 2: ./sapi/cli/php_old x.php
  Time (mean ± σ):      84.8 ms ±   1.1 ms    [User: 81.0 ms, System: 3.9 ms]
  Range (min … max):    82.6 ms …  88.0 ms    34 runs

Summary
  ./sapi/cli/php x.php  ran
    1.25 ± 0.03 times faster than ./sapi/cli/php_old x.php

Copy link
Member

@Girgias Girgias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few questions, but makes sense overall.

This patch optimizes reading and writing from SplFixedArray with the
dimension operators. It accomplishes this due to the following
optimizations:
* Fast-path for long keys (inlined).
* Optimization hints (UNEXPECTED + assertion)
* Using an unsigned index so we can do a single length comparison

For the following script:
```php
$test = new SplFixedArray(4);

for ($i = 0 ; $i< 5000000; $i++)
	$test[1] += $i;
```

On an i7-4790:
```
Benchmark 1: ./sapi/cli/php x.php
  Time (mean ± σ):      95.4 ms ±   1.6 ms    [User: 91.5 ms, System: 3.2 ms]
  Range (min … max):    93.7 ms … 100.8 ms    31 runs

Benchmark 2: ./sapi/cli/php_old x.php
  Time (mean ± σ):     119.1 ms ±   1.3 ms    [User: 114.7 ms, System: 3.6 ms]
  Range (min … max):   117.6 ms … 123.1 ms    24 runs

Summary
  ./sapi/cli/php x.php ran
    1.25 ± 0.03 times faster than ./sapi/cli/php_old x.php
```

On an i7-1185G7:
```
Benchmark 1: ./sapi/cli/php x.php
  Time (mean ± σ):      67.9 ms ±   1.1 ms    [User: 64.8 ms, System: 3.2 ms]
  Range (min … max):    66.6 ms …  72.8 ms    43 runs

Benchmark 2: ./sapi/cli/php_old x.php
  Time (mean ± σ):      84.8 ms ±   1.1 ms    [User: 81.0 ms, System: 3.9 ms]
  Range (min … max):    82.6 ms …  88.0 ms    34 runs

Summary
  ./sapi/cli/php x.php  ran
    1.25 ± 0.03 times faster than ./sapi/cli/php_old x.php
```
@nielsdos nielsdos merged commit 335c0b3 into php:master Mar 30, 2025
8 of 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