Stories
Slash Boxes
Comments

SoylentNews is people

posted by hubie on Wednesday October 07, @01:37PM   Printer-friendly
from the go-forth-and-code dept.

https://vincent.bernat.ch/en/blog/2026-go-netip-addrto6

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. 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. Today, this pattern is eight times slower than a native method. How can we teach the compiler to optimize this sequence?

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(), hoping the compiler eventually optimizes it. The unsafe package opens a third path, with the same performance as the first solution.

[...]

The Go compiler has several phases:

  • Parsing: The compiler tokenizes and parses the source code. It builds a syntax tree for each source file.
  • Type checking: The compiler maps each identifier to the object it denotes, folds constants, and infers the type of every expression.
  • IR construction: The compiler converts the syntax tree and its types into its own intermediate representation (IR). This process, called "noding," goes through a serialization format named unified IR.
  • Middle end: The compiler performs several optimization passes on the IR, such as devirtualization, function call inlining, and escape analysis.
  • Walk: This phase runs two steps: order of evaluation decomposes complex statements into simpler ones, and desugaring transforms higher-level Go constructs, like switch or channels, into more primitive instructions or calls to the runtime.
  • Generic SSA: The compiler converts the IR into Static Single Assignment (SSA) form, a lower-level intermediate representation suited for machine-independent optimizations and rewrite rules.
  • Machine code generation: The compiler rewrites the SSA form into machine-specific variants, allocates registers, and applies more optimization passes. At the end, the assembler turns the generated instructions into machine code.

Original Submission

This discussion was created by hubie (1068) for logged-in users only, but now has been archived. No new comments can be posted.
Display Options Threshold/Breakthrough Mark All as Read Mark All as Unread
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
(1)
  • (Score: 4, Informative) by VLM on Wednesday October 07, @04:38PM

    by VLM (445) on Wednesday October 07, @04:38PM (#1455989)

    I've written some Golang stuff mostly fooling around and its pretty nice. Tinygo is also cool on big microcontrollers but you better bring lotsa RAM.

    I would caution people who've never seen golang but read this article, that this is of the genre of "go makes it possible to relatively cleanly do the impossible" rather than being your typical hello_world.go experience.

    If you want to see "real golang" try https://gobyexample.com/ [gobyexample.com]

    Some cool lookin golang flavored golang thats a bit clearer and a bit less do the impossible:

    You can do mutex's the hard way https://gobyexample.com/mutexes [gobyexample.com]

    But why not use a simpler solution https://gobyexample.com/stateful-goroutines [gobyexample.com]

    Channels are super clean in Go https://gobyexample.com/channel-synchronization [gobyexample.com]

    Its templater is pretty cool for a compiled language https://gobyexample.com/text-templates [gobyexample.com]

    Context based signal handling is pretty clean compared to other languages https://gobyexample.com/signals [gobyexample.com]

    So yeah the linked article is cool but not representative of typical golang development. Although its cool that its possible.

  • (Score: 3, Interesting) by krishnoid on Wednesday October 07, @08:30PM

    by krishnoid (1156) on Wednesday October 07, @08:30PM (#1456002)

    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.

    High-performance networking [wikipedia.org], indeed (no citation provided for that, though).

  • (Score: 1, Touché) by Anonymous Coward on Thursday October 08, @02:53AM

    by Anonymous Coward on Thursday October 08, @02:53AM (#1456016)

    Go maintainers prefer a small external helper chaining netip.AddrFrom16() and netip.Addr.As16(), hoping the compiler eventually optimizes it.

    Yes this is the modern way. Just hopes and dreams that the compiler will somehow manage to produce good code. Whether the compiler actually produces good code is not important.

  • (Score: 2) by Ingar on Friday October 09, @02:45PM

    by Ingar (801) on Friday October 09, @02:45PM (#1456110) Homepage Journal

    A bet those two programs that have to unwrap IPv4-mapped IPv6 addresses would run so much faster with an optimized standard library function!

    --
    Love is a three-edged sword: heart, soul, and reality.
(1)