From 69a99fdcbbb1e7642329e62f0b281c1920860e48 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Wed, 13 May 2026 21:39:46 +0200 Subject: [PATCH] net/netip: inline single-use Addr.string{4,6,4In6} methods These are only used once in Addr.String. Change-Id: I9f04382934152f86b823f6032c142feb1179c02a Reviewed-on: https://go-review.googlesource.com/c/go/+/777760 Reviewed-by: Nicholas Husin Reviewed-by: Sean Liao Reviewed-by: Nicholas Husin Auto-Submit: Tobias Klauser Reviewed-by: Dmitri Shuralyov LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com --- src/net/netip/netip.go | 67 +++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 40 deletions(-) diff --git a/src/net/netip/netip.go b/src/net/netip/netip.go index ba6f9ccd35cf20..5a788205584682 100644 --- a/src/net/netip/netip.go +++ b/src/net/netip/netip.go @@ -785,17 +785,32 @@ func (ip Addr) Prev() Addr { // IPv4-mapped IPv6 addresses format with a "::ffff:" // prefix before the dotted quad. func (ip Addr) String() string { - switch ip.z { - case z0: + if !ip.IsValid() { return "invalid IP" - case z4: - return ip.string4() + } + var b []byte + switch { + case ip.z == z4: + const max = len("255.255.255.255") + b = make([]byte, 0, max) + b = ip.appendTo4(b) + case ip.Is4In6(): + const max = len("::ffff:255.255.255.255%enp5s0") + b = make([]byte, 0, max) + b = ip.appendTo4In6(b) default: - if ip.Is4In6() { - return ip.string4In6() - } - return ip.string6() + // Use a zone with a "plausibly long" name, so that most zone-ful + // IP addresses won't require additional allocation. + // + // The compiler does a cool optimization here, where b ends up + // stack-allocated and so the only allocation this function does + // is to construct the returned string. As such, it's okay to be a + // bit greedy here, size-wise. + const max = len("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff%enp5s0") + b = make([]byte, 0, max) + b = ip.appendTo6(b) } + return string(b) } // AppendTo appends a text encoding of ip, @@ -855,13 +870,6 @@ func appendHexPad(b []byte, x uint16) []byte { return append(b, digits[x>>12], digits[x>>8&0xf], digits[x>>4&0xf], digits[x&0xf]) } -func (ip Addr) string4() string { - const max = len("255.255.255.255") - ret := make([]byte, 0, max) - ret = ip.appendTo4(ret) - return string(ret) -} - func (ip Addr) appendTo4(ret []byte) []byte { ret = appendDecimal(ret, ip.v4(0)) ret = append(ret, '.') @@ -873,13 +881,6 @@ func (ip Addr) appendTo4(ret []byte) []byte { return ret } -func (ip Addr) string4In6() string { - const max = len("::ffff:255.255.255.255%enp5s0") - ret := make([]byte, 0, max) - ret = ip.appendTo4In6(ret) - return string(ret) -} - func (ip Addr) appendTo4In6(ret []byte) []byte { ret = append(ret, "::ffff:"...) ret = ip.Unmap().appendTo4(ret) @@ -890,25 +891,11 @@ func (ip Addr) appendTo4In6(ret []byte) []byte { return ret } -// string6 formats ip in IPv6 textual representation. It follows the -// guidelines in section 4 of RFC 5952 -// (https://tools.ietf.org/html/rfc5952#section-4): no unnecessary -// zeros, use :: to elide the longest run of zeros, and don't use :: +// appendTo6 formats ip in IPv6 textual representation, appends the result to +// the byte slice, and returns the updated slice. It follows the guidelines in +// section 4 of RFC 5952 (https://tools.ietf.org/html/rfc5952#section-4): no +// unnecessary zeros, use :: to elide the longest run of zeros, and don't use :: // to compact a single zero field. -func (ip Addr) string6() string { - // Use a zone with a "plausibly long" name, so that most zone-ful - // IP addresses won't require additional allocation. - // - // The compiler does a cool optimization here, where ret ends up - // stack-allocated and so the only allocation this function does - // is to construct the returned string. As such, it's okay to be a - // bit greedy here, size-wise. - const max = len("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff%enp5s0") - ret := make([]byte, 0, max) - ret = ip.appendTo6(ret) - return string(ret) -} - func (ip Addr) appendTo6(ret []byte) []byte { zeroStart, zeroEnd := uint8(255), uint8(255) for i := uint8(0); i < 8; i++ {