Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Optimize isList #285

Merged
merged 2 commits into from
Oct 16, 2024
Merged

Optimize isList #285

merged 2 commits into from
Oct 16, 2024

Conversation

kamil-tekiela
Copy link
Contributor

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 just array_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 of array_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.

Copy link
Collaborator

@shadowhand shadowhand left a 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.

Comment on lines +1891 to +1892
$keys = array_keys($array);
if (array_keys($keys) !== $keys) {
Copy link
Collaborator

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

Copy link
Contributor Author

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."

Copy link
Contributor Author

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.

Copy link
Collaborator

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.

Copy link
Contributor Author

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.

Copy link
Collaborator

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.

@shadowhand shadowhand merged commit fd088c3 into webmozarts:master Oct 16, 2024
11 checks passed
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants