1
0
mirror of https://github.com/json-iterator/go.git synced 2025-04-20 11:28:49 +02:00
This commit is contained in:
Tao Wen 2018-02-22 10:13:38 +08:00
parent 99fc16a363
commit df8295a48a

View File

@ -3,7 +3,6 @@
package jsoniter package jsoniter
import ( import (
"reflect"
"sync" "sync"
) )
@ -15,8 +14,8 @@ type frozenConfig struct {
onlyTaggedField bool onlyTaggedField bool
disallowUnknownFields bool disallowUnknownFields bool
cacheLock *sync.RWMutex cacheLock *sync.RWMutex
decoderCache map[reflect2.Type]ValDecoder decoderCache map[uintptr]ValDecoder
encoderCache map[reflect2.Type]ValEncoder encoderCache map[uintptr]ValEncoder
extensions []Extension extensions []Extension
streamPool chan *Stream streamPool chan *Stream
iteratorPool chan *Iterator iteratorPool chan *Iterator
@ -24,30 +23,30 @@ type frozenConfig struct {
func (cfg *frozenConfig) initCache() { func (cfg *frozenConfig) initCache() {
cfg.cacheLock = &sync.RWMutex{} cfg.cacheLock = &sync.RWMutex{}
cfg.decoderCache = map[reflect2.Type]ValDecoder{} cfg.decoderCache = map[uintptr]ValDecoder{}
cfg.encoderCache = map[reflect2.Type]ValEncoder{} cfg.encoderCache = map[uintptr]ValEncoder{}
} }
func (cfg *frozenConfig) addDecoderToCache(cacheKey reflect2.Type, decoder ValDecoder) { func (cfg *frozenConfig) addDecoderToCache(cacheKey uintptr, decoder ValDecoder) {
cfg.cacheLock.Lock() cfg.cacheLock.Lock()
cfg.decoderCache[cacheKey] = decoder cfg.decoderCache[cacheKey] = decoder
cfg.cacheLock.Unlock() cfg.cacheLock.Unlock()
} }
func (cfg *frozenConfig) addEncoderToCache(cacheKey reflect2.Type, encoder ValEncoder) { func (cfg *frozenConfig) addEncoderToCache(cacheKey uintptr, encoder ValEncoder) {
cfg.cacheLock.Lock() cfg.cacheLock.Lock()
cfg.encoderCache[cacheKey] = encoder cfg.encoderCache[cacheKey] = encoder
cfg.cacheLock.Unlock() cfg.cacheLock.Unlock()
} }
func (cfg *frozenConfig) getDecoderFromCache(cacheKey reflect2.Type) ValDecoder { func (cfg *frozenConfig) getDecoderFromCache(cacheKey uintptr) ValDecoder {
cfg.cacheLock.RLock() cfg.cacheLock.RLock()
decoder, _ := cfg.decoderCache[cacheKey].(ValDecoder) decoder, _ := cfg.decoderCache[cacheKey].(ValDecoder)
cfg.cacheLock.RUnlock() cfg.cacheLock.RUnlock()
return decoder return decoder
} }
func (cfg *frozenConfig) getEncoderFromCache(cacheKey reflect2.Type) ValEncoder { func (cfg *frozenConfig) getEncoderFromCache(cacheKey uintptr) ValEncoder {
cfg.cacheLock.RLock() cfg.cacheLock.RLock()
encoder, _ := cfg.encoderCache[cacheKey].(ValEncoder) encoder, _ := cfg.encoderCache[cacheKey].(ValEncoder)
cfg.cacheLock.RUnlock() cfg.cacheLock.RUnlock()