Interest in having a mempbrk() function in glibc

Zhao Wei Liew zhaoweiliew@gmail.com
Thu Jan 6 06:44:30 GMT 2022


Hi,

Is there any interest in having a mempbrk() function in glibc? It would be
the mem* version of strpbrk(); it takes a len parameter instead of relying
on the string being null-terminated.

I ask because it seems that there is significant interest in the wild, with
several projects implementing their own mempbrk(), such as Abseil [1] and
Wireshark [2]. In fact, Wireshark even implements a SSE4.2 version of
mempbrk() for strings with length 16 or more [3].

Personally, I've come across a related missed optimization with
libstdc++'s string_view::find_first_of(), which is implemented with
memchr() [4]. It appears that the whole missed optimization could've been
avoided if string_view::find_first_of() was implemented with a mempbrk()
function.

Hence, may I know what your thoughts are of having mempbrk() in glibc?
Thanks!

[1]:
https://github.com/abseil/abseil-cpp/blob/1ae9b71c474628d60eb251a3f62967fe64151bb2/absl/strings/internal/memutil.cc#L76

[2]:
https://github.com/wireshark/wireshark/blob/eb5f4eea99593b92298bacecc5c9d885cc13a9ad/wsutil/ws_mempbrk.c#L64

[3]:
https://github.com/wireshark/wireshark/blob/eb5f4eea99593b92298bacecc5c9d885cc13a9ad/wsutil/ws_mempbrk_sse42.c#L99

[4]:
https://stackoverflow.com/questions/70433152/missed-optimization-with-string-viewfind-first-of


More information about the Libc-alpha mailing list