@@ -89,14 +89,37 @@ func (u *unmarshalGen) tuple(s *Struct) {
89
89
u .p .printf ("\n if msgp.IsNil(bts) {\n bts = bts[1:]\n %s = nil\n } else {" , fieldElem .Varname ())
90
90
}
91
91
SetIsAllowNil (fieldElem , anField )
92
+ if s .Fields [i ].HasTagPart ("zerocopy" ) {
93
+ setRecursiveZC (fieldElem , true )
94
+ }
92
95
next (u , fieldElem )
96
+ if s .Fields [i ].HasTagPart ("zerocopy" ) {
97
+ setRecursiveZC (fieldElem , false )
98
+ }
99
+
93
100
u .ctx .Pop ()
94
101
if anField {
95
102
u .p .printf ("\n }" )
96
103
}
97
104
}
98
105
}
99
106
107
+ // setRecursiveZC will alloc zerocopy for byte fields that are present.
108
+ func setRecursiveZC (e Elem , enable bool ) {
109
+ if base , ok := e .(* BaseElem ); ok {
110
+ base .zerocopy = enable
111
+ }
112
+ if el , ok := e .(* Slice ); ok {
113
+ setRecursiveZC (el .Els , enable )
114
+ }
115
+ if el , ok := e .(* Array ); ok {
116
+ setRecursiveZC (el .Els , enable )
117
+ }
118
+ if el , ok := e .(* Map ); ok {
119
+ setRecursiveZC (el .Value , enable )
120
+ }
121
+ }
122
+
100
123
func (u * unmarshalGen ) mapstruct (s * Struct ) {
101
124
u .needsField ()
102
125
sz := randIdent ()
@@ -136,7 +159,13 @@ func (u *unmarshalGen) mapstruct(s *Struct) {
136
159
u .p .printf ("\n if msgp.IsNil(bts) {\n bts = bts[1:]\n %s = nil\n } else {" , fieldElem .Varname ())
137
160
}
138
161
SetIsAllowNil (fieldElem , anField )
162
+ if s .Fields [i ].HasTagPart ("zerocopy" ) {
163
+ setRecursiveZC (fieldElem , true )
164
+ }
139
165
next (u , fieldElem )
166
+ if s .Fields [i ].HasTagPart ("zerocopy" ) {
167
+ setRecursiveZC (fieldElem , false )
168
+ }
140
169
u .ctx .Pop ()
141
170
if oeCount > 0 && (s .Fields [i ].HasTagPart ("omitempty" ) || s .Fields [i ].HasTagPart ("omitzero" )) {
142
171
u .p .printf ("\n %s" , bm .setStmt (len (oeEmittedIdx )))
@@ -188,7 +217,11 @@ func (u *unmarshalGen) gBase(b *BaseElem) {
188
217
189
218
switch b .Value {
190
219
case Bytes :
191
- u .p .printf ("\n %s, bts, err = msgp.ReadBytesBytes(bts, %s)" , refname , lowered )
220
+ if b .zerocopy {
221
+ u .p .printf ("\n %s, bts, err = msgp.ReadBytesZC(bts)" , refname )
222
+ } else {
223
+ u .p .printf ("\n %s, bts, err = msgp.ReadBytesBytes(bts, %s)" , refname , lowered )
224
+ }
192
225
case Ext :
193
226
u .p .printf ("\n bts, err = msgp.ReadExtensionBytes(bts, %s)" , lowered )
194
227
case IDENT :
0 commit comments