Skip to content

Commit 3353055

Browse files
committed
use concurrent.Map for 1.8 support
1 parent 455b3f8 commit 3353055

File tree

3 files changed

+59
-134
lines changed

3 files changed

+59
-134
lines changed

config.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"sync"
88
"unsafe"
9+
"github.com/modern-go/concurrent"
910
)
1011

1112
// Config customize how the API should behave.
@@ -59,6 +60,64 @@ var ConfigFastest = Config{
5960
ObjectFieldMustBeSimpleString: true, // do not unescape object field
6061
}.Froze()
6162

63+
64+
type frozenConfig struct {
65+
configBeforeFrozen Config
66+
sortMapKeys bool
67+
indentionStep int
68+
objectFieldMustBeSimpleString bool
69+
onlyTaggedField bool
70+
disallowUnknownFields bool
71+
decoderCache *concurrent.Map
72+
encoderCache *concurrent.Map
73+
extensions []Extension
74+
streamPool *sync.Pool
75+
iteratorPool *sync.Pool
76+
}
77+
78+
func (cfg *frozenConfig) initCache() {
79+
cfg.decoderCache = concurrent.NewMap()
80+
cfg.encoderCache = concurrent.NewMap()
81+
}
82+
83+
func (cfg *frozenConfig) addDecoderToCache(cacheKey uintptr, decoder ValDecoder) {
84+
cfg.decoderCache.Store(cacheKey, decoder)
85+
}
86+
87+
func (cfg *frozenConfig) addEncoderToCache(cacheKey uintptr, encoder ValEncoder) {
88+
cfg.encoderCache.Store(cacheKey, encoder)
89+
}
90+
91+
func (cfg *frozenConfig) getDecoderFromCache(cacheKey uintptr) ValDecoder {
92+
decoder, found := cfg.decoderCache.Load(cacheKey)
93+
if found {
94+
return decoder.(ValDecoder)
95+
}
96+
return nil
97+
}
98+
99+
func (cfg *frozenConfig) getEncoderFromCache(cacheKey uintptr) ValEncoder {
100+
encoder, found := cfg.encoderCache.Load(cacheKey)
101+
if found {
102+
return encoder.(ValEncoder)
103+
}
104+
return nil
105+
}
106+
107+
var cfgCache = &sync.Map{}
108+
109+
func getFrozenConfigFromCache(cfg Config) *frozenConfig {
110+
obj, found := cfgCache.Load(cfg)
111+
if found {
112+
return obj.(*frozenConfig)
113+
}
114+
return nil
115+
}
116+
117+
func addFrozenConfigToCache(cfg Config, frozenConfig *frozenConfig) {
118+
cfgCache.Store(cfg, frozenConfig)
119+
}
120+
62121
// Froze forge API from config
63122
func (cfg Config) Froze() API {
64123
api := &frozenConfig{

config_with_sync_map.go

Lines changed: 0 additions & 64 deletions
This file was deleted.

config_without_sync_map.go

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
 (0)