Skip to content

Optimize array_slice for packed/unpacked arrays with large offsets #4860

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

Closed
wants to merge 2 commits into from

Conversation

TysonAndre
Copy link
Contributor

If the offset is 100000, and there are no gaps in the packed array,
then advance the pointer once by 100000(1+99999),
instead of looping and skipping 100000 times.

This was validated with this test script. Unit tests continue to pass locally:

<?php
$example = range('x', 1000000);
$start = microtime(true);
$output = '';
for ($i = 0; $i < 10; $i++) {
    $value = array_slice($example, 999900 + $i, 5);
    $output .= json_encode($value) . "\n";
}
// With this patch:    0.000010 (best run)
// Without this patch: 0.026424
printf("Elapsed time: %.6f\n", microtime(true) - $start);
echo $output;

@TysonAndre
Copy link
Contributor Author

Also, I should probably make the same optimization for $preserve_keys=true - It'd be inconsistent to have one be thousands of times slower than the other.

@TysonAndre TysonAndre force-pushed the array_slice-optimization branch from 8af7046 to 602d104 Compare October 25, 2019 23:04
@TysonAndre TysonAndre changed the title Optimize array_slice for packed arrays with large offsets Optimize array_slice for packed/unpacked arrays with large offsets Oct 25, 2019
@TysonAndre
Copy link
Contributor Author

The review comments have been addressed. The build failure was due to a mac OS job encountering a networking issue.

If the offset is 100000, and there are no gaps in the packed/unpacked array,
then advance the pointer once by 100000,
instead of looping and skipping 100000 times.

Add a new test of array_slice handling unset offsets.
@TysonAndre TysonAndre force-pushed the array_slice-optimization branch from afa3dfb to c6c0eb8 Compare October 26, 2019 14:20
@php-pulls php-pulls closed this in e7ff590 Oct 28, 2019
TysonAndre added a commit to TysonAndre/php-src that referenced this pull request Oct 29, 2019
Fix folding for the new helper method.

Clarify comment in UPGRADING:
The performance on associative arrays would also improve,
as long as no offsets were unset (no gaps).
Packed arrays can have gaps.
php-pulls pushed a commit that referenced this pull request Oct 30, 2019
Fix folding for the new helper method.

Clarify comment in UPGRADING:
The performance on associative arrays would also improve,
as long as no offsets were unset (no gaps).
Packed arrays can have gaps.

Closes GH-4873.
[ci skip]
@carusogabriel carusogabriel added this to the PHP 8.0 milestone May 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants