The fastest way to find the first occurrence of a byte in a buffer

Denial of responsibility


I am looking for the fastest way to identify the first occurrence of a given byte in a byte buffer.

This is similar to finding the first character in a string, except that:

  • the byte buffer is not NUL terminated, instead I have an explicit length (and possibly inline NUL characters)
  • the byte buffer is not allocated in stringor vector, I only get a slice (aka, pointer and length)

Basic solution:

size_t search(char const* buffer, size_t length, char c) {
    return std::find(buffer, buffer + length, c) - buffer;
}

, Godbolt (-O2 -msse2 -mavx) , , , .

?

: .

. x86_64 Linux, , .

+4
1

memchr, ( ) , .

http://en.cppreference.com/w/c/string/byte/memchr

: , V++ ( GCC, ), std::find memchr , , , memchr .

+4

Source: https://habr.com/ru/post/1660940/


All Articles