[PATCH 3/3] resolv: Optimize inet_ntop
Florian Weimer
fweimer@redhat.com
Wed Jun 4 08:09:27 GMT 2025
* Adhemerval Zanella:
> -static const char *
> +static inline const char *
> inet_ntop6 (const u_char *src, char *dst, socklen_t size)
> {
> /*
> @@ -108,7 +123,7 @@ inet_ntop6 (const u_char *src, char *dst, socklen_t size)
> */
> char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
> struct { int base, len; } best, cur;
> - u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
> + uint16_t words[NS_IN6ADDRSZ / NS_INT16SZ] = { 0 };
> int i;
>
> /*
> @@ -116,7 +131,6 @@ inet_ntop6 (const u_char *src, char *dst, socklen_t size)
> * Copy the input (bytewise) array into a wordwise array.
> * Find the longest run of 0x00's in src[] for :: shorthanding.
> */
> - memset(words, '\0', sizeof words);
> for (i = 0; i < NS_IN6ADDRSZ; i += 2)
> words[i / 2] = (src[i] << 8) | src[i + 1];
The words copy shouldn't really be needed, GCC can usually combine two
byte accesses into single 16-bit loads.
It should be possible to optimize put_uint16 using SWAR techniques. We
don't need to do byte-wise output, either. We can use full 32-bit
writes, potentially with overlaps.
If the user-supplied output buffer has the maximum possible size, we
don't need the temporary copy. (This applies to the IPv4 variant, too.)
Thanks,
Florian
More information about the Libc-alpha
mailing list