-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[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. |
src/Illuminate/Support/helpers.php
Outdated
foreach ($key as $i => $segment) { | ||
unset($key[$i]); | ||
|
||
if ($segment === null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR is in feature parity with the current 7.x state.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So after this discussion I'm confused. Is there ANY behavior change here at all?
There is no behavior change. It is in parity.
Thank you |
Replace
while:array_shift
withforeach:unset
. This operation is 3.5x faster. It is arguably more readable as well.In my isolation tests with 1000 iterations:
1.969ms
0.527ms