Re: [RFC] [Discussion] array_str_contains() for PHP 8.7
| From: | سپهر محمودی | Date: | Mon, 31 Aug 2026 16:17:44 +0000 |
| Subject: | Re: [RFC] [Discussion] array_str_contains() for PHP 8.7 | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-132406@lists.php.net to get a copy of this message | ||
در تاریخ دوشنبه ۳۱ اوت ۲۰۲۶، ۱۹:۲۲ Kamil Tekiela
<tekiela246@gmail.com>
نوشت:
> > 1. Short-circuiting vs Allocation: Using
array_filter
> > processes the
> entire array and allocates a new filtered array in memory, whereas a
> dedicated C implementation (array_str_contains / short-circuit
> loop)
> immediately returns true on the first match without extra
> allocations.
>
> What do you mean? I thought the purpose of the new function was to
> return a list with all matching entries? How could it short-circuit on
> the first match?
>
> > 2. Direct C-level loop: Bypassing the VM dispatch loop for each element
> gives noticeable gains, especially on larger datasets or hot paths.
>
> What kind of dispatch are you talking about here? Do you mean calling
> str_contains() for each row?
>
--------
Hi Kamil,
Thanks for pointing that out, and apologies for the confusion in my
previous wording!
1. You are totally right: the function returns an array of matched elements
(filtering), so it does iterate through the full array rather than
short-circuiting. My previous note mistakenly mixed up the behavior with an
existence-check helper like array_any(). The main benefit here is avoiding
userland closure invocation overhead for each element.
2. Exactly, by VM dispatch / call overhead, I meant the overhead of
repeatedly calling userland closures or functions (like invoking
str_contains() per element via array_filter()) compared to running a native
C loop using php_memnstr directly.
Best regards,
Sepehr