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

Optimise foreach on builtin::indexed #22641

Draft
wants to merge 3 commits into
base: blead
Choose a base branch
from

Conversation

leonerd
Copy link
Contributor

@leonerd leonerd commented Oct 2, 2024

Rather than generating an entire temporary list that is twice as big as the original array, instead set a flag on the OP_ITER that tells it to set one of the iteration variables to the current array index and use the same CXt_LOOP_ARY optimisastion that regular foreach over an array would use.

Currently this is a work-in-progress:

  • Find a neater solution to the extern void ... declaration of XS_builtin_indexed at the top of op.c

  • Consider whether builtin::indexed LIST... should do the same optimisation

  • Write perldelta

  • This set of changes requires a perldelta entry, and it is not included but I'll write something before un-drafting

@leonerd
Copy link
Contributor Author

leonerd commented Oct 2, 2024

This change does have the potential impact that, before it, any modifications to the iterated-on array that happen during the body of its own foreach loop would not get seen, whereas now they would. This is the same issue that single-variable foreach on a plain array already had, so it's not new. But it is a change for existing code that already calls this function.

op.c Outdated Show resolved Hide resolved
@@ -347,6 +347,30 @@ package FetchStoreCounter {
is(prototype(\&builtin::indexed), '@', 'indexed prototype');
}

# indexed + foreach loop optimisation appears transparent
Copy link
Contributor

Choose a reason for hiding this comment

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

commit message "optimisastion" too many s even for British spelling

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahyes, fixed + forcepushed

…cals

Rather than generating an entire temporary list that is twice as big as
the original array, instead set a flag on the `OP_ITER` that tells it to
set one of the iteration variables to the current array index and use
the same `CXt_LOOP_ARY` optimisation that regular foreach over an array
would use.
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.

3 participants