Hi everyone,
After the feedback from Larry and mickmackusa, I thought it would be more helpful to lay out the concrete, real-world use cases for this function in one place, rather than discussing it in abstract terms.
The Core Problem
array_slice() always creates a new array. For large arrays, this means significant memory overhead and slowdown, just to find the position of an element or a range of elements.
array_search_range() is designed to return only the indices, without making any copy.
Real-World Use Cases
1. Pagination on large arrays
When you have an array with a hundred thousand elements and you only want to process a specific range, you currently have to copy the whole slice or loop over everything. This function gives you just the indices of that range.
2. Processing large log files
You read a big log file into an array and you want to find only the lines within a specific time range. Instead of a full scan or a copy, you get just the relevant positions.
3. Queues
When you want to find items between two positions without modifying or copying the queue. The function returns only the indices, leaving the queue untouched.
4. Database cache
When you cache a query result in an array and want to find a specific range of results without re-running the query. Instead of copying records, you just get the indices.
Why This Small Function Is Worth It-
Memory: no copy means memory usage stays constant
Speed: a bounded search instead of a full scan
Simplicity: a small function, with no dependency on a large lazy slice project
Available now: usable today, not after a multi-month RFC
Thanks for your time. I’d love to hear your thoughts, especially if any other use cases come to mind that could be added to the RFC.
Best regards,
Sepehr