Hacking the Go compiler to efficiently map IPv4 to IPv6 1 ▲ Vincent Bernat 1 hour ago · 28 min read5517 words · Tech · hide · 0 comments netip.Addr features an Unmap() method returning the unwrapped IPv4 contained in an IPv4-mapped IPv6 address: from ::ffff:203.0.113.10 or ::ffff:cb00:710a, it returns 203.0.113.10.1 There is no Map() or To6() method for the reverse direction. Such a method is trivial to implement, but Go maintainers have rejected it on the grounds that users should write netip.AddrFrom16(ip.As16()) and let the compiler optimize it.2 Today, this pattern is eight times slower than a native method. How can we teach the compiler to optimize this sequence? The alternatives Modifying the Go standard library As a helper As an unsafe function Benchmarks Assembly code Hacking the Go compiler The hammer The screwdriver On paper In practice Out of order Back in order Next steps The alternatives Let’s explore three ways to implement the map semantics for netip.Addr. My favorite is to add it to the Go standard library. Go maintainers prefer a small external helper chaining netip.AddrFrom16() and netip.Addr.As16(),… No comments yet. Log in to reply on the Fediverse. Comments will appear here.