On 12/27/22 00:52, Alejandro Colomar wrote: > However, and this is interesting, calling strnlen(3) results in faster code even > when no truncation occurs; at least for the short string I tested. > > I suspect it might be because it is already heavily optimized in glibc, and it > implicitly simplifies surrounding code: > > -       slen = strlen(src); >         dsize = end - dst; > -       trunc = (slen >= dsize); > +       slen = strnlen(src, dsize); > +       trunc = (slen == dsize); > > The generated assembly code is one line smaller (on my system, and phase of the > moon), and some small percent faster.  :) I found the reason; it helps simplify considerably the code. Here's the resulting optimized code: char *stp_nullable stpecpy(char *stp_nullable dst, char *end, const char *restrict src) { bool trunc; size_t dsize, dlen, slen; if (dst == end) return end; if (stp_unlikely(dst == NULL)) // Allow chaining with stpeprintf(). return NULL; stp_impossible(dst > end); dsize = end - dst; slen = strnlen(src, dsize); trunc = (slen == dsize); dlen = slen - trunc; dst[dlen] = '\0'; return mempcpy(dst, src, dlen) + trunc; } See how using strnlen(3) removed the ternary operator. That's a great optimization. :) --