2021-02-04 12:00:08 +03:00
|
|
|
// +build !race
|
|
|
|
|
2021-06-03 12:49:01 +03:00
|
|
|
package decoder
|
2021-02-04 12:00:08 +03:00
|
|
|
|
2021-06-03 12:49:01 +03:00
|
|
|
import (
|
|
|
|
"unsafe"
|
2021-02-10 19:15:31 +03:00
|
|
|
|
2021-06-03 12:49:01 +03:00
|
|
|
"github.com/goccy/go-json/internal/runtime"
|
|
|
|
)
|
|
|
|
|
2021-06-03 13:10:17 +03:00
|
|
|
func CompileToGetDecoder(typ *runtime.Type) (Decoder, error) {
|
2021-02-10 19:15:31 +03:00
|
|
|
typeptr := uintptr(unsafe.Pointer(typ))
|
2021-06-03 12:49:01 +03:00
|
|
|
if typeptr > typeAddr.MaxTypeAddr {
|
2021-06-03 12:57:41 +03:00
|
|
|
return compileToGetDecoderSlowPath(typeptr, typ)
|
2021-02-04 12:00:08 +03:00
|
|
|
}
|
|
|
|
|
2021-06-03 12:49:01 +03:00
|
|
|
index := (typeptr - typeAddr.BaseTypeAddr) >> typeAddr.AddrShift
|
2021-02-04 12:00:08 +03:00
|
|
|
if dec := cachedDecoder[index]; dec != nil {
|
|
|
|
return dec, nil
|
|
|
|
}
|
|
|
|
|
2021-06-03 13:10:17 +03:00
|
|
|
dec, err := compileHead(typ, map[uintptr]Decoder{})
|
2021-02-04 12:00:08 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cachedDecoder[index] = dec
|
|
|
|
return dec, nil
|
|
|
|
}
|