ipbl/vendor/github.com/goccy/go-json/internal/encoder/compiler_race.go

46 lines
998 B
Go
Raw Normal View History

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) {
2022-06-05 23:07:32 +02:00
if typeptr > typeAddr.MaxTypeAddr || typeptr < typeAddr.BaseTypeAddr {
codeSet, err := compileToGetCodeSetSlowPath(typeptr)
if err != nil {
return nil, err
}
return getFilteredCodeSetIfNeeded(ctx, codeSet)
2021-11-07 20:44:48 +01:00
}
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
}