Hasty Briefsbeta

Bilingual

Hacking the Go compiler to efficiently map IPv4 to IPv6

2 days ago
  • Go's netip.Addr lacks a native To6() method to map IPv4 to IPv4-mapped IPv6; maintainers suggest using netip.AddrFrom16(ip.As16()) but this pattern is 8x slower.
  • Three implementation approaches were tested: modifying the standard library (fastest), a helper function using existing API (slow), and an unsafe function (matching builtin performance).
  • The helper function's slowness stems from Go's compiler not optimizing out the pack-copy-unpack sequence of bytes, resulting in extra instructions.
  • Hacking the Go compiler's SSA phase by adding rewrite rules and running memcombine earlier reduced the helper to the same speed as builtin (0.89 ns vs 0.88 ns).
  • The proposed compiler change may be rejected due to complexity; the author suggests adding a To6() method to netip.Addr as a simpler solution.