package runtimeimport ()// TypeCache is a table of the values for the types, which is looked up by the address of a type// without a lock.//// It is a hash table with open addressing. It used to be an array which had a slot for every address a type of// the program can be at, which was found from the type links of the runtime: that needed a linkname, a scan of// every type at the start, memory in proportion to the size of the program, and a fallback for the types which// are not in the type links. A lookup of this table costs about the same, and it has none of them.//// The zero value is ready to use.typeTypeCache[ any] struct { cache typeCache}// Load returns the value for the type, or nil. It is small enough to be inlined into its callers, which a call of// a generic function or of a function with a loop and a switch is not: the lookup is done by typeCache, for values// as pointers.func ( *TypeCache[]) ( uintptr) * {return (*)(.cache.load())}// LoadFirst returns the value for the type if it is in the entry the type is hashed to, or nil: then Load is to be// called. It is smaller than Load, without a loop.func ( *TypeCache[]) ( uintptr) * {return (*)(.cache.loadFirst())}// Store sets the value for the type, and returns the value which the table has for the type:// it is the value which was stored first if the type was compiled by more than one goroutine at a time.func ( *TypeCache[]) ( uintptr, *) * {return (*)(.cache.store(, unsafe.Pointer()))}// typeCache is TypeCache for the values as pointers. Its words are read and written by the functions of// sync/atomic, which cost less to inline than the methods of the atomic types.type typeCache struct { table unsafe.Pointer// *typeTable, read and written atomically// mu is for the writers. A value is stored only when a type is compiled. mu sync.Mutex}type typeTable struct { entries []typeEntry// shift makes an index of entries from the hash. shift uint// count is the number of the entries in use. It is guarded by typeCache.mu. count int}// typeEntry is written once: the value, and then the type, which makes it visible.type typeEntry struct { typ uintptr// read and written atomically value unsafe.Pointer// read and written atomically}const ( minTypeTableBits = 6)// TypeHashMultiplier is the multiplier of the Fibonacci hashing of the address of a type: the top bits of the// product spread the addresses, which are aligned and close to each other, over a table.constTypeHashMultiplier = 0x9E3779B97F4A7C15func newTypeTable( uint) *typeTable {return &typeTable{entries: make([]typeEntry, 1<<),shift: 64 - , }}func ( *typeTable) ( uintptr) uintptr {returnuintptr((uint64() * TypeHashMultiplier) >> .shift)}// entry returns the entry of the index, which is always less than the length:// the check of the bounds is not worth its cost on this path, which every Marshal and Unmarshal takes.func ( *typeTable) ( uintptr) *typeEntry {return (*typeEntry)(unsafe.Add(unsafe.Pointer(unsafe.SliceData(.entries)), *unsafe.Sizeof(typeEntry{})))}// loadFirst looks at the entry the type is hashed to only: it has no loop, so that it costs little in the code of// its callers.func ( *typeCache) ( uintptr) unsafe.Pointer { := (*typeTable)(atomic.LoadPointer(&.table))if == nil {returnnil }if := .entry(.index()); atomic.LoadUintptr(&.typ) == {returnatomic.LoadPointer(&.value) }returnnil}// load looks at the entries from the one the type is hashed to, up to the type or a free entry.func ( *typeCache) ( uintptr) unsafe.Pointer { := (*typeTable)(atomic.LoadPointer(&.table))if == nil {returnnil } := uintptr(len(.entries) - 1)for := .index(); ; = ( + 1) & { := .entry()switchatomic.LoadUintptr(&.typ) {case :returnatomic.LoadPointer(&.value)case0:returnnil } }}func ( *typeCache) ( uintptr, unsafe.Pointer) unsafe.Pointer { .mu.Lock()defer .mu.Unlock() := (*typeTable)(atomic.LoadPointer(&.table))if == nil { = newTypeTable(minTypeTableBits)atomic.StorePointer(&.table, unsafe.Pointer()) }if := .load(); != nil {return }// the table is at most half full, so a lookup ends after a few entries.if (.count+1)*2 > len(.entries) { := newTypeTable(64 - .shift + 1)for := range .entries { := &.entries[]if := atomic.LoadUintptr(&.typ); != 0 { .insert(, atomic.LoadPointer(&.value)) } } .insert(, )// the table gets visible after it has every value.atomic.StorePointer(&.table, unsafe.Pointer())return } .insert(, )return}func ( *typeTable) ( uintptr, unsafe.Pointer) { := uintptr(len(.entries) - 1)for := .index(); ; = ( + 1) & { := .entry()ifatomic.LoadUintptr(&.typ) == 0 {// the value is set before the entry gets visible by the type.atomic.StorePointer(&.value, )atomic.StoreUintptr(&.typ, ) .count++return } }}
The pages are generated with Goldsv0.8.7. (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.