Skip to content
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 array_contains functions #1395

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Pieter12345
Copy link
Contributor

Improve runtime performance on array_contains(), array_scontains() and array_contains_ic() by iterating over the array values whenever possible, rather than creating a keyset to iterate over and calling the getter to get all values.

Running the same code, profiler results (where the profiler started at different points in time) do show a large difference in array_contains().

Before:
afbeelding

After:
afbeelding

Improve runtime performance on `array_contains()`, `array_scontains()` and `array_contains_ic()` by iterating over the array values whenever possible, rather than creating a keyset to iterate over and calling the getter to get all values.
@@ -814,7 +814,7 @@ public Version since() {
@Override
public Iterator<Mixed> iterator() {
if(associativeMode) {
throw new RuntimeException("iterator() cannot be called on an associative array");
return associativeArray.values().iterator();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change worries me. Inherently, it's not possible to generally say whether someone intended to iterate the keys or values when getting the iterator, and I think it's reasonable to assume the opposite of whichever way we choose here. Indeed, Java's Map doesn't directly implement Iterable for a reason (though it provides entrySet, values, etc, to make it easy to iterate in various ways). I know methodscript arrays are terribly overloaded, but I think trying to directly iterate on an associative array is a sign that you're doing something you shouldn't, and so this should remain an error. array_keys and array_normalize exist, allowing people to iterate in either way, albeit not particularly lazily. Eventually though, there will be a standard object library, which will include proper (and non-magical) maps, sets, etc, with proper iterators, but for now, I would prefer to keep this strict and not allow direct iteration on an associative array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants