feature_config_with_sync_map.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //+build go1.9
  2. package jsoniter
  3. import (
  4. "reflect"
  5. "sync"
  6. )
  7. type frozenConfig struct {
  8. configBeforeFrozen Config
  9. sortMapKeys bool
  10. indentionStep int
  11. objectFieldMustBeSimpleString bool
  12. onlyTaggedField bool
  13. disallowUnknownFields bool
  14. decoderCache sync.Map
  15. encoderCache sync.Map
  16. extensions []Extension
  17. streamPool chan *Stream
  18. iteratorPool chan *Iterator
  19. }
  20. func (cfg *frozenConfig) initCache() {
  21. cfg.decoderCache = sync.Map{}
  22. cfg.encoderCache = sync.Map{}
  23. }
  24. func (cfg *frozenConfig) addDecoderToCache(cacheKey reflect.Type, decoder ValDecoder) {
  25. cfg.decoderCache.Store(cacheKey, decoder)
  26. }
  27. func (cfg *frozenConfig) addEncoderToCache(cacheKey reflect.Type, encoder ValEncoder) {
  28. cfg.encoderCache.Store(cacheKey, encoder)
  29. }
  30. func (cfg *frozenConfig) getDecoderFromCache(cacheKey reflect.Type) ValDecoder {
  31. decoder, found := cfg.decoderCache.Load(cacheKey)
  32. if found {
  33. return decoder.(ValDecoder)
  34. }
  35. return nil
  36. }
  37. func (cfg *frozenConfig) getEncoderFromCache(cacheKey reflect.Type) ValEncoder {
  38. encoder, found := cfg.encoderCache.Load(cacheKey)
  39. if found {
  40. return encoder.(ValEncoder)
  41. }
  42. return nil
  43. }
  44. var cfgCache = &sync.Map{}
  45. func getFrozenConfigFromCache(cfg Config) *frozenConfig {
  46. obj, found := cfgCache.Load(cfg)
  47. if found {
  48. return obj.(*frozenConfig)
  49. }
  50. return nil
  51. }
  52. func addFrozenConfigToCache(cfg Config, frozenConfig *frozenConfig) {
  53. cfgCache.Store(cfg, frozenConfig)
  54. }