-
Notifications
You must be signed in to change notification settings - Fork 145
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
Optimize isList #285
Optimize isList #285
Conversation
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 needs a rebase.
d550750
to
6fff132
Compare
$keys = array_keys($array); | ||
if (array_keys($keys) !== $keys) { |
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.
Try running composer run cs-fix
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 get "The system cannot find the path specified."
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've downloaded it and run it manually. Is this what you meant? It changed the array style to the old style.
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.
composer run install-tools
first.
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.
Thanks, same outcome though. It still uses the old array syntax.
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.
You need to add \
before array_keys
, I think? But cs says it passes, so maybe not.
This is a follow up to the discussion from #247.
During normal usage, I noticed this function is very inefficient. It doesn't use the built-in function
array_is_list
and the polyfil is very inefficient for most cases. Yes, the current implementation is very efficient for true lists, but has very bad performance with any other list, e.g. the one from the previous PR.The current implementation would benefit from a very simple optimization:
foreach (array_keys($array) as $k) {
but no sense in doing this if we can achieve an overall better performance with justarray_keys
.The current implementation builds a new array in memory. This is super efficient if the array was built like this
range(1, 1000)
. But if the array is large and contains other arrays or objects then building a copy of it is very bad. So we need to get rid ofarray_values()
. This also solves the issue from previous PR because we are no longer comparing the values.This is the most efficient and the most correct implementation I could find.