Source File
handle.go
Belonging Package
unique
// Copyright 2024 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.package uniqueimport (isync)var zero uintptr// 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.type Handle[ comparable] struct {value *}// Value returns a shallow copy of the T value that produced the Handle.// Value is safe for concurrent use by multiple goroutines.func ( Handle[]) () {return *.value}// 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.func [ comparable]( ) Handle[] {// Find the map for type T.:= abi.TypeFor[]()if .Size() == 0 {return Handle[]{(*)(unsafe.Pointer(&zero))}}, := uniqueMaps.Load()if ! {:= &uniqueMap[]{canonMap: newCanonMap[](), cloneSeq: makeCloneSeq()}, _ = uniqueMaps.LoadOrStore(, )}:= .(*uniqueMap[])// Find the value in the map.:= .Load()if == nil {// Insert a new value into the map.= .LoadOrStore(clone(, &.cloneSeq))}return Handle[]{}}// 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.var uniqueMaps isync.HashTrieMap[*abi.Type, any] // any is always a *uniqueMap[T].type uniqueMap[ comparable] struct {*canonMap[]cloneSeq}
![]() |
The pages are generated with Golds v0.7.9-preview. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |