[7.x] 3.5x faster micro performance enhancement for data_get()#32192
Conversation
Operation is 10x faster
|
how are you accounting for possible |
That never happens. Keys are either strings or integers. NB If you do an array access with another type of scalar, PHP will silently convert it (if it's numeric, to an integer then to a string, otherwise, straight to a string), and otherwise, it will raise a warning (or on PHP 8 I think it raises an actual Error). |
|
correct, but |
I was not sure of the purpose of $data = [
'product-one' => ['name' => ['sku' => 123], 'sku' => 987],
'product-two' => ['name' => ['sku' => 123], 'sku' => 987],
];
dd(data_get($data, ['*', null, 'sku']));I have added a check for this but I'd assume this issue is an edge case. Maybe it should throw an exception instead. I'd imagine anyone sending If |
|
The purpose of it originally was to figure out when the According to the
This way, the As I'm looking back at the old code, having a while(array_shift([null, 1, null])) {
echo 'test';
}In this example, "test" will never be echoed. |
|
Based on my testing adding the However, on the other hand, the |
|
So after this discussion I'm confused. Is there ANY behavior change here at all? |
|
I believe the latest commit that added the NULL check makes this code functionally equivalent to the old code. I ran a couple scenarios, but would love for a second set of eyes on it. |
| foreach ($key as $i => $segment) { | ||
| unset($key[$i]); | ||
|
|
||
| if ($segment === null) { |
There was a problem hiding this comment.
there was a PR awhile ago that switched $var === null to is_null($var).
https://github.com/laravel/framework/pull/15655/files
Might want to switch this line to maintain consistency.
There was a problem hiding this comment.
This PR is in feature parity with the current 7.x state.
There was a problem hiding this comment.
I agree the behavior is the same, but to maintain style consistency, we should switch $segment === null to is_null($segment).
Also, FYI. Parity, not parody.
|
Thank you |
Replace
while:array_shiftwithforeach:unset. This operation is 3.5x faster. It is arguably more readable as well.In my isolation tests with 1000 iterations:
1.969ms0.527ms