2021-11-27 20:14:57 +03:00
|
|
|
//go:build race
|
2021-03-16 13:44:32 +03:00
|
|
|
// +build race
|
|
|
|
|
|
|
|
package encoder
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
var setsMu sync.RWMutex
|
|
|
|
|
2022-01-03 06:33:51 +03:00
|
|
|
func CompileToGetCodeSet(ctx *RuntimeContext, typeptr uintptr) (*OpcodeSet, error) {
|
2022-03-19 03:33:16 +03:00
|
|
|
if typeptr > typeAddr.MaxTypeAddr || typeptr < typeAddr.BaseTypeAddr {
|
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 {
|
2022-01-03 06:33:51 +03:00
|
|
|
filtered, err := getFilteredCodeSetIfNeeded(ctx, codeSet)
|
|
|
|
if err != nil {
|
|
|
|
setsMu.RUnlock()
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
setsMu.RUnlock()
|
2022-01-03 06:33:51 +03:00
|
|
|
return filtered, nil
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|
|
|
|
setsMu.RUnlock()
|
|
|
|
|
2021-11-27 20:14:57 +03:00
|
|
|
codeSet, err := newCompiler().compile(typeptr)
|
2021-03-16 13:44:32 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-03 06:33:51 +03:00
|
|
|
filtered, err := getFilteredCodeSetIfNeeded(ctx, codeSet)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-16 13:44:32 +03:00
|
|
|
setsMu.Lock()
|
|
|
|
cachedOpcodeSets[index] = codeSet
|
|
|
|
setsMu.Unlock()
|
2022-01-03 06:33:51 +03:00
|
|
|
return filtered, nil
|
2021-03-16 13:44:32 +03:00
|
|
|
}
|