forked from goccy/go-json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode_context.go
166 lines (141 loc) · 3.18 KB
/
encode_context.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
package json
import (
"bytes"
"sync"
"unsafe"
)
type mapItem struct {
key []byte
value []byte
}
type mapslice struct {
items []mapItem
}
func (m *mapslice) Len() int {
return len(m.items)
}
func (m *mapslice) Less(i, j int) bool {
return bytes.Compare(m.items[i].key, m.items[j].key) < 0
}
func (m *mapslice) Swap(i, j int) {
m.items[i], m.items[j] = m.items[j], m.items[i]
}
type encodeMapContext struct {
pos []int
slice *mapslice
buf []byte
}
var mapContextPool = sync.Pool{
New: func() interface{} {
return &encodeMapContext{}
},
}
func newMapContext(mapLen int) *encodeMapContext {
ctx := mapContextPool.Get().(*encodeMapContext)
if ctx.slice == nil {
ctx.slice = &mapslice{
items: make([]mapItem, 0, mapLen),
}
}
if cap(ctx.pos) < (mapLen*2 + 1) {
ctx.pos = make([]int, 0, mapLen*2+1)
ctx.slice.items = make([]mapItem, 0, mapLen)
} else {
ctx.pos = ctx.pos[:0]
ctx.slice.items = ctx.slice.items[:0]
}
ctx.buf = ctx.buf[:0]
return ctx
}
func releaseMapContext(c *encodeMapContext) {
mapContextPool.Put(c)
}
type encodeCompileContext struct {
typ *rtype
root bool
opcodeIndex int
ptrIndex int
indent int
structTypeToCompiledCode map[uintptr]*compiledCode
parent *encodeCompileContext
}
func (c *encodeCompileContext) context() *encodeCompileContext {
return &encodeCompileContext{
typ: c.typ,
root: c.root,
opcodeIndex: c.opcodeIndex,
ptrIndex: c.ptrIndex,
indent: c.indent,
structTypeToCompiledCode: c.structTypeToCompiledCode,
parent: c,
}
}
func (c *encodeCompileContext) withType(typ *rtype) *encodeCompileContext {
ctx := c.context()
ctx.typ = typ
return ctx
}
func (c *encodeCompileContext) incIndent() *encodeCompileContext {
ctx := c.context()
ctx.indent++
return ctx
}
func (c *encodeCompileContext) decIndent() *encodeCompileContext {
ctx := c.context()
ctx.indent--
return ctx
}
func (c *encodeCompileContext) incIndex() {
c.incOpcodeIndex()
c.incPtrIndex()
}
func (c *encodeCompileContext) decIndex() {
c.decOpcodeIndex()
c.decPtrIndex()
}
func (c *encodeCompileContext) incOpcodeIndex() {
c.opcodeIndex++
if c.parent != nil {
c.parent.incOpcodeIndex()
}
}
func (c *encodeCompileContext) decOpcodeIndex() {
c.opcodeIndex--
if c.parent != nil {
c.parent.decOpcodeIndex()
}
}
func (c *encodeCompileContext) incPtrIndex() {
c.ptrIndex++
if c.parent != nil {
c.parent.incPtrIndex()
}
}
func (c *encodeCompileContext) decPtrIndex() {
c.ptrIndex--
if c.parent != nil {
c.parent.decPtrIndex()
}
}
type encodeRuntimeContext struct {
buf []byte
ptrs []uintptr
keepRefs []unsafe.Pointer
seenPtr []uintptr
baseIndent int
prefix []byte
indentStr []byte
}
func (c *encodeRuntimeContext) init(p uintptr, codelen int) {
if len(c.ptrs) < codelen {
c.ptrs = make([]uintptr, codelen)
}
c.ptrs[0] = p
c.keepRefs = c.keepRefs[:0]
c.seenPtr = c.seenPtr[:0]
c.baseIndent = 0
}
func (c *encodeRuntimeContext) ptr() uintptr {
header := (*sliceHeader)(unsafe.Pointer(&c.ptrs))
return uintptr(header.data)
}