Apply substr() optimization to array_slice()
| From: | Benjamin Coutu | Date: | Fri, 27 Oct 2017 12:12:19 +0000 |
| Subject: | Apply substr() optimization to array_slice() | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-100964@lists.php.net to get a copy of this message | ||
Hello everyone,
Please consider these two statements:
substr($string, 0, $length);
array_slice($array, 0, $length, true);
Currently, with substr(), if $offset is zero and $length is smaller or equal to the original string
length we just increase the reference count of the original string and return it via
RETURN_STR_COPY.
In that case we completely save the allocation of a new string.
Now, array_slice() could be optimized similarly, but currently (unless the resulting array is
expected to be empty) we always create a new array no matter if we actually have to.
The same mechanism as used with substr() could be applied to array_slice(), given that $offset is
zero and of course only if $preserve_keys is true.
A patch would look like this:
if (length <= num_in && offset == 0 && preserve_keys) {
/* Copy the original array */
ZVAL_COPY(return_value, input);
return;
}
I'd appreciate if someone could commit this. Thanks.
Cheers,
Benjamin
--
Bejamin Coutu
ben.coutu@zeyos.com
ZeyOS, Inc.
http://www.zeyos.com