Member-only story
New package in Go 1.23: unique
unique
The package provides some tools for normalizing "comparable values" (i.e. "residency").Specifically, “normalization” or “interning” refers to merging multiple identical values (such as strings or structures with the same content) into a unique replica through some mechanism. In this way, when there are multiple identical values, only one standardized version will be saved in memory, and all other identical values will point to this unique copy, thereby saving memory and accelerating equality comparison operations.
On the official Go blog, Michael Knyszek, the creator of the unique package, wrote an article about Introduction to the unique package And introduced some new discoveries during the implementation of this package (weak pointers, terminator alternatives). The following is the translation:
The Go 1.23 standard library has introduced a new package calledunique
. The goal of this package is to achieve comparable normalization. Simply put, it allows you to deduplicate values so that they point to unique, normalized replicas, and efficiently manage these normalized replicas at the underlying level. You may already have some understanding of this concept, which is called 'interning'. Let's delve into how it works and why it's useful.
A simple residency implementation
althoughunique
Packages are very useful, but they are different from the residency of strings because they are used to prevent strings from being deleted from the…