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

46 lines
998 B
Go
Raw Normal View History

2022-03-26 12:13:52 +01:00
//go:build race
2021-08-30 19:45:06 +02:00
// +build race
package encoder
import (
"sync"
)
var setsMu sync.RWMutex
2022-03-26 12:13:52 +01:00
func CompileToGetCodeSet(ctx *RuntimeContext, typeptr uintptr) (*OpcodeSet, error) {
if typeptr > typeAddr.MaxTypeAddr || typeptr < typeAddr.BaseTypeAddr {
2022-07-16 11:43:41 +02:00
codeSet, err := compileToGetCodeSetSlowPath(typeptr)
if err != nil {
return nil, err
}
return getFilteredCodeSetIfNeeded(ctx, codeSet)
2021-08-30 19:45:06 +02:00
}
index := (typeptr - typeAddr.BaseTypeAddr) >> typeAddr.AddrShift
setsMu.RLock()
if codeSet := cachedOpcodeSets[index]; codeSet != nil {
2022-03-26 12:13:52 +01:00
filtered, err := getFilteredCodeSetIfNeeded(ctx, codeSet)
if err != nil {
setsMu.RUnlock()
return nil, err
}
2021-08-30 19:45:06 +02:00
setsMu.RUnlock()
2022-03-26 12:13:52 +01:00
return filtered, nil
2021-08-30 19:45:06 +02:00
}
setsMu.RUnlock()
2022-03-26 12:13:52 +01:00
codeSet, err := newCompiler().compile(typeptr)
2021-08-30 19:45:06 +02:00
if err != nil {
return nil, err
}
2022-03-26 12:13:52 +01:00
filtered, err := getFilteredCodeSetIfNeeded(ctx, codeSet)
2021-08-30 19:45:06 +02:00
if err != nil {
return nil, err
}
setsMu.Lock()
cachedOpcodeSets[index] = codeSet
setsMu.Unlock()
2022-03-26 12:13:52 +01:00
return filtered, nil
2021-08-30 19:45:06 +02:00
}