Involved Source Filescanonmap.goclone.go The unique package provides facilities for canonicalizing ("interning")
comparable values.handle.go
Package-Level Type Names (total 7, in which 1 is exported)
/* sort exporteds by: | */
Type Parameters:
T: comparable Handle is a globally unique identity for some value of type T.
Two handles compare equal exactly if the two values used to create the handles
would have also compared equal. The comparison of two handles is trivial and
typically much more efficient than comparing the values used to create them.value*T Value returns a shallow copy of the T value that produced the Handle.
Value is safe for concurrent use by multiple goroutines.
func Make[T](value T) Handle[T]
var net/netip.z0unique.Handle[addrDetail]
var net/netip.z4unique.Handle[...]
var net/netip.z6nozunique.Handle[...]
Type Parameters:
T: comparable canonMap is a map of T -> *T. The map controls the creation
of a canonical *T, and elements of the map are automatically
deleted when the canonical *T is no longer referenced.hashfunc(unsafe.Pointer, uintptr) uintptrrootatomic.Pointer[indirect[T]]seeduintptr(*canonMap[T]) Load(key T) *T(*canonMap[T]) LoadOrStore(key T) *T cleanup deletes the entry corresponding to wp in the canon map, if it's
still in the map. wp must have a Value method that returns nil by the
time this function is called. hash must be the hash of the value that
wp once pointed to (that is, the hash of *wp.Value()). expand takes oldEntry and newEntry whose hashes conflict from bit 64 down to hashShift and
produces a subtree of indirect nodes to hold the two new entries. newHash is the hash of
the value in the new entry.
func newCanonMap[T]() *canonMap[T]
Type Parameters:
T: comparable entry is a leaf node in the hash-trie.hashuintptrkeyweak.Pointer[T]nodenode[T]node.isEntrybool // Overflow for hash collisions.(*entry[T]) entry() *entry[T] hasWeakPointer returns true if the provided weak pointer can be found in the overflow chain.(*entry[T]) indirect() *indirect[T] lookup finds the entry in the overflow chain that has the provided key.
Returns the key's canonical pointer and the weak pointer for that canonical pointer. prune removes all entries in the overflow chain whose keys are nil.
The caller must hold the lock on e's parent node.
func newEntryNode[T](key T, hash uintptr) (*entry[T], *T, weak.Pointer[T])
Type Parameters:
T: comparable node is the header for a node. It's polymorphic and
is actually either an entry or an indirect.isEntrybool(*node[T]) entry() *entry[T](*node[T]) indirect() *indirect[T]
Type Parameters:
T: comparablecanonMap*canonMap[T]canonMap.hashfunc(unsafe.Pointer, uintptr) uintptrcanonMap.rootatomic.Pointer[indirect[T]]canonMap.seeduintptrcloneSeqcloneSeqcloneSeq.stringOffsets[]uintptr( uniqueMap[T]) Load(key T) *T( uniqueMap[T]) LoadOrStore(key T) *T cleanup deletes the entry corresponding to wp in the canon map, if it's
still in the map. wp must have a Value method that returns nil by the
time this function is called. hash must be the hash of the value that
wp once pointed to (that is, the hash of *wp.Value()). expand takes oldEntry and newEntry whose hashes conflict from bit 64 down to hashShift and
produces a subtree of indirect nodes to hold the two new entries. newHash is the hash of
the value in the new entry.
Package-Level Functions (total 9, in which 1 is exported)
Type Parameters:
T: comparable Make returns a globally unique handle for a value of type T. Handles
are equal if and only if the values used to produce them are equal.
Make is safe for concurrent use by multiple goroutines.
buildArrayCloneSeq populates a cloneSeq for an abi.Type that has Kind abi.Array.
buildStructCloneSeq populates a cloneSeq for an abi.Type that has Kind abi.Struct.
Type Parameters:
T: comparable clone makes a copy of value, and may update string values found in value
with a cloned version of those strings. The purpose of explicitly cloning
strings is to avoid accidentally giving a large string a long lifetime.
Note that this will clone strings in structs and arrays found in value,
and will clone value if it itself is a string. It will not, however, clone
strings if value is of interface or slice type (that is, found via an
indirection).
Pull in runtime.rand so that we don't need to take a dependency
on math/rand/v2.
Package-Level Variables (total 3, none are exported)
singleStringClone describes how to clone a single string.
uniqueMaps is an index of type-specific concurrent maps used for unique.Make.
The two-level map might seem odd at first since the HashTrieMap could have "any"
as its key type, but the issue is escape analysis. We do not want to force lookups
to escape the argument, and using a type-specific map allows us to avoid that where
possible (for example, for strings and plain-ol'-data structs). We also get the
benefit of not cramming every different type into a single map, but that's certainly
not enough to outweigh the cost of two map lookups. What is worth it though, is saving
on those allocations.
16 children. This seems to be the sweet spot for
load performance: any smaller and we lose out on
50% or more in CPU performance. Any larger and the
returns are minuscule (~1% improvement for 32 children).