-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder_scratch.go
More file actions
243 lines (227 loc) · 6.52 KB
/
Copy pathdecoder_scratch.go
File metadata and controls
243 lines (227 loc) · 6.52 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package vibejson
import (
"reflect"
"sync/atomic"
)
const (
decoderPlanStateCacheSlots = 4
decoderMapScratchRetentionBytes = 64 << 10
)
// decoderMapScratch owns the addressable reflection boxes for one compiled map
// or inline catch-all. SetMapIndex copies both boxes into the destination map.
// The boxes are cleared before reuse so cached scratch retains no decoded
// object graph.
type decoderMapScratch struct {
key reflect.Value
element reflect.Value
entries int
inUse bool
}
type decoderWideSeenScratch struct {
words []uint64
inUse bool
}
// decoderPlanState is shared by copies of one immutable Decoder. Its bounded
// cache holds only operation metadata and cleared reflection boxes; detached
// standard-method receiver arrays are always operation-local and never enter
// the cache.
type decoderPlanState struct {
mapSlots int
cache [decoderPlanStateCacheSlots]atomic.Pointer[decoderState]
}
func newDecoderPlanState(mapSlots int, receivers bool) *decoderPlanState {
if mapSlots == 0 && !receivers {
return nil
}
return &decoderPlanState{mapSlots: mapSlots}
}
func (p *decoderPlanState) take() *decoderState {
for i := range p.cache {
if state := p.cache[i].Swap(nil); state != nil {
return state
}
}
operation := &decoderOperationState{}
if p.mapSlots != 0 {
operation.maps = make([]decoderMapScratch, p.mapSlots)
}
return &decoderState{operation: operation}
}
func (p *decoderPlanState) release(state *decoderState) {
state.resetOperationState()
state.structural = decoderStructuralTape{}
state.structuralActive = false
for i := range p.cache {
if p.cache[i].CompareAndSwap(nil, state) {
return
}
}
}
func (s *decoderState) resetOperationState() {
s.replaceDestination = nil
s.replaceSpan = 0
operation := s.operation
if operation == nil {
return
}
operation.resetReceivers()
for i := range operation.maps {
scratch := &operation.maps[i]
if scratch.key.IsValid() {
scratch.key.SetZero()
}
if scratch.element.IsValid() {
scratch.element.SetZero()
}
scratch.inUse = false
scratch.entries = 0
}
for i := range operation.wideSeen {
operation.wideSeen[i].inUse = false
}
}
func (c *decoderCursor) takeWideSeen(fieldCount int) ([]uint64, int) {
wordCount := (fieldCount + 63) / 64
if c.state == nil || c.state.operation == nil {
return make([]uint64, wordCount), -1
}
operation := c.state.operation
for index := range operation.wideSeen {
scratch := &operation.wideSeen[index]
if scratch.inUse {
continue
}
if cap(scratch.words) < wordCount {
scratch.words = make([]uint64, wordCount)
} else {
scratch.words = scratch.words[:wordCount]
clear(scratch.words)
}
scratch.inUse = true
return scratch.words, index
}
operation.wideSeen = append(operation.wideSeen, decoderWideSeenScratch{
words: make([]uint64, wordCount),
inUse: true,
})
return operation.wideSeen[len(operation.wideSeen)-1].words, len(operation.wideSeen) - 1
}
func (c *decoderCursor) releaseWideSeen(index int) {
if index >= 0 {
c.state.operation.wideSeen[index].inUse = false
}
}
func (c *decoderCursor) takeMapScratch(node *typedNode) *decoderMapScratch {
if node.decMapScratch == 0 || c.state == nil || c.state.operation == nil {
return nil
}
index := int(node.decMapScratch - 1)
if index >= len(c.state.operation.maps) {
return nil
}
scratch := &c.state.operation.maps[index]
if scratch.inUse {
return nil
}
if !scratch.element.IsValid() {
element := reflect.New(node.elem.typ)
scratch.element = element.Elem()
scratch.key = reflect.New(node.typ.Key()).Elem()
}
scratch.inUse = true
return scratch
}
func releaseMapScratch(scratch *decoderMapScratch) {
if scratch == nil {
return
}
scratch.key.SetZero()
scratch.element.SetZero()
scratch.inUse = false
}
func releaseInlineMapScratch(scratch *decoderMapScratch) {
if scratch == nil {
return
}
scratch.entries = 0
releaseMapScratch(scratch)
}
// prepareDecoderMapScratch assigns fixed operation-state slots only to maps
// and inline catch-alls whose boxes cannot be observed by user code. Text key
// methods, native value hooks, and dynamic interfaces retain the ordinary
// one-call allocation path. The cumulative shallow box size bounds every
// cached state.
func prepareDecoderMapScratch(root *typedNode) int {
seen := make(map[*typedNode]bool)
usedBytes := uintptr(0)
scratchBytes := reflect.TypeFor[decoderMapScratch]().Size()
slots := 0
var visit func(*typedNode)
visit = func(node *typedNode) {
if node == nil || seen[node] {
return
}
seen[node] = true
assign := func(mapType reflect.Type, elem *typedNode, textKey bool, slot *uint32) {
if textKey || !decoderMapScratchSafe(elem, make(map[*typedNode]bool)) {
return
}
boxBytes := scratchBytes + mapType.Key().Size() + elem.typ.Size()
if usedBytes <= decoderMapScratchRetentionBytes && boxBytes <= decoderMapScratchRetentionBytes-usedBytes {
slots++
*slot = uint32(slots)
usedBytes += boxBytes
}
}
if node.kind == typedMap || node.kind == typedMapReplace {
assign(node.typ, node.elem, node.mapKeyTextDecode, &node.decMapScratch)
}
switch node.kind {
case typedStruct:
for i := range node.fields {
visit(node.fields[i].node)
}
if node.inlineMap != nil {
assign(node.inlineMap.mapType, node.inlineMap.elem, false, &node.inlineMap.decMapScratch)
visit(node.inlineMap.elem)
}
case typedSlice, typedArray, typedMap, typedPointer,
typedPointerReplace, typedSliceReplace, typedMapReplace, typedBytesReplace:
visit(node.elem)
}
}
visit(root)
return slots
}
func decoderMapScratchSafe(node *typedNode, visiting map[*typedNode]bool) bool {
if node == nil {
return true
}
if visiting[node] {
return true
}
visiting[node] = true
defer delete(visiting, node)
switch node.kind {
case typedBool, typedString, typedNumber, typedInt, typedUint, typedFloat, typedBytes:
return true
case typedUnmarshalerJSON, typedUnmarshalerText:
// Standard methods run on detached shadows. A retained receiver cannot
// observe the element box copied into the destination map.
return true
case typedStruct:
for i := range node.fields {
if !decoderMapScratchSafe(node.fields[i].node, visiting) {
return false
}
}
return node.inlineMap == nil || decoderMapScratchSafe(node.inlineMap.elem, visiting)
case typedSlice, typedArray, typedPointer,
typedPointerReplace, typedSliceReplace, typedBytesReplace:
return decoderMapScratchSafe(node.elem, visiting)
case typedMap, typedMapReplace:
return !node.mapKeyTextDecode && decoderMapScratchSafe(node.elem, visiting)
default:
return false
}
}