Skip to content
This repository was archived by the owner on Dec 15, 2025. It is now read-only.

Commit ee8cfb7

Browse files
committed
cache frozenConfig
1 parent 28452fc commit ee8cfb7

File tree

4 files changed

+68
-9
lines changed

4 files changed

+68
-9
lines changed

benchmarks/encode_string_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package test
2+
3+
import (
4+
"testing"
5+
"github.com/json-iterator/go"
6+
"bytes"
7+
)
8+
9+
func Benchmark_encode_string_with_SetEscapeHTML(b *testing.B) {
10+
type V struct {
11+
S string
12+
B bool
13+
I int
14+
}
15+
var json = jsoniter.ConfigCompatibleWithStandardLibrary
16+
b.ReportAllocs()
17+
for i := 0; i < b.N; i++ {
18+
buf := &bytes.Buffer{}
19+
enc := json.NewEncoder(buf)
20+
enc.SetEscapeHTML(true)
21+
if err := enc.Encode(V{S: "s", B: true, I: 233}); err != nil {
22+
b.Fatal(err)
23+
}
24+
}
25+
}

feature_config.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,30 +60,34 @@ var ConfigFastest = Config{
6060

6161
// Froze forge API from config
6262
func (cfg Config) Froze() API {
63-
// TODO: cache frozen config
64-
frozenConfig := &frozenConfig{
63+
api := getFrozenConfigFromCache(cfg)
64+
if api != nil {
65+
return api
66+
}
67+
api = &frozenConfig{
6568
sortMapKeys: cfg.SortMapKeys,
6669
indentionStep: cfg.IndentionStep,
6770
objectFieldMustBeSimpleString: cfg.ObjectFieldMustBeSimpleString,
6871
onlyTaggedField: cfg.OnlyTaggedField,
6972
streamPool: make(chan *Stream, 16),
7073
iteratorPool: make(chan *Iterator, 16),
7174
}
72-
frozenConfig.initCache()
75+
api.initCache()
7376
if cfg.MarshalFloatWith6Digits {
74-
frozenConfig.marshalFloatWith6Digits()
77+
api.marshalFloatWith6Digits()
7578
}
7679
if cfg.EscapeHTML {
77-
frozenConfig.escapeHTML()
80+
api.escapeHTML()
7881
}
7982
if cfg.UseNumber {
80-
frozenConfig.useNumber()
83+
api.useNumber()
8184
}
8285
if cfg.ValidateJsonRawMessage {
83-
frozenConfig.validateJsonRawMessage()
86+
api.validateJsonRawMessage()
8487
}
85-
frozenConfig.configBeforeFrozen = cfg
86-
return frozenConfig
88+
api.configBeforeFrozen = cfg
89+
addFrozenConfigToCache(cfg, api)
90+
return api
8791
}
8892

8993
func (cfg *frozenConfig) validateJsonRawMessage() {

feature_config_with_sync_map.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,18 @@ func (cfg *frozenConfig) getEncoderFromCache(cacheKey reflect.Type) ValEncoder {
4848
return encoder.(ValEncoder)
4949
}
5050
return nil
51+
}
52+
53+
var cfgCache = &sync.Map{}
54+
55+
func getFrozenConfigFromCache(cfg Config) *frozenConfig {
56+
obj, found := cfgCache.Load(cfg)
57+
if found {
58+
return obj.(*frozenConfig)
59+
}
60+
return nil
61+
}
62+
63+
func addFrozenConfigToCache(cfg Config, frozenConfig *frozenConfig) {
64+
cfgCache.Store(cfg, frozenConfig)
5165
}

feature_config_without_sync_map.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,19 @@ func (cfg *frozenConfig) getEncoderFromCache(cacheKey reflect.Type) ValEncoder {
5252
cfg.cacheLock.RUnlock()
5353
return encoder
5454
}
55+
56+
var cfgCacheLock = &sync.RWMutex{}
57+
var cfgCache = map[Config]*frozenConfig{}
58+
59+
func getFrozenConfigFromCache(cfg Config) *frozenConfig {
60+
cfgCacheLock.RLock()
61+
frozenConfig := cfgCache[cfg]
62+
cfgCacheLock.RUnlock()
63+
return frozenConfig
64+
}
65+
66+
func addFrozenConfigToCache(cfg Config, frozenConfig *frozenConfig) {
67+
cfgCacheLock.Lock()
68+
cfgCache[cfg] = frozenConfig
69+
cfgCacheLock.Unlock()
70+
}

0 commit comments

Comments
 (0)