35 ms·
`s[0] == 'h'` isn't sufficient to guarantee that `s[3]` can be access without a segfault, so the compiler is not allowed to perform this optimization. If you u
by ynik 10mo ago
`s[0] == 'h'` isn't sufficient to guarantee that `s[3]` can be access without a segfault, so the compiler is not allowed to perform this optimization.
If you use `&` instead of `&&` (so that all array elements are accessed unconditionally), the optimization will happen: https://godbolt.org/z/KjdT16Kfb https://godbolt.org/z/KjdT16Kfb
(also note you got the endianness wrong in your hand-optimized version)
- abainbridge 10mo agoOoo, I'd never thought of using & like that. Interesting. > (also note you got the endianness wrong in your hand-optimized version) Doh :-)
- rdc12 10mo agoMatt Godbolt's talk on ray tracers, shows how effective that change can be. Think it was that talk anyway. https://www.youtube.com/watch?v=HG6c4Kwbv4I https://www.youtube.com/watch?v=HG6c4Kwbv4I
- NooneAtAll3 10mo agogood ol' short circuiting
- zrm 10mo ago> If you use `&` instead of `&&` (so that all array elements are accessed unconditionally), the optimization will happen But then you're accessing four elements of a string that could have a strlen of less than 3. If the strlen is 1 then the short circuit case saves you because s[1] will be '\0' instead of 'e' and then you don't access elements past the end of the string. The "optimized" version is UB for short strings.
- Denvercoder9 10mo agoYes, so that's why the compiler can't and doesn't emit the optimized version if you write the short circuited version - because it behaves differently for short strings.
- immibis 10mo agoUB doesn't exist in the processor (it does, but not here). If the compiler knows the pointer is aligned it can do the transformation.
- zrm 10mo agoFor the compiler to know the pointer is aligned it would have to actually be aligned and there is no guarantee that it is.
- kragen 10mo agoThis is fantastic, thanks! This is the approach I use in httpdito to detect the CRLFCRLF that terminates an HTTP/1.0 GET request, but I'm doing it in assembly.