2021-11-22 07:20:03 +03:00
|
|
|
//go:build race
|
2021-03-16 13:44:32 +03:00
|
|
|
// +build race
|
|
|
|
|
|
|
|
package encoder
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
var setsMu sync.RWMutex
|
|
|
|
|
2021-03-17 06:32:23 +03:00
|
|
|
func CompileToGetCodeSet(typeptr uintptr) (*OpcodeSet, error) {
|
2021-03-16 18:22:19 +03:00
|
|
|
if typeptr > typeAddr.MaxTypeAddr {
|
2021-03-16 13:44:32 +03:00
|
|
|
return compileToGetCodeSetSlowPath(typeptr)
|
|
|
|
}
|
2021-05-04 03:00:10 +03:00
|
|
|
index := (typeptr - typeAddr.BaseTypeAddr) >> typeAddr.AddrShift
|
2021-03-16 13:44:32 +03:00
|
|
|
setsMu.RLock()
|
|
|
|
if codeSet := cachedOpcodeSets[index]; codeSet != nil {
|
|
|
|
setsMu.RUnlock()
|
|
|
|
return codeSet, nil
|
|
|
|
}
|
|
|
|
setsMu.RUnlock()
|
|
|
|
|
2021-11-27 15:56:31 +03:00
|
|
|
codeSet, err := newCompiler().compile(typeptr)
|
2021-03-16 13:44:32 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
setsMu.Lock()
|
|
|
|
cachedOpcodeSets[index] = codeSet
|
|
|
|
setsMu.Unlock()
|
|
|
|
return codeSet, nil
|
|
|
|
}
|