-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cow cache is not same, as map read will modify the underlying map. us…
…e sync.Map for 1.9 and above, and mutex if sync.Map not available
- Loading branch information
Showing
3 changed files
with
106 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
//+build go1.9 | ||
|
||
package jsoniter | ||
|
||
import ( | ||
"reflect" | ||
"sync" | ||
) | ||
|
||
type frozenConfig struct { | ||
configBeforeFrozen Config | ||
sortMapKeys bool | ||
indentionStep int | ||
objectFieldMustBeSimpleString bool | ||
onlyTaggedField bool | ||
decoderCache sync.Map | ||
encoderCache sync.Map | ||
extensions []Extension | ||
streamPool chan *Stream | ||
iteratorPool chan *Iterator | ||
} | ||
|
||
func (cfg *frozenConfig) initCache() { | ||
cfg.decoderCache = sync.Map{} | ||
cfg.encoderCache = sync.Map{} | ||
} | ||
|
||
|
||
func (cfg *frozenConfig) addDecoderToCache(cacheKey reflect.Type, decoder ValDecoder) { | ||
cfg.decoderCache.Store(cacheKey, decoder) | ||
} | ||
|
||
func (cfg *frozenConfig) addEncoderToCache(cacheKey reflect.Type, encoder ValEncoder) { | ||
cfg.encoderCache.Store(cacheKey, encoder) | ||
} | ||
|
||
func (cfg *frozenConfig) getDecoderFromCache(cacheKey reflect.Type) ValDecoder { | ||
decoder, found := cfg.decoderCache.Load(cacheKey) | ||
if found { | ||
return decoder.(ValDecoder) | ||
} | ||
return nil | ||
} | ||
|
||
func (cfg *frozenConfig) getEncoderFromCache(cacheKey reflect.Type) ValEncoder { | ||
encoder, found := cfg.encoderCache.Load(cacheKey) | ||
if found { | ||
return encoder.(ValEncoder) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//+build !go1.9 | ||
|
||
package jsoniter | ||
|
||
import ( | ||
"reflect" | ||
"sync" | ||
) | ||
|
||
type frozenConfig struct { | ||
configBeforeFrozen Config | ||
sortMapKeys bool | ||
indentionStep int | ||
objectFieldMustBeSimpleString bool | ||
onlyTaggedField bool | ||
cacheLock *sync.RWMutex | ||
decoderCache map[reflect.Type]ValDecoder | ||
encoderCache map[reflect.Type]ValEncoder | ||
extensions []Extension | ||
streamPool chan *Stream | ||
iteratorPool chan *Iterator | ||
} | ||
|
||
func (cfg *frozenConfig) initCache() { | ||
cfg.cacheLock = &sync.RWMutex{} | ||
cfg.decoderCache = map[reflect.Type]ValDecoder{} | ||
cfg.encoderCache = map[reflect.Type]ValEncoder{} | ||
} | ||
|
||
func (cfg *frozenConfig) addDecoderToCache(cacheKey reflect.Type, decoder ValDecoder) { | ||
cfg.cacheLock.Lock() | ||
cfg.decoderCache[cacheKey] = decoder | ||
cfg.cacheLock.Unlock() | ||
} | ||
|
||
func (cfg *frozenConfig) addEncoderToCache(cacheKey reflect.Type, encoder ValEncoder) { | ||
cfg.cacheLock.Lock() | ||
cfg.encoderCache[cacheKey] = encoder | ||
cfg.cacheLock.Unlock() | ||
} | ||
|
||
func (cfg *frozenConfig) getDecoderFromCache(cacheKey reflect.Type) ValDecoder { | ||
cfg.cacheLock.RLock() | ||
decoder, _ := cfg.decoderCache[cacheKey].(ValDecoder) | ||
cfg.cacheLock.RUnlock() | ||
return decoder | ||
} | ||
|
||
func (cfg *frozenConfig) getEncoderFromCache(cacheKey reflect.Type) ValEncoder { | ||
cfg.cacheLock.RLock() | ||
encoder, _ := cfg.encoderCache[cacheKey].(ValEncoder) | ||
cfg.cacheLock.RUnlock() | ||
return encoder | ||
} |