| 1 |
package runtime |
| 2 |
|
| 3 |
import ( |
| 4 |
"sync" |
| 5 |
"sync/atomic" |
| 6 |
"unsafe" |
| 7 |
) |
| 8 |
|
| 9 |
// TypeCache is a table of the values for the types, which is looked up by the address of a type |
| 10 |
// without a lock. |
| 11 |
// |
| 12 |
// It is a hash table with open addressing. It used to be an array which had a slot for every address a type of |
| 13 |
// the program can be at, which was found from the type links of the runtime: that needed a linkname, a scan of |
| 14 |
// every type at the start, memory in proportion to the size of the program, and a fallback for the types which |
| 15 |
// are not in the type links. A lookup of this table costs about the same, and it has none of them. |
| 16 |
// |
| 17 |
// The zero value is ready to use. |
| 18 |
type TypeCache[T any] struct { |
| 19 |
table atomic.Pointer[typeTable[T]] |
| 20 |
// mu is for the writers. A value is stored only when a type is compiled. |
| 21 |
mu sync.Mutex |
| 22 |
} |
| 23 |
|
| 24 |
type typeTable[T any] struct { |
| 25 |
entries []typeEntry[T] |
| 26 |
// shift makes an index of entries from the hash. |
| 27 |
shift uint |
| 28 |
// count is the number of the entries in use. It is guarded by TypeCache.mu. |
| 29 |
count int |
| 30 |
} |
| 31 |
|
| 32 |
// typeEntry is written once: the value, and then the type, which makes it visible. |
| 33 |
type typeEntry[T any] struct { |
| 34 |
typ atomic.Uintptr |
| 35 |
value atomic.Pointer[T] |
| 36 |
} |
| 37 |
|
| 38 |
const ( |
| 39 |
minTypeTableBits = 6 |
| 40 |
) |
| 41 |
|
| 42 |
// TypeHashMultiplier is the multiplier of the Fibonacci hashing of the address of a type: the top bits of the |
| 43 |
// product spread the addresses, which are aligned and close to each other, over a table. |
| 44 |
const TypeHashMultiplier = 0x9E3779B97F4A7C15 |
| 45 |
|
| 46 |
func newTypeTable[T any](bits uint) *typeTable[T] { |
| 47 |
return &typeTable[T]{ |
| 48 |
entries: make([]typeEntry[T], 1<<bits), |
| 49 |
shift: 64 - bits, |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
func (t *typeTable[T]) index(typ uintptr) uintptr { |
| 54 |
return uintptr((uint64(typ) * TypeHashMultiplier) >> t.shift) |
| 55 |
} |
| 56 |
|
| 57 |
// entry returns the entry of the index, which is always less than the length: |
| 58 |
// the check of the bounds is not worth its cost on this path, which every Marshal and Unmarshal takes. |
| 59 |
func (t *typeTable[T]) entry(index uintptr) *typeEntry[T] { |
| 60 |
return (*typeEntry[T])(unsafe.Add(unsafe.Pointer(unsafe.SliceData(t.entries)), index*unsafe.Sizeof(typeEntry[T]{}))) |
| 61 |
} |
| 62 |
|
| 63 |
// Load returns the value for the type, or nil. |
| 64 |
func (c *TypeCache[T]) Load(typ uintptr) *T { |
| 65 |
t := c.table.Load() |
| 66 |
if t == nil { |
| 67 |
return nil |
| 68 |
} |
| 69 |
mask := uintptr(len(t.entries) - 1) |
| 70 |
for i := t.index(typ); ; i = (i + 1) & mask { |
| 71 |
e := t.entry(i) |
| 72 |
switch e.typ.Load() { |
| 73 |
case typ: |
| 74 |
return e.value.Load() |
| 75 |
case 0: |
| 76 |
return nil |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
// Store sets the value for the type, and returns the value which the table has for the type: |
| 82 |
// it is the value which was stored first if the type was compiled by more than one goroutine at a time. |
| 83 |
func (c *TypeCache[T]) Store(typ uintptr, v *T) *T { |
| 84 |
c.mu.Lock() |
| 85 |
defer c.mu.Unlock() |
| 86 |
|
| 87 |
t := c.table.Load() |
| 88 |
if t == nil { |
| 89 |
t = newTypeTable[T](minTypeTableBits) |
| 90 |
c.table.Store(t) |
| 91 |
} |
| 92 |
if existing := c.Load(typ); existing != nil { |
| 93 |
return existing |
| 94 |
} |
| 95 |
// the table is at most half full, so a lookup ends after a few entries. |
| 96 |
if (t.count+1)*2 > len(t.entries) { |
| 97 |
grown := newTypeTable[T](64 - t.shift + 1) |
| 98 |
for i := range t.entries { |
| 99 |
e := &t.entries[i] |
| 100 |
if typ := e.typ.Load(); typ != 0 { |
| 101 |
grown.insert(typ, e.value.Load()) |
| 102 |
} |
| 103 |
} |
| 104 |
grown.insert(typ, v) |
| 105 |
// the table gets visible after it has every value. |
| 106 |
c.table.Store(grown) |
| 107 |
return v |
| 108 |
} |
| 109 |
t.insert(typ, v) |
| 110 |
return v |
| 111 |
} |
| 112 |
|
| 113 |
func (t *typeTable[T]) insert(typ uintptr, v *T) { |
| 114 |
mask := uintptr(len(t.entries) - 1) |
| 115 |
for i := t.index(typ); ; i = (i + 1) & mask { |
| 116 |
e := t.entry(i) |
| 117 |
if e.typ.Load() == 0 { |
| 118 |
// the value is set before the entry gets visible by the type. |
| 119 |
e.value.Store(v) |
| 120 |
e.typ.Store(typ) |
| 121 |
t.count++ |
| 122 |
return |
| 123 |
} |
| 124 |
} |
| 125 |
} |