Perl / perl5

🐪 The Perl programming language
https://dev.perl.org/perl5/
Other
1.95k stars 554 forks source link

Optimise foreach on builtin::indexed #22641

Open leonerd opened 2 weeks ago

leonerd commented 2 weeks ago

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:

leonerd commented 2 weeks ago

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.