gio/gdatainputstream: use memchr() when possible

Scanning for stop chars can require looking through a considerable amount
of input data. In the case there is a single stop character, use memchr()
which can be optimized by the compiler to look at word size or greater
amounts of data at a time.
This commit is contained in:
Christian Hergert 2024-10-02 13:33:17 -07:00
parent 17124abc7e
commit e7e5ddd2ae

View File

@ -870,13 +870,26 @@ scan_for_chars (GDataInputStream *stream,
end = available; end = available;
peeked = end - start; peeked = end - start;
for (i = 0; checked < available && i < peeked; i++) /* For single-char case such as \0, defer to memchr which can
* take advantage of simd/etc.
*/
if (stop_chars_len == 1)
{ {
for (stop_char = stop_chars; stop_char != stop_end; stop_char++) const char *p = memchr (buffer, stop_chars[0], peeked);
{
if (buffer[i] == *stop_char) if (p != NULL)
return (start + i); return start + (p - buffer);
} }
else
{
for (i = 0; checked < available && i < peeked; i++)
{
for (stop_char = stop_chars; stop_char != stop_end; stop_char++)
{
if (buffer[i] == *stop_char)
return (start + i);
}
}
} }
checked = end; checked = end;