44 "encoding"
55 "encoding/json"
66 "fmt"
7+ "math/big"
78 "reflect"
89 "sort"
910 "strconv"
@@ -49,19 +50,21 @@ type decodeFunc func(decoder, []byte, unsafe.Pointer) ([]byte, error)
4950type emptyFunc func (unsafe.Pointer ) bool
5051type sortFunc func ([]reflect.Value )
5152
52- var (
53- // Eventually consistent cache mapping go types to dynamically generated
54- // codecs.
55- //
56- // Note: using a uintptr as key instead of reflect.Type shaved ~15ns off of
57- // the ~30ns Marhsal/Unmarshal functions which were dominated by the map
58- // lookup time for simple types like bool, int, etc..
59- cache unsafe.Pointer // map[unsafe.Pointer]codec
60- )
53+ // Eventually consistent cache mapping go types to dynamically generated
54+ // codecs.
55+ //
56+ // Note: using a uintptr as key instead of reflect.Type shaved ~15ns off of
57+ // the ~30ns Marhsal/Unmarshal functions which were dominated by the map
58+ // lookup time for simple types like bool, int, etc..
59+ var cache atomic.Pointer [map [unsafe.Pointer ]codec ]
6160
6261func cacheLoad () map [unsafe.Pointer ]codec {
63- p := atomic .LoadPointer (& cache )
64- return * (* map [unsafe.Pointer ]codec )(unsafe .Pointer (& p ))
62+ p := cache .Load ()
63+ if p == nil {
64+ return nil
65+ }
66+
67+ return * p
6568}
6669
6770func cacheStore (typ reflect.Type , cod codec , oldCodecs map [unsafe.Pointer ]codec ) {
@@ -72,7 +75,7 @@ func cacheStore(typ reflect.Type, cod codec, oldCodecs map[unsafe.Pointer]codec)
7275 newCodecs [t ] = c
7376 }
7477
75- atomic . StorePointer ( & cache , * ( * unsafe . Pointer )( unsafe . Pointer ( & newCodecs )) )
78+ cache . Store ( & newCodecs )
7679}
7780
7881func typeid (t reflect.Type ) unsafe.Pointer {
@@ -838,6 +841,7 @@ func constructInlineValueEncodeFunc(encode encodeFunc) encodeFunc {
838841// compiles down to zero instructions.
839842// USE CAREFULLY!
840843// This was copied from the runtime; see issues 23382 and 7921.
844+ //
841845//go:nosplit
842846func noescape (p unsafe.Pointer ) unsafe.Pointer {
843847 x := uintptr (p )
@@ -1078,6 +1082,7 @@ var (
10781082 float32Type = reflect .TypeOf (float32 (0 ))
10791083 float64Type = reflect .TypeOf (float64 (0 ))
10801084
1085+ bigIntType = reflect .TypeOf (new (big.Int ))
10811086 numberType = reflect .TypeOf (json .Number ("" ))
10821087 stringType = reflect .TypeOf ("" )
10831088 stringsType = reflect .TypeOf ([]string (nil ))
@@ -1104,6 +1109,8 @@ var (
11041109 jsonUnmarshalerType = reflect .TypeOf ((* Unmarshaler )(nil )).Elem ()
11051110 textMarshalerType = reflect .TypeOf ((* encoding .TextMarshaler )(nil )).Elem ()
11061111 textUnmarshalerType = reflect .TypeOf ((* encoding .TextUnmarshaler )(nil )).Elem ()
1112+
1113+ bigIntDecoder = constructJSONUnmarshalerDecodeFunc (bigIntType , false )
11071114)
11081115
11091116// =============================================================================
0 commit comments