Disclaimer
- I have read What is the fastest substring search algorithm?, which may be suboptimal for the single character case.
strchr
requires a NUL-terminated string
I am looking for the fastest way to identify the first occurrence of a given byte in a byte buffer.
This is reminiscent of looking for the first occurrence of a character in a string except that:
- the byte buffer is not NUL-terminated, instead I have an explicit length (and possibly embedded NUL characters)
- the byte buffer is not allocated in a
string
orvector
, I am only handed down a slice (aka, pointer & length)
The basic solution is:
size_t search(char const* buffer, size_t length, char c) {
return std::find(buffer, buffer + length, c) - buffer;
}
However, a quick round-trip with the Godbolt compiler (-O2 -msse2 -mavx
) does not show any hint of a vectorized instruction, only some unrolling, so I am wondering whether this is the optimal.
Is there a faster way to find the first occurrence of a given byte in a buffer?
Note: only the first occurrence matters.
Note: I am exclusively concerned with modern x86_64 CPUs on Linux, though I encourage answers to be as generic as possible and mention assumptions clearly.