mirror of
https://github.com/json-iterator/go.git
synced 2025-02-19 19:59:49 +02:00
cache frozenConfig
This commit is contained in:
parent
28452fcdec
commit
ee8cfb7547
25
benchmarks/encode_string_test.go
Normal file
25
benchmarks/encode_string_test.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"github.com/json-iterator/go"
|
||||||
|
"bytes"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Benchmark_encode_string_with_SetEscapeHTML(b *testing.B) {
|
||||||
|
type V struct {
|
||||||
|
S string
|
||||||
|
B bool
|
||||||
|
I int
|
||||||
|
}
|
||||||
|
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||||
|
b.ReportAllocs()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
enc := json.NewEncoder(buf)
|
||||||
|
enc.SetEscapeHTML(true)
|
||||||
|
if err := enc.Encode(V{S: "s", B: true, I: 233}); err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -60,8 +60,11 @@ var ConfigFastest = Config{
|
|||||||
|
|
||||||
// Froze forge API from config
|
// Froze forge API from config
|
||||||
func (cfg Config) Froze() API {
|
func (cfg Config) Froze() API {
|
||||||
// TODO: cache frozen config
|
api := getFrozenConfigFromCache(cfg)
|
||||||
frozenConfig := &frozenConfig{
|
if api != nil {
|
||||||
|
return api
|
||||||
|
}
|
||||||
|
api = &frozenConfig{
|
||||||
sortMapKeys: cfg.SortMapKeys,
|
sortMapKeys: cfg.SortMapKeys,
|
||||||
indentionStep: cfg.IndentionStep,
|
indentionStep: cfg.IndentionStep,
|
||||||
objectFieldMustBeSimpleString: cfg.ObjectFieldMustBeSimpleString,
|
objectFieldMustBeSimpleString: cfg.ObjectFieldMustBeSimpleString,
|
||||||
@ -69,21 +72,22 @@ func (cfg Config) Froze() API {
|
|||||||
streamPool: make(chan *Stream, 16),
|
streamPool: make(chan *Stream, 16),
|
||||||
iteratorPool: make(chan *Iterator, 16),
|
iteratorPool: make(chan *Iterator, 16),
|
||||||
}
|
}
|
||||||
frozenConfig.initCache()
|
api.initCache()
|
||||||
if cfg.MarshalFloatWith6Digits {
|
if cfg.MarshalFloatWith6Digits {
|
||||||
frozenConfig.marshalFloatWith6Digits()
|
api.marshalFloatWith6Digits()
|
||||||
}
|
}
|
||||||
if cfg.EscapeHTML {
|
if cfg.EscapeHTML {
|
||||||
frozenConfig.escapeHTML()
|
api.escapeHTML()
|
||||||
}
|
}
|
||||||
if cfg.UseNumber {
|
if cfg.UseNumber {
|
||||||
frozenConfig.useNumber()
|
api.useNumber()
|
||||||
}
|
}
|
||||||
if cfg.ValidateJsonRawMessage {
|
if cfg.ValidateJsonRawMessage {
|
||||||
frozenConfig.validateJsonRawMessage()
|
api.validateJsonRawMessage()
|
||||||
}
|
}
|
||||||
frozenConfig.configBeforeFrozen = cfg
|
api.configBeforeFrozen = cfg
|
||||||
return frozenConfig
|
addFrozenConfigToCache(cfg, api)
|
||||||
|
return api
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cfg *frozenConfig) validateJsonRawMessage() {
|
func (cfg *frozenConfig) validateJsonRawMessage() {
|
||||||
|
@ -49,3 +49,17 @@ func (cfg *frozenConfig) getEncoderFromCache(cacheKey reflect.Type) ValEncoder {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var cfgCache = &sync.Map{}
|
||||||
|
|
||||||
|
func getFrozenConfigFromCache(cfg Config) *frozenConfig {
|
||||||
|
obj, found := cfgCache.Load(cfg)
|
||||||
|
if found {
|
||||||
|
return obj.(*frozenConfig)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func addFrozenConfigToCache(cfg Config, frozenConfig *frozenConfig) {
|
||||||
|
cfgCache.Store(cfg, frozenConfig)
|
||||||
|
}
|
@ -52,3 +52,19 @@ func (cfg *frozenConfig) getEncoderFromCache(cacheKey reflect.Type) ValEncoder {
|
|||||||
cfg.cacheLock.RUnlock()
|
cfg.cacheLock.RUnlock()
|
||||||
return encoder
|
return encoder
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var cfgCacheLock = &sync.RWMutex{}
|
||||||
|
var cfgCache = map[Config]*frozenConfig{}
|
||||||
|
|
||||||
|
func getFrozenConfigFromCache(cfg Config) *frozenConfig {
|
||||||
|
cfgCacheLock.RLock()
|
||||||
|
frozenConfig := cfgCache[cfg]
|
||||||
|
cfgCacheLock.RUnlock()
|
||||||
|
return frozenConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
func addFrozenConfigToCache(cfg Config, frozenConfig *frozenConfig) {
|
||||||
|
cfgCacheLock.Lock()
|
||||||
|
cfgCache[cfg] = frozenConfig
|
||||||
|
cfgCacheLock.Unlock()
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user