[PATCH 1/1] string: Add stpecpy(3)
Alejandro Colomar
alx.manpages@gmail.com
Tue Dec 27 00:12:03 GMT 2022
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. :)
--
<http://www.alejandro-colomar.es/>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature
Type: application/pgp-signature
Size: 833 bytes
Desc: OpenPGP digital signature
URL: <https://sourceware.org/pipermail/libc-alpha/attachments/20221227/eb38e715/attachment.sig>
More information about the Libc-alpha
mailing list