go-json/encode_compile_norace.go

35 lines
807 B
Go
Raw Normal View History

2021-02-01 14:02:43 +03:00
// +build !race
package json
import "unsafe"
func encodeCompileToGetCodeSet(typeptr uintptr) (*opcodeSet, error) {
2021-02-09 15:37:18 +03:00
if typeptr > maxTypeAddr {
2021-02-01 14:02:43 +03:00
return encodeCompileToGetCodeSetSlowPath(typeptr)
}
2021-02-09 15:37:18 +03:00
index := typeptr - baseTypeAddr
if codeSet := cachedOpcodeSets[index]; codeSet != nil {
2021-02-01 14:02:43 +03:00
return codeSet, nil
}
// noescape trick for header.typ ( reflect.*rtype )
copiedType := *(**rtype)(unsafe.Pointer(&typeptr))
code, err := encodeCompileHead(&encodeCompileContext{
typ: copiedType,
structTypeToCompiledCode: map[uintptr]*compiledCode{},
})
if err != nil {
return nil, err
}
code = copyOpcode(code)
codeLength := code.totalLength()
codeSet := &opcodeSet{
code: code,
codeLength: codeLength,
}
2021-02-09 15:37:18 +03:00
cachedOpcodeSets[index] = codeSet
2021-02-01 14:02:43 +03:00
return codeSet, nil
}