/
syncpool.go
185 lines (156 loc) · 4.28 KB
/
syncpool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// nolint
// Copyright 20xx The Alipay Authors.
//
// @authors[0]: bingwu.ybw(bingwu.ybw@antfin.com|detailyang@gmail.com)
// @authors[1]: robotx(robotx@antfin.com)
//
// *Legal Disclaimer*
// Within this source code, the comments in Chinese shall be the original, governing version. Any comment in other languages are for reference only. In the event of any conflict between the Chinese language version comments and other language version comments, the Chinese language version shall prevail.
// *法律免责声明*
// 关于代码注释部分,中文注释为官方版本,其它语言注释仅做参考。中文注释可能与其它语言注释存在不一致,当中文注释与其它语言注释存在不一致时,请以中文注释为准。
//
//
package sofahessian
import (
"bufio"
"bytes"
"io"
"sync"
)
var (
bbrPool sync.Pool
mStringInterfacePool = sync.Pool{New: func() interface{} {
m := make(map[string]interface{}, 8)
return &m
}}
hessianEncodeCtxPool = sync.Pool{
New: func() interface{} {
return NewEncodeContext()
},
}
hessianDecodeCtxPool = sync.Pool{
New: func() interface{} {
return NewDecodeContext()
},
}
hessianEncoderPool = sync.Pool{
New: func() interface{} {
return NewEncoder(NewEncodeContext())
},
}
hessianDecoderPool = sync.Pool{
New: func() interface{} {
return NewDecoder(NewDecodeContext(), nil)
},
}
jsonctxPool = sync.Pool{
New: func() interface{} {
return NewJSONEncodeContext()
},
}
)
func acquireMStringInterface() *map[string]interface{} {
return mStringInterfacePool.Get().(*map[string]interface{})
}
func releaseMStringInterface(m *map[string]interface{}) {
for i := range *m {
delete(*m, i)
}
mStringInterfacePool.Put(m)
}
var utf8HelperPool = sync.Pool{
New: func() interface{} {
return &utf8Helper{}
},
}
func acquireUTF8Helper(s string) *utf8Helper {
return utf8HelperPool.Get().(*utf8Helper)
}
func releaseUTF8Helper(h *utf8Helper) {
h.Reset()
utf8HelperPool.Put(h)
}
var u8Pool = sync.Pool{
New: func() interface{} {
var p [8]byte
return &p
},
}
func acquireU8() *[8]byte {
return u8Pool.Get().(*[8]byte)
}
func releaseU8(p *[8]byte) {
u8Pool.Put(p)
}
// AcquireHessianDecodeContext acquires decode context from sync.pool.
func AcquireHessianDecodeContext() *DecodeContext {
return hessianDecodeCtxPool.Get().(*DecodeContext)
}
// ReleaseHessianDecodeContext releases decode context to sync.pool.
func ReleaseHessianDecodeContext(hd *DecodeContext) {
hd.Reset()
hessianDecodeCtxPool.Put(hd)
}
// AcquireHessianEncodeContext acquires encode context from sync.pool.
func AcquireHessianEncodeContext() *EncodeContext {
return hessianEncodeCtxPool.Get().(*EncodeContext)
}
// ReleaseHessianEncodeContext releases encode context to sync.pool.
func ReleaseHessianEncodeContext(hc *EncodeContext) {
hc.Reset()
hessianEncodeCtxPool.Put(hc)
}
// AcquireHessianDecoder acquires decoder from sync.pool.
func AcquireHessianDecoder(ctx *DecodeContext, reader io.Reader) *Decoder {
hd, ok := hessianDecoderPool.Get().(*Decoder)
if !ok {
panic("failed to type casting")
}
hd.ResetWithContext(ctx, reader)
return hd
}
// ReleaseHessianDecoder releases decoder to sync.pool.
func ReleaseHessianDecoder(hd *Decoder) {
hessianDecoderPool.Put(hd)
}
// AcquireHessianEncoder acquires encoder from sync.pool.
func AcquireHessianEncoder(ctx *EncodeContext) *Encoder {
he, ok := hessianEncoderPool.Get().(*Encoder)
if !ok {
panic("failed to type casting")
}
he.ResetWithContext(ctx)
return he
}
// ReleaseHessianEncoder releases decoder to sync.pool.
func ReleaseHessianEncoder(he *Encoder) {
hessianEncoderPool.Put(he)
}
// AcquireJSONContext acquires json context from sync.pool.
func AcquireJSONContext() *JSONEncodeContext {
return jsonctxPool.Get().(*JSONEncodeContext)
}
// ReleaseJSONContext releases json context to sync.pool.
func ReleaseJSONContext(jctx *JSONEncodeContext) {
jctx.Reset()
jsonctxPool.Put(jctx)
}
func AcquireBytesBufioReader(b []byte) *BytesBufioReader {
i := bbrPool.Get()
if i == nil {
bbr := &BytesBufioReader{
r: bytes.NewReader(b),
}
bbr.br = bufio.NewReaderSize(bbr.r, 8192)
return bbr
}
bbr, ok := i.(*BytesBufioReader)
if !ok {
panic("failed to type casting")
}
bbr.Reset(b)
return bbr
}
func ReleaseBytesBufioReader(bbr *BytesBufioReader) {
bbrPool.Put(bbr)
}