2021-12-12 17:33:40 +01:00
|
|
|
//go:build race
|
2021-11-07 20:44:48 +01:00
|
|
|
// +build race
|
|
|
|
|
|
|
|
package encoder
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
var setsMu sync.RWMutex
|
|
|
|
|
2022-02-22 14:43:07 +01:00
|
|
|
func CompileToGetCodeSet(ctx *RuntimeContext, typeptr uintptr) (*OpcodeSet, error) {
|
2021-11-07 20:44:48 +01:00
|
|
|
if typeptr > typeAddr.MaxTypeAddr {
|
|
|
|
return compileToGetCodeSetSlowPath(typeptr)
|
|
|
|
}
|
|
|
|
index := (typeptr - typeAddr.BaseTypeAddr) >> typeAddr.AddrShift
|
|
|
|
setsMu.RLock()
|
|
|
|
if codeSet := cachedOpcodeSets[index]; codeSet != nil {
|
2022-02-22 14:43:07 +01:00
|
|
|
filtered, err := getFilteredCodeSetIfNeeded(ctx, codeSet)
|
|
|
|
if err != nil {
|
|
|
|
setsMu.RUnlock()
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-07 20:44:48 +01:00
|
|
|
setsMu.RUnlock()
|
2022-02-22 14:43:07 +01:00
|
|
|
return filtered, nil
|
2021-11-07 20:44:48 +01:00
|
|
|
}
|
|
|
|
setsMu.RUnlock()
|
|
|
|
|
2021-12-12 17:33:40 +01:00
|
|
|
codeSet, err := newCompiler().compile(typeptr)
|
2021-11-07 20:44:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-02-22 14:43:07 +01:00
|
|
|
filtered, err := getFilteredCodeSetIfNeeded(ctx, codeSet)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-07 20:44:48 +01:00
|
|
|
setsMu.Lock()
|
|
|
|
cachedOpcodeSets[index] = codeSet
|
|
|
|
setsMu.Unlock()
|
2022-02-22 14:43:07 +01:00
|
|
|
return filtered, nil
|
2021-11-07 20:44:48 +01:00
|
|
|
}
|