Re: [Pre-RFC] Lazy iterator_* functions for iterables

From: Date: Mon, 23 Nov 2026 20:02:56 +0000
Subject: Re: [Pre-RFC] Lazy iterator_* functions for iterables
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-133208@lists.php.net to get a copy of this message
Hi Morgan, hi Ilija, > From your mention of iterator_to_array and iterator_count I take it > these new functions would take any iterable as argument (specifically: > pass them an array directly, instead of wrapping it in an ArrayIterator > first)? Morgan: Yes, each function accepts an array directly, without an ArrayIterator. Which of the functions I deferred (chunk, zip, chain, flat_map, ...) do you use most often in your library? > Small heads-up that this has been on my TODO list for the longest time. > I was planning on collaborating with Tim Düsterhus on a similar > proposal. The main challenge is to build something with acceptable > performance, particularly if compared to a manual userland loop (calling > into userland code from a C function is comparatively expensive). Hence, > we want to compile iterator calls directly into the equivalent userland > code whenever possible. Ilija: Thanks for the karma. Digging deeper, I see that this topic is more complex than I initially thought, so your experience and advice are very welcome. You're right about the performance. Earlier, I only compared the C implementation with the polyfill. Compared to a manual foreach loop, filter -> map -> reduce is 1.3x slower without JIT and 2.2x slower with JIT.. With the callback code inlined into the loop, it is 2.9x and 3.6x slower, respectively (5 million elements). Based on hand-written equivalents, rewriting only the terminal function into a loop, like the array_map() optimization from GH-20934, would not help a chain. Only folding the entire chain into a single loop would. I'd be happy to collaborate. I have an API design, tests, a polyfill, and a C implementation that could serve as a fallback. Three questions: 1. Do you plan to fold entire chains into a single loop? 2. How should strict_types be handled? I assume the optimization should also work in files with strict_types=1, where the array_map() optimization is currently disabled. 3. Should we use the iterator_ prefix or a namespace? Best regards, Paweł pon., 23 lis 2026 o 02:52 Ilija Tovilo <tovilo.ilija@gmail.com> napisał(a): > Hi Paweł > > On 22.11.26 12:26, Paweł Wankiewicz wrote: > > I'm Paweł, and this is my first post here. Before writing an official > RFC, > > I'd like to gather your feedback on a set of iterator_* functions for > > iterables. > > Small heads-up that this has been on my TODO list for the longest time. > I was planning on collaborating with Tim Düsterhus on a similar > proposal. The main challenge is to build something with acceptable > performance, particularly if compared to a manual userland loop (calling > into userland code from a C function is comparatively expensive). Hence, > we want to compile iterator calls directly into the equivalent userland > code whenever possible. > > We can discuss collaborating if our vision for the feature aligns, or we > can each proceed with our own RFC. > > > 3. Is there enough interest to move forward with a formal RFC? If so, > > could > > someone please grant wiki RFC karma to my account (pawell67)? > > I granted you RFC karma either way. Good luck! > > Ilija > -- Paweł Wankiewicz +48 886 477 169

« previous php.internals (#133208) next »