1
2
3
4 package codec
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 import (
193 "encoding"
194 "encoding/binary"
195 "errors"
196 "fmt"
197 "io"
198 "math"
199 "reflect"
200 "runtime"
201 "sort"
202 "strconv"
203 "strings"
204 "sync"
205 "sync/atomic"
206 "time"
207 "unicode/utf8"
208 )
209
210
211
212
213
214
215
216
217
218
219 const debugging = false
220
221 const (
222
223
224
225 containerLenUnknown = -1
226
227
228
229 containerLenNil = math.MinInt32
230
231
232
233
234
235
236
237 handleBytesWithinKArray = true
238
239
240
241 supportMarshalInterfaces = true
242
243
244 bytesFreeListNoCache = false
245
246
247
248 cacheLineSize = 64
249
250 wordSizeBits = 32 << (^uint(0) >> 63)
251 wordSize = wordSizeBits / 8
252
253
254
255
256
257 skipFastpathTypeSwitchInDirectCall = false
258 )
259
260 const cpu32Bit = ^uint(0)>>32 == 0
261
262 type rkind byte
263
264 const (
265 rkindPtr = rkind(reflect.Ptr)
266 rkindString = rkind(reflect.String)
267 rkindChan = rkind(reflect.Chan)
268 )
269
270 type mapKeyFastKind uint8
271
272 const (
273 mapKeyFastKind32 = iota + 1
274 mapKeyFastKind32ptr
275 mapKeyFastKind64
276 mapKeyFastKind64ptr
277 mapKeyFastKindStr
278 )
279
280 var (
281
282
283
284 handleInitMu sync.Mutex
285
286 must mustHdl
287 halt panicHdl
288
289 digitCharBitset bitset256
290 numCharBitset bitset256
291 whitespaceCharBitset bitset256
292 asciiAlphaNumBitset bitset256
293
294
295
296
297
298
299
300
301
302 refBitset bitset32
303
304
305 isnilBitset bitset32
306
307
308 numBoolBitset bitset32
309
310
311 numBoolStrSliceBitset bitset32
312
313
314 scalarBitset bitset32
315
316 mapKeyFastKindVals [32]mapKeyFastKind
317
318
319 codecgen bool
320
321 oneByteArr [1]byte
322 zeroByteSlice = oneByteArr[:0:0]
323
324 eofReader devNullReader
325 )
326
327 var (
328 errMapTypeNotMapKind = errors.New("MapType MUST be of Map Kind")
329 errSliceTypeNotSliceKind = errors.New("SliceType MUST be of Slice Kind")
330
331 errExtFnWriteExtUnsupported = errors.New("BytesExt.WriteExt is not supported")
332 errExtFnReadExtUnsupported = errors.New("BytesExt.ReadExt is not supported")
333 errExtFnConvertExtUnsupported = errors.New("InterfaceExt.ConvertExt is not supported")
334 errExtFnUpdateExtUnsupported = errors.New("InterfaceExt.UpdateExt is not supported")
335
336 errPanicUndefined = errors.New("panic: undefined error")
337
338 errHandleInited = errors.New("cannot modify initialized Handle")
339
340 errNoFormatHandle = errors.New("no handle (cannot identify format)")
341 )
342
343 var pool4tiload = sync.Pool{
344 New: func() interface{} {
345 return &typeInfoLoad{
346 etypes: make([]uintptr, 0, 4),
347 sfis: make([]structFieldInfo, 0, 4),
348 sfiNames: make(map[string]uint16, 4),
349 }
350 },
351 }
352
353 func init() {
354 xx := func(f mapKeyFastKind, k ...reflect.Kind) {
355 for _, v := range k {
356 mapKeyFastKindVals[byte(v)&31] = f
357 }
358 }
359
360 var f mapKeyFastKind
361
362 f = mapKeyFastKind64
363 if wordSizeBits == 32 {
364 f = mapKeyFastKind32
365 }
366 xx(f, reflect.Int, reflect.Uint, reflect.Uintptr)
367
368 f = mapKeyFastKind64ptr
369 if wordSizeBits == 32 {
370 f = mapKeyFastKind32ptr
371 }
372 xx(f, reflect.Ptr)
373
374 xx(mapKeyFastKindStr, reflect.String)
375 xx(mapKeyFastKind32, reflect.Uint32, reflect.Int32, reflect.Float32)
376 xx(mapKeyFastKind64, reflect.Uint64, reflect.Int64, reflect.Float64)
377
378 numBoolBitset.
379 set(byte(reflect.Bool)).
380 set(byte(reflect.Int)).
381 set(byte(reflect.Int8)).
382 set(byte(reflect.Int16)).
383 set(byte(reflect.Int32)).
384 set(byte(reflect.Int64)).
385 set(byte(reflect.Uint)).
386 set(byte(reflect.Uint8)).
387 set(byte(reflect.Uint16)).
388 set(byte(reflect.Uint32)).
389 set(byte(reflect.Uint64)).
390 set(byte(reflect.Uintptr)).
391 set(byte(reflect.Float32)).
392 set(byte(reflect.Float64)).
393 set(byte(reflect.Complex64)).
394 set(byte(reflect.Complex128))
395
396 numBoolStrSliceBitset = numBoolBitset
397
398 numBoolStrSliceBitset.
399 set(byte(reflect.String)).
400 set(byte(reflect.Slice))
401
402 scalarBitset = numBoolBitset
403
404 scalarBitset.
405 set(byte(reflect.String))
406
407
408
409 refBitset.
410 set(byte(reflect.Map)).
411 set(byte(reflect.Ptr)).
412 set(byte(reflect.Func)).
413 set(byte(reflect.Chan)).
414 set(byte(reflect.UnsafePointer))
415
416 isnilBitset = refBitset
417
418 isnilBitset.
419 set(byte(reflect.Interface)).
420 set(byte(reflect.Slice))
421
422
423
424
425
426
427 for i := byte(0); i <= utf8.RuneSelf; i++ {
428 if (i >= '0' && i <= '9') || (i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z') {
429 asciiAlphaNumBitset.set(i)
430 }
431 switch i {
432 case ' ', '\t', '\r', '\n':
433 whitespaceCharBitset.set(i)
434 case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
435 digitCharBitset.set(i)
436 numCharBitset.set(i)
437 case '.', '+', '-':
438 numCharBitset.set(i)
439 case 'e', 'E':
440 numCharBitset.set(i)
441 }
442 }
443 }
444
445
446
447
448
449 type driverStateManager interface {
450 resetState()
451 captureState() interface{}
452 restoreState(state interface{})
453 }
454
455 type bdAndBdread struct {
456 bdRead bool
457 bd byte
458 }
459
460 func (x bdAndBdread) captureState() interface{} { return x }
461 func (x *bdAndBdread) resetState() { x.bd, x.bdRead = 0, false }
462 func (x *bdAndBdread) reset() { x.resetState() }
463 func (x *bdAndBdread) restoreState(v interface{}) { *x = v.(bdAndBdread) }
464
465 type clsErr struct {
466 err error
467 closed bool
468 }
469
470 type charEncoding uint8
471
472 const (
473 _ charEncoding = iota
474 cUTF8
475 cUTF16LE
476 cUTF16BE
477 cUTF32LE
478 cUTF32BE
479
480 cRAW charEncoding = 255
481 )
482
483
484 type valueType uint8
485
486 const (
487 valueTypeUnset valueType = iota
488 valueTypeNil
489 valueTypeInt
490 valueTypeUint
491 valueTypeFloat
492 valueTypeBool
493 valueTypeString
494 valueTypeSymbol
495 valueTypeBytes
496 valueTypeMap
497 valueTypeArray
498 valueTypeTime
499 valueTypeExt
500
501
502 )
503
504 var valueTypeStrings = [...]string{
505 "Unset",
506 "Nil",
507 "Int",
508 "Uint",
509 "Float",
510 "Bool",
511 "String",
512 "Symbol",
513 "Bytes",
514 "Map",
515 "Array",
516 "Timestamp",
517 "Ext",
518 }
519
520 func (x valueType) String() string {
521 if int(x) < len(valueTypeStrings) {
522 return valueTypeStrings[x]
523 }
524 return strconv.FormatInt(int64(x), 10)
525 }
526
527
528
529 type containerState uint8
530
531 const (
532 _ containerState = iota
533
534 containerMapStart
535 containerMapKey
536 containerMapValue
537 containerMapEnd
538 containerArrayStart
539 containerArrayElem
540 containerArrayEnd
541 )
542
543
544
545
546
547 const rgetMaxRecursion = 2
548
549
550
551
552
553
554
555
556
557
558 type fauxUnion struct {
559
560
561
562 u uint64
563 i int64
564 f float64
565 l []byte
566 s string
567
568
569 t time.Time
570 b bool
571
572
573 v valueType
574 }
575
576
577 type typeInfoLoad struct {
578 etypes []uintptr
579 sfis []structFieldInfo
580 sfiNames map[string]uint16
581 }
582
583 func (x *typeInfoLoad) reset() {
584 x.etypes = x.etypes[:0]
585 x.sfis = x.sfis[:0]
586 for k := range x.sfiNames {
587 delete(x.sfiNames, k)
588 }
589 }
590
591
592
593
594 type jsonMarshaler interface {
595 MarshalJSON() ([]byte, error)
596 }
597 type jsonUnmarshaler interface {
598 UnmarshalJSON([]byte) error
599 }
600
601 type isZeroer interface {
602 IsZero() bool
603 }
604
605 type isCodecEmptyer interface {
606 IsCodecEmpty() bool
607 }
608
609 type codecError struct {
610 err error
611 name string
612 pos int
613 encode bool
614 }
615
616 func (e *codecError) Cause() error {
617 return e.err
618 }
619
620 func (e *codecError) Unwrap() error {
621 return e.err
622 }
623
624 func (e *codecError) Error() string {
625 if e.encode {
626 return fmt.Sprintf("%s encode error: %v", e.name, e.err)
627 }
628 return fmt.Sprintf("%s decode error [pos %d]: %v", e.name, e.pos, e.err)
629 }
630
631 func wrapCodecErr(in error, name string, numbytesread int, encode bool) (out error) {
632 x, ok := in.(*codecError)
633 if ok && x.pos == numbytesread && x.name == name && x.encode == encode {
634 return in
635 }
636 return &codecError{in, name, numbytesread, encode}
637 }
638
639 var (
640 bigen bigenHelper
641
642 bigenstd = binary.BigEndian
643
644 structInfoFieldName = "_struct"
645
646 mapStrIntfTyp = reflect.TypeOf(map[string]interface{}(nil))
647 mapIntfIntfTyp = reflect.TypeOf(map[interface{}]interface{}(nil))
648 intfSliceTyp = reflect.TypeOf([]interface{}(nil))
649 intfTyp = intfSliceTyp.Elem()
650
651 reflectValTyp = reflect.TypeOf((*reflect.Value)(nil)).Elem()
652
653 stringTyp = reflect.TypeOf("")
654 timeTyp = reflect.TypeOf(time.Time{})
655 rawExtTyp = reflect.TypeOf(RawExt{})
656 rawTyp = reflect.TypeOf(Raw{})
657 uintptrTyp = reflect.TypeOf(uintptr(0))
658 uint8Typ = reflect.TypeOf(uint8(0))
659 uint8SliceTyp = reflect.TypeOf([]uint8(nil))
660 uintTyp = reflect.TypeOf(uint(0))
661 intTyp = reflect.TypeOf(int(0))
662
663 mapBySliceTyp = reflect.TypeOf((*MapBySlice)(nil)).Elem()
664
665 binaryMarshalerTyp = reflect.TypeOf((*encoding.BinaryMarshaler)(nil)).Elem()
666 binaryUnmarshalerTyp = reflect.TypeOf((*encoding.BinaryUnmarshaler)(nil)).Elem()
667
668 textMarshalerTyp = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
669 textUnmarshalerTyp = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
670
671 jsonMarshalerTyp = reflect.TypeOf((*jsonMarshaler)(nil)).Elem()
672 jsonUnmarshalerTyp = reflect.TypeOf((*jsonUnmarshaler)(nil)).Elem()
673
674 selferTyp = reflect.TypeOf((*Selfer)(nil)).Elem()
675 missingFielderTyp = reflect.TypeOf((*MissingFielder)(nil)).Elem()
676 iszeroTyp = reflect.TypeOf((*isZeroer)(nil)).Elem()
677 isCodecEmptyerTyp = reflect.TypeOf((*isCodecEmptyer)(nil)).Elem()
678 isSelferViaCodecgenerTyp = reflect.TypeOf((*isSelferViaCodecgener)(nil)).Elem()
679
680 uint8TypId = rt2id(uint8Typ)
681 uint8SliceTypId = rt2id(uint8SliceTyp)
682 rawExtTypId = rt2id(rawExtTyp)
683 rawTypId = rt2id(rawTyp)
684 intfTypId = rt2id(intfTyp)
685 timeTypId = rt2id(timeTyp)
686 stringTypId = rt2id(stringTyp)
687
688 mapStrIntfTypId = rt2id(mapStrIntfTyp)
689 mapIntfIntfTypId = rt2id(mapIntfIntfTyp)
690 intfSliceTypId = rt2id(intfSliceTyp)
691
692
693 intBitsize = uint8(intTyp.Bits())
694 uintBitsize = uint8(uintTyp.Bits())
695
696
697 bsAll0xff = []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
698
699 chkOvf checkOverflow
700 )
701
702 var defTypeInfos = NewTypeInfos([]string{"codec", "json"})
703
704
705
706
707
708
709
710
711 var SelfExt = &extFailWrapper{}
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730 type Selfer interface {
731 CodecEncodeSelf(*Encoder)
732 CodecDecodeSelf(*Decoder)
733 }
734
735 type isSelferViaCodecgener interface {
736 codecSelferViaCodecgen()
737 }
738
739
740
741
742
743
744
745
746
747
748 type MissingFielder interface {
749
750
751
752 CodecMissingField(field []byte, value interface{}) bool
753
754
755
756
757 CodecMissingFields() map[string]interface{}
758 }
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781 type MapBySlice interface {
782 MapBySlice()
783 }
784
785
786
787
788
789
790
791
792 type basicHandleRuntimeState struct {
793
794
795 rtidFns atomicRtidFnSlice
796 rtidFnsNoExt atomicRtidFnSlice
797
798
799
800
801
802 extHandle
803
804 intf2impls
805
806 mu sync.Mutex
807
808 jsonHandle bool
809 binaryHandle bool
810
811
812
813
814 timeBuiltin bool
815 _ bool
816 }
817
818
819
820
821 type BasicHandle struct {
822
823
824
825
826
827
828 TypeInfos *TypeInfos
829
830 *basicHandleRuntimeState
831
832
833
834 DecodeOptions
835
836
837
838 EncodeOptions
839
840 RPCOptions
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857 TimeNotBuiltin bool
858
859
860
861
862
863 ExplicitRelease bool
864
865
866 inited uint32
867
868 }
869
870
871
872
873 func initHandle(hh Handle) {
874 x := hh.getBasicHandle()
875
876
877
878
879
880
881
882
883
884
885
886
887
888 if atomic.LoadUint32(&x.inited) == 0 {
889 x.initHandle(hh)
890 }
891 }
892
893 func (x *BasicHandle) basicInit() {
894 x.rtidFns.store(nil)
895 x.rtidFnsNoExt.store(nil)
896 x.timeBuiltin = !x.TimeNotBuiltin
897 }
898
899 func (x *BasicHandle) init() {}
900
901 func (x *BasicHandle) isInited() bool {
902 return atomic.LoadUint32(&x.inited) != 0
903 }
904
905
906 func (x *BasicHandle) clearInited() {
907 atomic.StoreUint32(&x.inited, 0)
908 }
909
910
911
912 func (x *basicHandleRuntimeState) TimeBuiltin() bool {
913 return x.timeBuiltin
914 }
915
916 func (x *basicHandleRuntimeState) isJs() bool {
917 return x.jsonHandle
918 }
919
920 func (x *basicHandleRuntimeState) isBe() bool {
921 return x.binaryHandle
922 }
923
924 func (x *basicHandleRuntimeState) setExt(rt reflect.Type, tag uint64, ext Ext) (err error) {
925 rk := rt.Kind()
926 for rk == reflect.Ptr {
927 rt = rt.Elem()
928 rk = rt.Kind()
929 }
930
931 if rt.PkgPath() == "" || rk == reflect.Interface {
932 return fmt.Errorf("codec.Handle.SetExt: Takes named type, not a pointer or interface: %v", rt)
933 }
934
935 rtid := rt2id(rt)
936
937
938
939 switch rtid {
940 case rawTypId, rawExtTypId:
941 return
942 case timeTypId:
943 if x.timeBuiltin {
944 return
945 }
946 }
947
948 for i := range x.extHandle {
949 v := &x.extHandle[i]
950 if v.rtid == rtid {
951 v.tag, v.ext = tag, ext
952 return
953 }
954 }
955 rtidptr := rt2id(reflect.PtrTo(rt))
956 x.extHandle = append(x.extHandle, extTypeTagFn{rtid, rtidptr, rt, tag, ext})
957 return
958 }
959
960
961
962
963
964 func (x *BasicHandle) initHandle(hh Handle) {
965 handleInitMu.Lock()
966 defer handleInitMu.Unlock()
967 if x.inited == 0 {
968 if x.basicHandleRuntimeState == nil {
969 x.basicHandleRuntimeState = new(basicHandleRuntimeState)
970 }
971 x.jsonHandle = hh.isJson()
972 x.binaryHandle = hh.isBinary()
973
974 if x.MapType != nil && x.MapType.Kind() != reflect.Map {
975 halt.onerror(errMapTypeNotMapKind)
976 }
977 if x.SliceType != nil && x.SliceType.Kind() != reflect.Slice {
978 halt.onerror(errSliceTypeNotSliceKind)
979 }
980 x.basicInit()
981 hh.init()
982 atomic.StoreUint32(&x.inited, 1)
983 }
984 }
985
986 func (x *BasicHandle) getBasicHandle() *BasicHandle {
987 return x
988 }
989
990 func (x *BasicHandle) typeInfos() *TypeInfos {
991 if x.TypeInfos != nil {
992 return x.TypeInfos
993 }
994 return defTypeInfos
995 }
996
997 func (x *BasicHandle) getTypeInfo(rtid uintptr, rt reflect.Type) (pti *typeInfo) {
998 return x.typeInfos().get(rtid, rt)
999 }
1000
1001 func findRtidFn(s []codecRtidFn, rtid uintptr) (i uint, fn *codecFn) {
1002
1003
1004
1005
1006 var h uint
1007 var j = uint(len(s))
1008 LOOP:
1009 if i < j {
1010 h = (i + j) >> 1
1011 if s[h].rtid < rtid {
1012 i = h + 1
1013 } else {
1014 j = h
1015 }
1016 goto LOOP
1017 }
1018 if i < uint(len(s)) && s[i].rtid == rtid {
1019 fn = s[i].fn
1020 }
1021 return
1022 }
1023
1024 func (x *BasicHandle) fn(rt reflect.Type) (fn *codecFn) {
1025 return x.fnVia(rt, x.typeInfos(), &x.rtidFns, x.CheckCircularRef, true)
1026 }
1027
1028 func (x *BasicHandle) fnNoExt(rt reflect.Type) (fn *codecFn) {
1029 return x.fnVia(rt, x.typeInfos(), &x.rtidFnsNoExt, x.CheckCircularRef, false)
1030 }
1031
1032 func (x *basicHandleRuntimeState) fnVia(rt reflect.Type, tinfos *TypeInfos, fs *atomicRtidFnSlice, checkCircularRef, checkExt bool) (fn *codecFn) {
1033 rtid := rt2id(rt)
1034 sp := fs.load()
1035 if sp != nil {
1036 if _, fn = findRtidFn(sp, rtid); fn != nil {
1037 return
1038 }
1039 }
1040
1041 fn = x.fnLoad(rt, rtid, tinfos, checkCircularRef, checkExt)
1042 x.mu.Lock()
1043 sp = fs.load()
1044
1045
1046 if sp == nil {
1047 sp = []codecRtidFn{{rtid, fn}}
1048 fs.store(sp)
1049 } else {
1050 idx, fn2 := findRtidFn(sp, rtid)
1051 if fn2 == nil {
1052 sp2 := make([]codecRtidFn, len(sp)+1)
1053 copy(sp2[idx+1:], sp[idx:])
1054 copy(sp2, sp[:idx])
1055 sp2[idx] = codecRtidFn{rtid, fn}
1056 fs.store(sp2)
1057 }
1058 }
1059 x.mu.Unlock()
1060 return
1061 }
1062
1063 func fnloadFastpathUnderlying(ti *typeInfo) (f *fastpathE, u reflect.Type) {
1064 var rtid uintptr
1065 var idx int
1066 rtid = rt2id(ti.fastpathUnderlying)
1067 idx = fastpathAvIndex(rtid)
1068 if idx == -1 {
1069 return
1070 }
1071 f = &fastpathAv[idx]
1072 if uint8(reflect.Array) == ti.kind {
1073 u = reflectArrayOf(ti.rt.Len(), ti.elem)
1074 } else {
1075 u = f.rt
1076 }
1077 return
1078 }
1079
1080 func (x *basicHandleRuntimeState) fnLoad(rt reflect.Type, rtid uintptr, tinfos *TypeInfos, checkCircularRef, checkExt bool) (fn *codecFn) {
1081 fn = new(codecFn)
1082 fi := &(fn.i)
1083 ti := tinfos.get(rtid, rt)
1084 fi.ti = ti
1085 rk := reflect.Kind(ti.kind)
1086
1087
1088
1089
1090
1091 fi.addrDf = true
1092
1093
1094 if rtid == timeTypId && x.timeBuiltin {
1095 fn.fe = (*Encoder).kTime
1096 fn.fd = (*Decoder).kTime
1097 } else if rtid == rawTypId {
1098 fn.fe = (*Encoder).raw
1099 fn.fd = (*Decoder).raw
1100 } else if rtid == rawExtTypId {
1101 fn.fe = (*Encoder).rawExt
1102 fn.fd = (*Decoder).rawExt
1103 fi.addrD = true
1104 fi.addrE = true
1105 } else if xfFn := x.getExt(rtid, checkExt); xfFn != nil {
1106 fi.xfTag, fi.xfFn = xfFn.tag, xfFn.ext
1107 fn.fe = (*Encoder).ext
1108 fn.fd = (*Decoder).ext
1109 fi.addrD = true
1110 if rk == reflect.Struct || rk == reflect.Array {
1111 fi.addrE = true
1112 }
1113 } else if (ti.flagSelfer || ti.flagSelferPtr) &&
1114 !(checkCircularRef && ti.flagSelferViaCodecgen && ti.kind == byte(reflect.Struct)) {
1115
1116 fn.fe = (*Encoder).selferMarshal
1117 fn.fd = (*Decoder).selferUnmarshal
1118 fi.addrD = ti.flagSelferPtr
1119 fi.addrE = ti.flagSelferPtr
1120 } else if supportMarshalInterfaces && x.isBe() &&
1121 (ti.flagBinaryMarshaler || ti.flagBinaryMarshalerPtr) &&
1122 (ti.flagBinaryUnmarshaler || ti.flagBinaryUnmarshalerPtr) {
1123 fn.fe = (*Encoder).binaryMarshal
1124 fn.fd = (*Decoder).binaryUnmarshal
1125 fi.addrD = ti.flagBinaryUnmarshalerPtr
1126 fi.addrE = ti.flagBinaryMarshalerPtr
1127 } else if supportMarshalInterfaces && !x.isBe() && x.isJs() &&
1128 (ti.flagJsonMarshaler || ti.flagJsonMarshalerPtr) &&
1129 (ti.flagJsonUnmarshaler || ti.flagJsonUnmarshalerPtr) {
1130
1131 fn.fe = (*Encoder).jsonMarshal
1132 fn.fd = (*Decoder).jsonUnmarshal
1133 fi.addrD = ti.flagJsonUnmarshalerPtr
1134 fi.addrE = ti.flagJsonMarshalerPtr
1135 } else if supportMarshalInterfaces && !x.isBe() &&
1136 (ti.flagTextMarshaler || ti.flagTextMarshalerPtr) &&
1137 (ti.flagTextUnmarshaler || ti.flagTextUnmarshalerPtr) {
1138 fn.fe = (*Encoder).textMarshal
1139 fn.fd = (*Decoder).textUnmarshal
1140 fi.addrD = ti.flagTextUnmarshalerPtr
1141 fi.addrE = ti.flagTextMarshalerPtr
1142 } else {
1143 if fastpathEnabled && (rk == reflect.Map || rk == reflect.Slice || rk == reflect.Array) {
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155 var rtid2 uintptr
1156 if !ti.flagHasPkgPath {
1157 rtid2 = rtid
1158 if rk == reflect.Array {
1159 rtid2 = rt2id(ti.key)
1160 }
1161 if idx := fastpathAvIndex(rtid2); idx != -1 {
1162 fn.fe = fastpathAv[idx].encfn
1163 fn.fd = fastpathAv[idx].decfn
1164 fi.addrD = true
1165 fi.addrDf = false
1166 if rk == reflect.Array {
1167 fi.addrD = false
1168 }
1169 }
1170 } else {
1171
1172 xfe, xrt := fnloadFastpathUnderlying(ti)
1173 if xfe != nil {
1174 xfnf := xfe.encfn
1175 xfnf2 := xfe.decfn
1176 if rk == reflect.Array {
1177 fi.addrD = false
1178 fn.fd = func(d *Decoder, xf *codecFnInfo, xrv reflect.Value) {
1179 xfnf2(d, xf, rvConvert(xrv, xrt))
1180 }
1181 } else {
1182 fi.addrD = true
1183 fi.addrDf = false
1184 xptr2rt := reflect.PtrTo(xrt)
1185 fn.fd = func(d *Decoder, xf *codecFnInfo, xrv reflect.Value) {
1186 if xrv.Kind() == reflect.Ptr {
1187 xfnf2(d, xf, rvConvert(xrv, xptr2rt))
1188 } else {
1189 xfnf2(d, xf, rvConvert(xrv, xrt))
1190 }
1191 }
1192 }
1193 fn.fe = func(e *Encoder, xf *codecFnInfo, xrv reflect.Value) {
1194 xfnf(e, xf, rvConvert(xrv, xrt))
1195 }
1196 }
1197 }
1198 }
1199 if fn.fe == nil && fn.fd == nil {
1200 switch rk {
1201 case reflect.Bool:
1202 fn.fe = (*Encoder).kBool
1203 fn.fd = (*Decoder).kBool
1204 case reflect.String:
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215 fn.fe = (*Encoder).kString
1216 fn.fd = (*Decoder).kString
1217 case reflect.Int:
1218 fn.fd = (*Decoder).kInt
1219 fn.fe = (*Encoder).kInt
1220 case reflect.Int8:
1221 fn.fe = (*Encoder).kInt8
1222 fn.fd = (*Decoder).kInt8
1223 case reflect.Int16:
1224 fn.fe = (*Encoder).kInt16
1225 fn.fd = (*Decoder).kInt16
1226 case reflect.Int32:
1227 fn.fe = (*Encoder).kInt32
1228 fn.fd = (*Decoder).kInt32
1229 case reflect.Int64:
1230 fn.fe = (*Encoder).kInt64
1231 fn.fd = (*Decoder).kInt64
1232 case reflect.Uint:
1233 fn.fd = (*Decoder).kUint
1234 fn.fe = (*Encoder).kUint
1235 case reflect.Uint8:
1236 fn.fe = (*Encoder).kUint8
1237 fn.fd = (*Decoder).kUint8
1238 case reflect.Uint16:
1239 fn.fe = (*Encoder).kUint16
1240 fn.fd = (*Decoder).kUint16
1241 case reflect.Uint32:
1242 fn.fe = (*Encoder).kUint32
1243 fn.fd = (*Decoder).kUint32
1244 case reflect.Uint64:
1245 fn.fe = (*Encoder).kUint64
1246 fn.fd = (*Decoder).kUint64
1247 case reflect.Uintptr:
1248 fn.fe = (*Encoder).kUintptr
1249 fn.fd = (*Decoder).kUintptr
1250 case reflect.Float32:
1251 fn.fe = (*Encoder).kFloat32
1252 fn.fd = (*Decoder).kFloat32
1253 case reflect.Float64:
1254 fn.fe = (*Encoder).kFloat64
1255 fn.fd = (*Decoder).kFloat64
1256 case reflect.Complex64:
1257 fn.fe = (*Encoder).kComplex64
1258 fn.fd = (*Decoder).kComplex64
1259 case reflect.Complex128:
1260 fn.fe = (*Encoder).kComplex128
1261 fn.fd = (*Decoder).kComplex128
1262 case reflect.Chan:
1263 fn.fe = (*Encoder).kChan
1264 fn.fd = (*Decoder).kChan
1265 case reflect.Slice:
1266 fn.fe = (*Encoder).kSlice
1267 fn.fd = (*Decoder).kSlice
1268 case reflect.Array:
1269 fi.addrD = false
1270 fn.fe = (*Encoder).kArray
1271 fn.fd = (*Decoder).kArray
1272 case reflect.Struct:
1273 if ti.anyOmitEmpty ||
1274 ti.flagMissingFielder ||
1275 ti.flagMissingFielderPtr {
1276 fn.fe = (*Encoder).kStruct
1277 } else {
1278 fn.fe = (*Encoder).kStructNoOmitempty
1279 }
1280 fn.fd = (*Decoder).kStruct
1281 case reflect.Map:
1282 fn.fe = (*Encoder).kMap
1283 fn.fd = (*Decoder).kMap
1284 case reflect.Interface:
1285
1286 fn.fd = (*Decoder).kInterface
1287 fn.fe = (*Encoder).kErr
1288 default:
1289
1290 fn.fe = (*Encoder).kErr
1291 fn.fd = (*Decoder).kErr
1292 }
1293 }
1294 }
1295 return
1296 }
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313 type Handle interface {
1314 Name() string
1315 getBasicHandle() *BasicHandle
1316 newEncDriver() encDriver
1317 newDecDriver() decDriver
1318 isBinary() bool
1319 isJson() bool
1320
1321 desc(bd byte) string
1322
1323 init()
1324 }
1325
1326
1327
1328
1329
1330 type Raw []byte
1331
1332
1333
1334
1335
1336
1337
1338 type RawExt struct {
1339 Tag uint64
1340
1341
1342 Data []byte
1343
1344
1345
1346 Value interface{}
1347 }
1348
1349 func (re *RawExt) setData(xbs []byte, zerocopy bool) {
1350 if zerocopy {
1351 re.Data = xbs
1352 } else {
1353 re.Data = append(re.Data[:0], xbs...)
1354 }
1355 }
1356
1357
1358
1359 type BytesExt interface {
1360
1361
1362
1363 WriteExt(v interface{}) []byte
1364
1365
1366
1367
1368 ReadExt(dst interface{}, src []byte)
1369 }
1370
1371
1372
1373
1374
1375 type InterfaceExt interface {
1376
1377
1378
1379
1380 ConvertExt(v interface{}) interface{}
1381
1382
1383
1384
1385
1386 UpdateExt(dst interface{}, src interface{})
1387 }
1388
1389
1390 type Ext interface {
1391 BytesExt
1392 InterfaceExt
1393 }
1394
1395
1396 type addExtWrapper struct {
1397 encFn func(reflect.Value) ([]byte, error)
1398 decFn func(reflect.Value, []byte) error
1399 }
1400
1401 func (x addExtWrapper) WriteExt(v interface{}) []byte {
1402 bs, err := x.encFn(reflect.ValueOf(v))
1403 halt.onerror(err)
1404 return bs
1405 }
1406
1407 func (x addExtWrapper) ReadExt(v interface{}, bs []byte) {
1408 halt.onerror(x.decFn(reflect.ValueOf(v), bs))
1409 }
1410
1411 func (x addExtWrapper) ConvertExt(v interface{}) interface{} {
1412 return x.WriteExt(v)
1413 }
1414
1415 func (x addExtWrapper) UpdateExt(dest interface{}, v interface{}) {
1416 x.ReadExt(dest, v.([]byte))
1417 }
1418
1419 type bytesExtFailer struct{}
1420
1421 func (bytesExtFailer) WriteExt(v interface{}) []byte {
1422 halt.onerror(errExtFnWriteExtUnsupported)
1423 return nil
1424 }
1425 func (bytesExtFailer) ReadExt(v interface{}, bs []byte) {
1426 halt.onerror(errExtFnReadExtUnsupported)
1427 }
1428
1429 type interfaceExtFailer struct{}
1430
1431 func (interfaceExtFailer) ConvertExt(v interface{}) interface{} {
1432 halt.onerror(errExtFnConvertExtUnsupported)
1433 return nil
1434 }
1435 func (interfaceExtFailer) UpdateExt(dest interface{}, v interface{}) {
1436 halt.onerror(errExtFnUpdateExtUnsupported)
1437 }
1438
1439 type bytesExtWrapper struct {
1440 interfaceExtFailer
1441 BytesExt
1442 }
1443
1444 type interfaceExtWrapper struct {
1445 bytesExtFailer
1446 InterfaceExt
1447 }
1448
1449 type extFailWrapper struct {
1450 bytesExtFailer
1451 interfaceExtFailer
1452 }
1453
1454 type binaryEncodingType struct{}
1455
1456 func (binaryEncodingType) isBinary() bool { return true }
1457 func (binaryEncodingType) isJson() bool { return false }
1458
1459 type textEncodingType struct{}
1460
1461 func (textEncodingType) isBinary() bool { return false }
1462 func (textEncodingType) isJson() bool { return false }
1463
1464 type notJsonType struct{}
1465
1466 func (notJsonType) isJson() bool { return false }
1467
1468
1469
1470
1471 type noBuiltInTypes struct{}
1472
1473 func (noBuiltInTypes) EncodeBuiltin(rt uintptr, v interface{}) {}
1474 func (noBuiltInTypes) DecodeBuiltin(rt uintptr, v interface{}) {}
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488 type bigenHelper struct{}
1489
1490 func (z bigenHelper) PutUint16(v uint16) (b [2]byte) {
1491 return [...]byte{
1492 byte(v >> 8),
1493 byte(v),
1494 }
1495 }
1496
1497 func (z bigenHelper) PutUint32(v uint32) (b [4]byte) {
1498 return [...]byte{
1499 byte(v >> 24),
1500 byte(v >> 16),
1501 byte(v >> 8),
1502 byte(v),
1503 }
1504 }
1505
1506 func (z bigenHelper) PutUint64(v uint64) (b [8]byte) {
1507 return [...]byte{
1508 byte(v >> 56),
1509 byte(v >> 48),
1510 byte(v >> 40),
1511 byte(v >> 32),
1512 byte(v >> 24),
1513 byte(v >> 16),
1514 byte(v >> 8),
1515 byte(v),
1516 }
1517 }
1518
1519 func (z bigenHelper) Uint16(b [2]byte) (v uint16) {
1520 return uint16(b[1]) |
1521 uint16(b[0])<<8
1522 }
1523
1524 func (z bigenHelper) Uint32(b [4]byte) (v uint32) {
1525 return uint32(b[3]) |
1526 uint32(b[2])<<8 |
1527 uint32(b[1])<<16 |
1528 uint32(b[0])<<24
1529 }
1530
1531 func (z bigenHelper) Uint64(b [8]byte) (v uint64) {
1532 return uint64(b[7]) |
1533 uint64(b[6])<<8 |
1534 uint64(b[5])<<16 |
1535 uint64(b[4])<<24 |
1536 uint64(b[3])<<32 |
1537 uint64(b[2])<<40 |
1538 uint64(b[1])<<48 |
1539 uint64(b[0])<<56
1540 }
1541
1542 func (z bigenHelper) writeUint16(w *encWr, v uint16) {
1543 x := z.PutUint16(v)
1544 w.writen2(x[0], x[1])
1545 }
1546
1547 func (z bigenHelper) writeUint32(w *encWr, v uint32) {
1548
1549
1550
1551
1552 w.writen4(z.PutUint32(v))
1553 }
1554
1555 func (z bigenHelper) writeUint64(w *encWr, v uint64) {
1556 w.writen8(z.PutUint64(v))
1557 }
1558
1559 type extTypeTagFn struct {
1560 rtid uintptr
1561 rtidptr uintptr
1562 rt reflect.Type
1563 tag uint64
1564 ext Ext
1565 }
1566
1567 type extHandle []extTypeTagFn
1568
1569
1570
1571
1572
1573 func (x *BasicHandle) AddExt(rt reflect.Type, tag byte,
1574 encfn func(reflect.Value) ([]byte, error),
1575 decfn func(reflect.Value, []byte) error) (err error) {
1576 if encfn == nil || decfn == nil {
1577 return x.SetExt(rt, uint64(tag), nil)
1578 }
1579 return x.SetExt(rt, uint64(tag), addExtWrapper{encfn, decfn})
1580 }
1581
1582
1583
1584
1585
1586
1587
1588 func (x *BasicHandle) SetExt(rt reflect.Type, tag uint64, ext Ext) (err error) {
1589 if x.isInited() {
1590 return errHandleInited
1591 }
1592 if x.basicHandleRuntimeState == nil {
1593 x.basicHandleRuntimeState = new(basicHandleRuntimeState)
1594 }
1595 return x.basicHandleRuntimeState.setExt(rt, tag, ext)
1596 }
1597
1598 func (o extHandle) getExtForI(x interface{}) (v *extTypeTagFn) {
1599 if len(o) > 0 {
1600 v = o.getExt(i2rtid(x), true)
1601 }
1602 return
1603 }
1604
1605 func (o extHandle) getExt(rtid uintptr, check bool) (v *extTypeTagFn) {
1606 if !check {
1607 return
1608 }
1609 for i := range o {
1610 v = &o[i]
1611 if v.rtid == rtid || v.rtidptr == rtid {
1612 return
1613 }
1614 }
1615 return nil
1616 }
1617
1618 func (o extHandle) getExtForTag(tag uint64) (v *extTypeTagFn) {
1619 for i := range o {
1620 v = &o[i]
1621 if v.tag == tag {
1622 return
1623 }
1624 }
1625 return nil
1626 }
1627
1628 type intf2impl struct {
1629 rtid uintptr
1630 impl reflect.Type
1631 }
1632
1633 type intf2impls []intf2impl
1634
1635
1636
1637
1638
1639
1640
1641 func (o *intf2impls) Intf2Impl(intf, impl reflect.Type) (err error) {
1642 if impl != nil && !impl.Implements(intf) {
1643 return fmt.Errorf("Intf2Impl: %v does not implement %v", impl, intf)
1644 }
1645 rtid := rt2id(intf)
1646 o2 := *o
1647 for i := range o2 {
1648 v := &o2[i]
1649 if v.rtid == rtid {
1650 v.impl = impl
1651 return
1652 }
1653 }
1654 *o = append(o2, intf2impl{rtid, impl})
1655 return
1656 }
1657
1658 func (o intf2impls) intf2impl(rtid uintptr) (rv reflect.Value) {
1659 for i := range o {
1660 v := &o[i]
1661 if v.rtid == rtid {
1662 if v.impl == nil {
1663 return
1664 }
1665 vkind := v.impl.Kind()
1666 if vkind == reflect.Ptr {
1667 return reflect.New(v.impl.Elem())
1668 }
1669 return rvZeroAddrK(v.impl, vkind)
1670 }
1671 }
1672 return
1673 }
1674
1675
1676
1677
1678
1679
1680 type structFieldInfoPathNode struct {
1681 parent *structFieldInfoPathNode
1682
1683 offset uint16
1684 index uint16
1685 kind uint8
1686 numderef uint8
1687
1688
1689
1690
1691 encNameAsciiAlphaNum bool
1692 omitEmpty bool
1693
1694 typ reflect.Type
1695 }
1696
1697
1698 func (path *structFieldInfoPathNode) depth() (d int) {
1699 TOP:
1700 if path != nil {
1701 d++
1702 path = path.parent
1703 goto TOP
1704 }
1705 return
1706 }
1707
1708
1709 func (path *structFieldInfoPathNode) field(v reflect.Value) (rv2 reflect.Value) {
1710 if parent := path.parent; parent != nil {
1711 v = parent.field(v)
1712 for j, k := uint8(0), parent.numderef; j < k; j++ {
1713 if rvIsNil(v) {
1714 return
1715 }
1716 v = v.Elem()
1717 }
1718 }
1719 return path.rvField(v)
1720 }
1721
1722
1723
1724 func (path *structFieldInfoPathNode) fieldAlloc(v reflect.Value) (rv2 reflect.Value) {
1725 if parent := path.parent; parent != nil {
1726 v = parent.fieldAlloc(v)
1727 for j, k := uint8(0), parent.numderef; j < k; j++ {
1728 if rvIsNil(v) {
1729 rvSetDirect(v, reflect.New(v.Type().Elem()))
1730 }
1731 v = v.Elem()
1732 }
1733 }
1734 return path.rvField(v)
1735 }
1736
1737 type structFieldInfo struct {
1738 encName string
1739
1740
1741
1742
1743
1744
1745
1746
1747 path structFieldInfoPathNode
1748 }
1749
1750 func parseStructInfo(stag string) (toArray, omitEmpty bool, keytype valueType) {
1751 keytype = valueTypeString
1752 if stag == "" {
1753 return
1754 }
1755 ss := strings.Split(stag, ",")
1756 if len(ss) < 2 {
1757 return
1758 }
1759 for _, s := range ss[1:] {
1760 switch s {
1761 case "omitempty":
1762 omitEmpty = true
1763 case "toarray":
1764 toArray = true
1765 case "int":
1766 keytype = valueTypeInt
1767 case "uint":
1768 keytype = valueTypeUint
1769 case "float":
1770 keytype = valueTypeFloat
1771
1772
1773 case "string":
1774 keytype = valueTypeString
1775 }
1776 }
1777 return
1778 }
1779
1780 func (si *structFieldInfo) parseTag(stag string) {
1781 if stag == "" {
1782 return
1783 }
1784 for i, s := range strings.Split(stag, ",") {
1785 if i == 0 {
1786 if s != "" {
1787 si.encName = s
1788 }
1789 } else {
1790 switch s {
1791 case "omitempty":
1792 si.path.omitEmpty = true
1793 }
1794 }
1795 }
1796 }
1797
1798 type sfiSortedByEncName []*structFieldInfo
1799
1800 func (p sfiSortedByEncName) Len() int { return len(p) }
1801 func (p sfiSortedByEncName) Swap(i, j int) { p[uint(i)], p[uint(j)] = p[uint(j)], p[uint(i)] }
1802 func (p sfiSortedByEncName) Less(i, j int) bool { return p[uint(i)].encName < p[uint(j)].encName }
1803
1804
1805
1806 type typeInfo4Container struct {
1807 elem reflect.Type
1808
1809
1810
1811
1812 key reflect.Type
1813
1814
1815
1816
1817
1818 fastpathUnderlying reflect.Type
1819
1820 tikey *typeInfo
1821 tielem *typeInfo
1822 }
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833 type typeInfo struct {
1834 rt reflect.Type
1835 ptr reflect.Type
1836
1837
1838
1839 rtid uintptr
1840
1841 numMeth uint16
1842 kind uint8
1843 chandir uint8
1844
1845 anyOmitEmpty bool
1846 toArray bool
1847 keyType valueType
1848 mbs bool
1849
1850 sfi4Name map[string]*structFieldInfo
1851
1852 *typeInfo4Container
1853
1854
1855
1856 size, keysize, elemsize uint32
1857
1858 keykind, elemkind uint8
1859
1860 flagHasPkgPath bool
1861 flagComparable bool
1862 flagCanTransient bool
1863
1864 flagMarshalInterface bool
1865 flagSelferViaCodecgen bool
1866
1867
1868 flagIsZeroer bool
1869 flagIsZeroerPtr bool
1870
1871 flagIsCodecEmptyer bool
1872 flagIsCodecEmptyerPtr bool
1873
1874 flagBinaryMarshaler bool
1875 flagBinaryMarshalerPtr bool
1876
1877 flagBinaryUnmarshaler bool
1878 flagBinaryUnmarshalerPtr bool
1879
1880 flagTextMarshaler bool
1881 flagTextMarshalerPtr bool
1882
1883 flagTextUnmarshaler bool
1884 flagTextUnmarshalerPtr bool
1885
1886 flagJsonMarshaler bool
1887 flagJsonMarshalerPtr bool
1888
1889 flagJsonUnmarshaler bool
1890 flagJsonUnmarshalerPtr bool
1891
1892 flagSelfer bool
1893 flagSelferPtr bool
1894
1895 flagMissingFielder bool
1896 flagMissingFielderPtr bool
1897
1898 infoFieldOmitempty bool
1899
1900 sfi structFieldInfos
1901 }
1902
1903 func (ti *typeInfo) siForEncName(name []byte) (si *structFieldInfo) {
1904 return ti.sfi4Name[string(name)]
1905 }
1906
1907 func (ti *typeInfo) resolve(x []structFieldInfo, ss map[string]uint16) (n int) {
1908 n = len(x)
1909
1910 for i := range x {
1911 ui := uint16(i)
1912 xn := x[i].encName
1913 j, ok := ss[xn]
1914 if ok {
1915 i2clear := ui
1916 if x[i].path.depth() < x[j].path.depth() {
1917 ss[xn] = ui
1918 i2clear = j
1919 }
1920 if x[i2clear].encName != "" {
1921 x[i2clear].encName = ""
1922 n--
1923 }
1924 } else {
1925 ss[xn] = ui
1926 }
1927 }
1928
1929 return
1930 }
1931
1932 func (ti *typeInfo) init(x []structFieldInfo, n int) {
1933 var anyOmitEmpty bool
1934
1935
1936 m := make(map[string]*structFieldInfo, n)
1937 w := make([]structFieldInfo, n)
1938 y := make([]*structFieldInfo, n+n)
1939 z := y[n:]
1940 y = y[:n]
1941 n = 0
1942 for i := range x {
1943 if x[i].encName == "" {
1944 continue
1945 }
1946 if !anyOmitEmpty && x[i].path.omitEmpty {
1947 anyOmitEmpty = true
1948 }
1949 w[n] = x[i]
1950 y[n] = &w[n]
1951 m[x[i].encName] = &w[n]
1952 n++
1953 }
1954 if n != len(y) {
1955 halt.errorf("failure reading struct %v - expecting %d of %d valid fields, got %d", ti.rt, len(y), len(x), n)
1956 }
1957
1958 copy(z, y)
1959 sort.Sort(sfiSortedByEncName(z))
1960
1961 ti.anyOmitEmpty = anyOmitEmpty
1962 ti.sfi.load(y, z)
1963 ti.sfi4Name = m
1964 }
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979 func transientBitsetFlags() *bitset32 {
1980 if transientValueHasStringSlice {
1981 return &numBoolStrSliceBitset
1982 }
1983 return &numBoolBitset
1984 }
1985
1986 func isCanTransient(t reflect.Type, k reflect.Kind) (v bool) {
1987 var bs = transientBitsetFlags()
1988 if bs.isset(byte(k)) {
1989 v = true
1990 } else if k == reflect.Slice {
1991 elem := t.Elem()
1992 v = numBoolBitset.isset(byte(elem.Kind()))
1993 } else if k == reflect.Array {
1994 elem := t.Elem()
1995 v = isCanTransient(elem, elem.Kind())
1996 } else if k == reflect.Struct {
1997 v = true
1998 for j, jlen := 0, t.NumField(); j < jlen; j++ {
1999 f := t.Field(j)
2000 if !isCanTransient(f.Type, f.Type.Kind()) {
2001 v = false
2002 return
2003 }
2004 }
2005 } else {
2006 v = false
2007 }
2008 return
2009 }
2010
2011 func (ti *typeInfo) doSetFlagCanTransient() {
2012 if transientSizeMax > 0 {
2013 ti.flagCanTransient = ti.size <= transientSizeMax
2014 } else {
2015 ti.flagCanTransient = true
2016 }
2017 if ti.flagCanTransient {
2018 if !transientBitsetFlags().isset(ti.kind) {
2019 ti.flagCanTransient = isCanTransient(ti.rt, reflect.Kind(ti.kind))
2020 }
2021 }
2022 }
2023
2024 type rtid2ti struct {
2025 rtid uintptr
2026 ti *typeInfo
2027 }
2028
2029
2030
2031
2032
2033 type TypeInfos struct {
2034 infos atomicTypeInfoSlice
2035 mu sync.Mutex
2036 _ uint64
2037 tags []string
2038 _ uint64
2039 }
2040
2041
2042
2043
2044
2045 func NewTypeInfos(tags []string) *TypeInfos {
2046 return &TypeInfos{tags: tags}
2047 }
2048
2049 func (x *TypeInfos) structTag(t reflect.StructTag) (s string) {
2050
2051
2052 for _, x := range x.tags {
2053 s = t.Get(x)
2054 if s != "" {
2055 return s
2056 }
2057 }
2058 return
2059 }
2060
2061 func findTypeInfo(s []rtid2ti, rtid uintptr) (i uint, ti *typeInfo) {
2062
2063
2064
2065 var h uint
2066 var j = uint(len(s))
2067 LOOP:
2068 if i < j {
2069 h = (i + j) >> 1
2070 if s[h].rtid < rtid {
2071 i = h + 1
2072 } else {
2073 j = h
2074 }
2075 goto LOOP
2076 }
2077 if i < uint(len(s)) && s[i].rtid == rtid {
2078 ti = s[i].ti
2079 }
2080 return
2081 }
2082
2083 func (x *TypeInfos) get(rtid uintptr, rt reflect.Type) (pti *typeInfo) {
2084 if pti = x.find(rtid); pti == nil {
2085 pti = x.load(rt)
2086 }
2087 return
2088 }
2089
2090 func (x *TypeInfos) find(rtid uintptr) (pti *typeInfo) {
2091 sp := x.infos.load()
2092 if sp != nil {
2093 _, pti = findTypeInfo(sp, rtid)
2094 }
2095 return
2096 }
2097
2098 func (x *TypeInfos) load(rt reflect.Type) (pti *typeInfo) {
2099 rk := rt.Kind()
2100
2101 if rk == reflect.Ptr {
2102 halt.errorf("invalid kind passed to TypeInfos.get: %v - %v", rk, rt)
2103 }
2104
2105 rtid := rt2id(rt)
2106
2107
2108
2109 ti := typeInfo{
2110 rt: rt,
2111 ptr: reflect.PtrTo(rt),
2112 rtid: rtid,
2113 kind: uint8(rk),
2114 size: uint32(rt.Size()),
2115 numMeth: uint16(rt.NumMethod()),
2116 keyType: valueTypeString,
2117
2118
2119 flagHasPkgPath: rt.PkgPath() != "",
2120 }
2121
2122
2123 bset := func(when bool, b *bool) {
2124 if when {
2125 *b = true
2126 }
2127 }
2128
2129 var b1, b2 bool
2130
2131 b1, b2 = implIntf(rt, binaryMarshalerTyp)
2132 bset(b1, &ti.flagBinaryMarshaler)
2133 bset(b2, &ti.flagBinaryMarshalerPtr)
2134 b1, b2 = implIntf(rt, binaryUnmarshalerTyp)
2135 bset(b1, &ti.flagBinaryUnmarshaler)
2136 bset(b2, &ti.flagBinaryUnmarshalerPtr)
2137 b1, b2 = implIntf(rt, textMarshalerTyp)
2138 bset(b1, &ti.flagTextMarshaler)
2139 bset(b2, &ti.flagTextMarshalerPtr)
2140 b1, b2 = implIntf(rt, textUnmarshalerTyp)
2141 bset(b1, &ti.flagTextUnmarshaler)
2142 bset(b2, &ti.flagTextUnmarshalerPtr)
2143 b1, b2 = implIntf(rt, jsonMarshalerTyp)
2144 bset(b1, &ti.flagJsonMarshaler)
2145 bset(b2, &ti.flagJsonMarshalerPtr)
2146 b1, b2 = implIntf(rt, jsonUnmarshalerTyp)
2147 bset(b1, &ti.flagJsonUnmarshaler)
2148 bset(b2, &ti.flagJsonUnmarshalerPtr)
2149 b1, b2 = implIntf(rt, selferTyp)
2150 bset(b1, &ti.flagSelfer)
2151 bset(b2, &ti.flagSelferPtr)
2152 b1, b2 = implIntf(rt, missingFielderTyp)
2153 bset(b1, &ti.flagMissingFielder)
2154 bset(b2, &ti.flagMissingFielderPtr)
2155 b1, b2 = implIntf(rt, iszeroTyp)
2156 bset(b1, &ti.flagIsZeroer)
2157 bset(b2, &ti.flagIsZeroerPtr)
2158 b1, b2 = implIntf(rt, isCodecEmptyerTyp)
2159 bset(b1, &ti.flagIsCodecEmptyer)
2160 bset(b2, &ti.flagIsCodecEmptyerPtr)
2161
2162 b1, b2 = implIntf(rt, isSelferViaCodecgenerTyp)
2163 ti.flagSelferViaCodecgen = b1 || b2
2164
2165 ti.flagMarshalInterface = ti.flagSelfer || ti.flagSelferPtr ||
2166 ti.flagSelferViaCodecgen ||
2167 ti.flagBinaryMarshaler || ti.flagBinaryMarshalerPtr ||
2168 ti.flagBinaryUnmarshaler || ti.flagBinaryUnmarshalerPtr ||
2169 ti.flagTextMarshaler || ti.flagTextMarshalerPtr ||
2170 ti.flagTextUnmarshaler || ti.flagTextUnmarshalerPtr ||
2171 ti.flagJsonMarshaler || ti.flagJsonMarshalerPtr ||
2172 ti.flagJsonUnmarshaler || ti.flagJsonUnmarshalerPtr
2173
2174 b1 = rt.Comparable()
2175
2176 ti.flagComparable = b1
2177
2178 ti.doSetFlagCanTransient()
2179
2180 var tt reflect.Type
2181 switch rk {
2182 case reflect.Struct:
2183 var omitEmpty bool
2184 if f, ok := rt.FieldByName(structInfoFieldName); ok {
2185 ti.toArray, omitEmpty, ti.keyType = parseStructInfo(x.structTag(f.Tag))
2186 ti.infoFieldOmitempty = omitEmpty
2187 } else {
2188 ti.keyType = valueTypeString
2189 }
2190 pp, pi := &pool4tiload, pool4tiload.Get()
2191 pv := pi.(*typeInfoLoad)
2192 pv.reset()
2193 pv.etypes = append(pv.etypes, ti.rtid)
2194 x.rget(rt, rtid, nil, pv, omitEmpty)
2195 n := ti.resolve(pv.sfis, pv.sfiNames)
2196 ti.init(pv.sfis, n)
2197 pp.Put(pi)
2198 case reflect.Map:
2199 ti.typeInfo4Container = new(typeInfo4Container)
2200 ti.elem = rt.Elem()
2201 for tt = ti.elem; tt.Kind() == reflect.Ptr; tt = tt.Elem() {
2202 }
2203 ti.tielem = x.get(rt2id(tt), tt)
2204 ti.elemkind = uint8(ti.elem.Kind())
2205 ti.elemsize = uint32(ti.elem.Size())
2206 ti.key = rt.Key()
2207 for tt = ti.key; tt.Kind() == reflect.Ptr; tt = tt.Elem() {
2208 }
2209 ti.tikey = x.get(rt2id(tt), tt)
2210 ti.keykind = uint8(ti.key.Kind())
2211 ti.keysize = uint32(ti.key.Size())
2212 if ti.flagHasPkgPath {
2213 ti.fastpathUnderlying = reflect.MapOf(ti.key, ti.elem)
2214 }
2215 case reflect.Slice:
2216 ti.typeInfo4Container = new(typeInfo4Container)
2217 ti.mbs, b2 = implIntf(rt, mapBySliceTyp)
2218 if !ti.mbs && b2 {
2219 ti.mbs = b2
2220 }
2221 ti.elem = rt.Elem()
2222 for tt = ti.elem; tt.Kind() == reflect.Ptr; tt = tt.Elem() {
2223 }
2224 ti.tielem = x.get(rt2id(tt), tt)
2225 ti.elemkind = uint8(ti.elem.Kind())
2226 ti.elemsize = uint32(ti.elem.Size())
2227 if ti.flagHasPkgPath {
2228 ti.fastpathUnderlying = reflect.SliceOf(ti.elem)
2229 }
2230 case reflect.Chan:
2231 ti.typeInfo4Container = new(typeInfo4Container)
2232 ti.elem = rt.Elem()
2233 for tt = ti.elem; tt.Kind() == reflect.Ptr; tt = tt.Elem() {
2234 }
2235 ti.tielem = x.get(rt2id(tt), tt)
2236 ti.elemkind = uint8(ti.elem.Kind())
2237 ti.elemsize = uint32(ti.elem.Size())
2238 ti.chandir = uint8(rt.ChanDir())
2239 ti.key = reflect.SliceOf(ti.elem)
2240 ti.keykind = uint8(reflect.Slice)
2241 case reflect.Array:
2242 ti.typeInfo4Container = new(typeInfo4Container)
2243 ti.mbs, b2 = implIntf(rt, mapBySliceTyp)
2244 if !ti.mbs && b2 {
2245 ti.mbs = b2
2246 }
2247 ti.elem = rt.Elem()
2248 ti.elemkind = uint8(ti.elem.Kind())
2249 ti.elemsize = uint32(ti.elem.Size())
2250 for tt = ti.elem; tt.Kind() == reflect.Ptr; tt = tt.Elem() {
2251 }
2252 ti.tielem = x.get(rt2id(tt), tt)
2253 ti.key = reflect.SliceOf(ti.elem)
2254 ti.keykind = uint8(reflect.Slice)
2255 ti.keysize = uint32(ti.key.Size())
2256 if ti.flagHasPkgPath {
2257 ti.fastpathUnderlying = ti.key
2258 }
2259
2260
2261
2262
2263
2264
2265 }
2266
2267 x.mu.Lock()
2268 sp := x.infos.load()
2269
2270
2271 if sp == nil {
2272 pti = &ti
2273 sp = []rtid2ti{{rtid, pti}}
2274 x.infos.store(sp)
2275 } else {
2276 var idx uint
2277 idx, pti = findTypeInfo(sp, rtid)
2278 if pti == nil {
2279 pti = &ti
2280 sp2 := make([]rtid2ti, len(sp)+1)
2281 copy(sp2[idx+1:], sp[idx:])
2282 copy(sp2, sp[:idx])
2283 sp2[idx] = rtid2ti{rtid, pti}
2284 x.infos.store(sp2)
2285 }
2286 }
2287 x.mu.Unlock()
2288 return
2289 }
2290
2291 func (x *TypeInfos) rget(rt reflect.Type, rtid uintptr,
2292 path *structFieldInfoPathNode, pv *typeInfoLoad, omitEmpty bool) {
2293
2294
2295
2296
2297
2298
2299
2300
2301 flen := rt.NumField()
2302 LOOP:
2303 for j, jlen := uint16(0), uint16(flen); j < jlen; j++ {
2304 f := rt.Field(int(j))
2305 fkind := f.Type.Kind()
2306
2307
2308 switch fkind {
2309 case reflect.Func, reflect.UnsafePointer:
2310 continue LOOP
2311 }
2312
2313 isUnexported := f.PkgPath != ""
2314 if isUnexported && !f.Anonymous {
2315 continue
2316 }
2317 stag := x.structTag(f.Tag)
2318 if stag == "-" {
2319 continue
2320 }
2321 var si structFieldInfo
2322
2323 var numderef uint8 = 0
2324 for xft := f.Type; xft.Kind() == reflect.Ptr; xft = xft.Elem() {
2325 numderef++
2326 }
2327
2328 var parsed bool
2329
2330
2331 if f.Anonymous && fkind != reflect.Interface {
2332
2333 ft := f.Type
2334 isPtr := ft.Kind() == reflect.Ptr
2335 for ft.Kind() == reflect.Ptr {
2336 ft = ft.Elem()
2337 }
2338 isStruct := ft.Kind() == reflect.Struct
2339
2340
2341
2342
2343
2344 if (isUnexported && !isStruct) || (!allowSetUnexportedEmbeddedPtr && isUnexported && isPtr) {
2345 continue
2346 }
2347 doInline := stag == ""
2348 if !doInline {
2349 si.parseTag(stag)
2350 parsed = true
2351 doInline = si.encName == ""
2352 }
2353 if doInline && isStruct {
2354
2355 ftid := rt2id(ft)
2356
2357
2358
2359
2360 processIt := true
2361 numk := 0
2362 for _, k := range pv.etypes {
2363 if k == ftid {
2364 numk++
2365 if numk == rgetMaxRecursion {
2366 processIt = false
2367 break
2368 }
2369 }
2370 }
2371 if processIt {
2372 pv.etypes = append(pv.etypes, ftid)
2373 path2 := &structFieldInfoPathNode{
2374 parent: path,
2375 typ: f.Type,
2376 offset: uint16(f.Offset),
2377 index: j,
2378 kind: uint8(fkind),
2379 numderef: numderef,
2380 }
2381 x.rget(ft, ftid, path2, pv, omitEmpty)
2382 }
2383 continue
2384 }
2385 }
2386
2387
2388 if isUnexported || f.Name == "" {
2389 continue
2390 }
2391
2392 si.path = structFieldInfoPathNode{
2393 parent: path,
2394 typ: f.Type,
2395 offset: uint16(f.Offset),
2396 index: j,
2397 kind: uint8(fkind),
2398 numderef: numderef,
2399
2400 encNameAsciiAlphaNum: true,
2401
2402 omitEmpty: si.path.omitEmpty,
2403 }
2404
2405 if !parsed {
2406 si.encName = f.Name
2407 si.parseTag(stag)
2408 parsed = true
2409 } else if si.encName == "" {
2410 si.encName = f.Name
2411 }
2412
2413
2414
2415 if omitEmpty {
2416 si.path.omitEmpty = true
2417 }
2418
2419 for i := len(si.encName) - 1; i >= 0; i-- {
2420 if !asciiAlphaNumBitset.isset(si.encName[i]) {
2421 si.path.encNameAsciiAlphaNum = false
2422 break
2423 }
2424 }
2425
2426 pv.sfis = append(pv.sfis, si)
2427 }
2428 }
2429
2430 func implIntf(rt, iTyp reflect.Type) (base bool, indir bool) {
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440 base = rt.Implements(iTyp)
2441 if base {
2442 indir = true
2443 } else {
2444 indir = reflect.PtrTo(rt).Implements(iTyp)
2445 }
2446 return
2447 }
2448
2449 func bool2int(b bool) (v uint8) {
2450
2451 if b {
2452 v = 1
2453 }
2454 return
2455 }
2456
2457 func isSliceBoundsError(s string) bool {
2458 return strings.Contains(s, "index out of range") ||
2459 strings.Contains(s, "slice bounds out of range")
2460 }
2461
2462 func sprintf(format string, v ...interface{}) string {
2463 return fmt.Sprintf(format, v...)
2464 }
2465
2466 func panicValToErr(h errDecorator, v interface{}, err *error) {
2467 if v == *err {
2468 return
2469 }
2470 switch xerr := v.(type) {
2471 case nil:
2472 case runtime.Error:
2473 d, dok := h.(*Decoder)
2474 if dok && d.bytes && isSliceBoundsError(xerr.Error()) {
2475 *err = io.ErrUnexpectedEOF
2476 } else {
2477 h.wrapErr(xerr, err)
2478 }
2479 case error:
2480 switch xerr {
2481 case nil:
2482 case io.EOF, io.ErrUnexpectedEOF, errEncoderNotInitialized, errDecoderNotInitialized:
2483
2484 *err = xerr
2485 default:
2486 h.wrapErr(xerr, err)
2487 }
2488 default:
2489
2490 h.wrapErr(fmt.Errorf("%v", v), err)
2491 }
2492 }
2493
2494 func usableByteSlice(bs []byte, slen int) (out []byte, changed bool) {
2495 const maxCap = 1024 * 1024 * 64
2496 const skipMaxCap = false
2497 if slen <= 0 {
2498 return []byte{}, true
2499 }
2500 if slen <= cap(bs) {
2501 return bs[:slen], false
2502 }
2503
2504 if skipMaxCap || slen <= maxCap {
2505 return make([]byte, slen), true
2506 }
2507 return make([]byte, maxCap), true
2508 }
2509
2510 func mapKeyFastKindFor(k reflect.Kind) mapKeyFastKind {
2511 return mapKeyFastKindVals[k&31]
2512 }
2513
2514
2515
2516 type codecFnInfo struct {
2517 ti *typeInfo
2518 xfFn Ext
2519 xfTag uint64
2520 addrD bool
2521 addrDf bool
2522 addrE bool
2523
2524 }
2525
2526
2527
2528
2529
2530 type codecFn struct {
2531 i codecFnInfo
2532 fe func(*Encoder, *codecFnInfo, reflect.Value)
2533 fd func(*Decoder, *codecFnInfo, reflect.Value)
2534
2535 }
2536
2537 type codecRtidFn struct {
2538 rtid uintptr
2539 fn *codecFn
2540 }
2541
2542 func makeExt(ext interface{}) Ext {
2543 switch t := ext.(type) {
2544 case Ext:
2545 return t
2546 case BytesExt:
2547 return &bytesExtWrapper{BytesExt: t}
2548 case InterfaceExt:
2549 return &interfaceExtWrapper{InterfaceExt: t}
2550 }
2551 return &extFailWrapper{}
2552 }
2553
2554 func baseRV(v interface{}) (rv reflect.Value) {
2555
2556 for rv = reflect.ValueOf(v); rv.Kind() == reflect.Ptr; rv = rv.Elem() {
2557 }
2558 return
2559 }
2560
2561
2562
2563
2564
2565
2566
2567
2568 type checkOverflow struct{}
2569
2570 func (checkOverflow) Float32(v float64) (overflow bool) {
2571 if v < 0 {
2572 v = -v
2573 }
2574 return math.MaxFloat32 < v && v <= math.MaxFloat64
2575 }
2576 func (checkOverflow) Uint(v uint64, bitsize uint8) (overflow bool) {
2577 if v != 0 && v != (v<<(64-bitsize))>>(64-bitsize) {
2578 overflow = true
2579 }
2580 return
2581 }
2582 func (checkOverflow) Int(v int64, bitsize uint8) (overflow bool) {
2583 if v != 0 && v != (v<<(64-bitsize))>>(64-bitsize) {
2584 overflow = true
2585 }
2586 return
2587 }
2588
2589 func (checkOverflow) Uint2Int(v uint64, neg bool) (overflow bool) {
2590 return (neg && v > 1<<63) || (!neg && v >= 1<<63)
2591 }
2592
2593 func (checkOverflow) SignedInt(v uint64) (overflow bool) {
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609 overflow = (v>>63) != 0 && v&0x7fffffffffffffff > math.MaxInt64-1
2610
2611 return
2612 }
2613
2614 func (x checkOverflow) Float32V(v float64) float64 {
2615 if x.Float32(v) {
2616 halt.errorf("float32 overflow: %v", v)
2617 }
2618 return v
2619 }
2620 func (x checkOverflow) UintV(v uint64, bitsize uint8) uint64 {
2621 if x.Uint(v, bitsize) {
2622 halt.errorf("uint64 overflow: %v", v)
2623 }
2624 return v
2625 }
2626 func (x checkOverflow) IntV(v int64, bitsize uint8) int64 {
2627 if x.Int(v, bitsize) {
2628 halt.errorf("int64 overflow: %v", v)
2629 }
2630 return v
2631 }
2632 func (x checkOverflow) SignedIntV(v uint64) int64 {
2633 if x.SignedInt(v) {
2634 halt.errorf("uint64 to int64 overflow: %v", v)
2635 }
2636 return int64(v)
2637 }
2638
2639
2640
2641 func isNaN64(f float64) bool { return f != f }
2642
2643 func isWhitespaceChar(v byte) bool {
2644
2645
2646 return v < 33
2647
2648
2649
2650
2651 }
2652
2653 func isNumberChar(v byte) bool {
2654
2655
2656 return numCharBitset.isset(v)
2657
2658
2659 }
2660
2661
2662
2663 type ioFlusher interface {
2664 Flush() error
2665 }
2666
2667 type ioBuffered interface {
2668 Buffered() int
2669 }
2670
2671
2672
2673 type sfiRv struct {
2674 v *structFieldInfo
2675 r reflect.Value
2676 }
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700 type bitset32 [32]bool
2701
2702 func (x *bitset32) set(pos byte) *bitset32 {
2703 x[pos&31] = true
2704 return x
2705 }
2706 func (x *bitset32) isset(pos byte) bool {
2707 return x[pos&31]
2708 }
2709
2710 type bitset256 [256]bool
2711
2712 func (x *bitset256) set(pos byte) *bitset256 {
2713 x[pos] = true
2714 return x
2715 }
2716 func (x *bitset256) isset(pos byte) bool {
2717 return x[pos]
2718 }
2719
2720
2721
2722 type panicHdl struct{}
2723
2724
2725 func (panicHdl) onerror(err error) {
2726 if err != nil {
2727 panic(err)
2728 }
2729 }
2730
2731
2732
2733
2734
2735
2736
2737 func (panicHdl) errorf(format string, params ...interface{}) {
2738 if format == "" {
2739 panic(errPanicUndefined)
2740 }
2741 if len(params) == 0 {
2742 panic(errors.New(format))
2743 }
2744 panic(fmt.Errorf(format, params...))
2745 }
2746
2747
2748
2749 type errDecorator interface {
2750 wrapErr(in error, out *error)
2751 }
2752
2753 type errDecoratorDef struct{}
2754
2755 func (errDecoratorDef) wrapErr(v error, e *error) { *e = v }
2756
2757
2758
2759 type mustHdl struct{}
2760
2761 func (mustHdl) String(s string, err error) string {
2762 halt.onerror(err)
2763 return s
2764 }
2765 func (mustHdl) Int(s int64, err error) int64 {
2766 halt.onerror(err)
2767 return s
2768 }
2769 func (mustHdl) Uint(s uint64, err error) uint64 {
2770 halt.onerror(err)
2771 return s
2772 }
2773 func (mustHdl) Float(s float64, err error) float64 {
2774 halt.onerror(err)
2775 return s
2776 }
2777
2778
2779
2780 func freelistCapacity(length int) (capacity int) {
2781 for capacity = 8; capacity <= length; capacity *= 2 {
2782 }
2783 return
2784 }
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810 type bytesFreelist [][]byte
2811
2812
2813
2814 func (x *bytesFreelist) peek(length int, pop bool) (out []byte) {
2815 if bytesFreeListNoCache {
2816 return make([]byte, 0, freelistCapacity(length))
2817 }
2818 y := *x
2819 if len(y) > 0 {
2820 out = y[len(y)-1]
2821 }
2822
2823 const minLenBytes = 64
2824 if length < minLenBytes {
2825 length = minLenBytes
2826 }
2827 if cap(out) < length {
2828 out = make([]byte, 0, freelistCapacity(length))
2829 y = append(y, out)
2830 *x = y
2831 }
2832 if pop && len(y) > 0 {
2833 y = y[:len(y)-1]
2834 *x = y
2835 }
2836 return
2837 }
2838
2839
2840
2841 func (x *bytesFreelist) get(length int) (out []byte) {
2842 if bytesFreeListNoCache {
2843 return make([]byte, 0, freelistCapacity(length))
2844 }
2845 y := *x
2846
2847
2848 for i := 0; i < len(y); i++ {
2849 v := y[i]
2850 if cap(v) >= length {
2851
2852 copy(y[i:], y[i+1:])
2853 *x = y[:len(y)-1]
2854 return v
2855 }
2856 }
2857 return make([]byte, 0, freelistCapacity(length))
2858 }
2859
2860 func (x *bytesFreelist) put(v []byte) {
2861 if bytesFreeListNoCache || cap(v) == 0 {
2862 return
2863 }
2864 if len(v) != 0 {
2865 v = v[:0]
2866 }
2867
2868 y := append(*x, v)
2869 *x = y
2870
2871
2872 for i := 0; i < len(y)-1; i++ {
2873 z := y[i]
2874 if cap(z) > cap(v) {
2875 copy(y[i+1:], y[i:])
2876 y[i] = v
2877 return
2878 }
2879 }
2880 }
2881
2882 func (x *bytesFreelist) check(v []byte, length int) (out []byte) {
2883
2884 if cap(v) >= length {
2885 return v[:0]
2886 }
2887 return x.checkPutGet(v, length)
2888 }
2889
2890 func (x *bytesFreelist) checkPutGet(v []byte, length int) []byte {
2891
2892 const useSeparateCalls = false
2893
2894 if useSeparateCalls {
2895 x.put(v)
2896 return x.get(length)
2897 }
2898
2899 if bytesFreeListNoCache {
2900 return make([]byte, 0, freelistCapacity(length))
2901 }
2902
2903
2904 y := *x
2905 var put = cap(v) == 0
2906 if !put {
2907 y = append(y, v)
2908 *x = y
2909 }
2910 for i := 0; i < len(y); i++ {
2911 z := y[i]
2912 if put {
2913 if cap(z) >= length {
2914 copy(y[i:], y[i+1:])
2915 y = y[:len(y)-1]
2916 *x = y
2917 return z
2918 }
2919 } else {
2920 if cap(z) > cap(v) {
2921 copy(y[i+1:], y[i:])
2922 y[i] = v
2923 put = true
2924 }
2925 }
2926 }
2927 return make([]byte, 0, freelistCapacity(length))
2928 }
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941 type sfiRvFreelist [][]sfiRv
2942
2943 func (x *sfiRvFreelist) get(length int) (out []sfiRv) {
2944 y := *x
2945
2946
2947
2948 for i := 0; i < len(y); i++ {
2949 v := y[i]
2950 if cap(v) >= length {
2951
2952 copy(y[i:], y[i+1:])
2953 *x = y[:len(y)-1]
2954 return v
2955 }
2956 }
2957 return make([]sfiRv, 0, freelistCapacity(length))
2958 }
2959
2960 func (x *sfiRvFreelist) put(v []sfiRv) {
2961 if len(v) != 0 {
2962 v = v[:0]
2963 }
2964
2965 y := append(*x, v)
2966 *x = y
2967
2968
2969 for i := 0; i < len(y)-1; i++ {
2970 z := y[i]
2971 if cap(z) > cap(v) {
2972 copy(y[i+1:], y[i:])
2973 y[i] = v
2974 return
2975 }
2976 }
2977 }
2978
2979
2980
2981
2982
2983
2984
2985
2986 const (
2987 internMaxStrLen = 16
2988 internCap = 64 * 2
2989 )
2990
2991 type internerMap map[string]string
2992
2993 func (x *internerMap) init() {
2994 *x = make(map[string]string, internCap)
2995 }
2996
2997 func (x internerMap) string(v []byte) (s string) {
2998 s, ok := x[string(v)]
2999 if !ok {
3000 s = string(v)
3001 x[s] = s
3002 }
3003 return
3004 }
3005
View as plain text