/
helper.go
2960 lines (2595 loc) · 81.8 KB
/
helper.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2012-2020 Ugorji Nwoke. All rights reserved.
// Use of this source code is governed by a MIT license found in the LICENSE file.
package codec
// Contains code shared by both encode and decode.
// Some shared ideas around encoding/decoding
// ------------------------------------------
//
// If an interface{} is passed, we first do a type assertion to see if it is
// a primitive type or a map/slice of primitive types, and use a fastpath to handle it.
//
// If we start with a reflect.Value, we are already in reflect.Value land and
// will try to grab the function for the underlying Type and directly call that function.
// This is more performant than calling reflect.Value.Interface().
//
// This still helps us bypass many layers of reflection, and give best performance.
//
// Containers
// ------------
// Containers in the stream are either associative arrays (key-value pairs) or
// regular arrays (indexed by incrementing integers).
//
// Some streams support indefinite-length containers, and use a breaking
// byte-sequence to denote that the container has come to an end.
//
// Some streams also are text-based, and use explicit separators to denote the
// end/beginning of different values.
//
// Philosophy
// ------------
// On decode, this codec will update containers appropriately:
// - If struct, update fields from stream into fields of struct.
// If field in stream not found in struct, handle appropriately (based on option).
// If a struct field has no corresponding value in the stream, leave it AS IS.
// If nil in stream, set value to nil/zero value.
// - If map, update map from stream.
// If the stream value is NIL, set the map to nil.
// - if slice, try to update up to length of array in stream.
// if container len is less than stream array length,
// and container cannot be expanded, handled (based on option).
// This means you can decode 4-element stream array into 1-element array.
//
// ------------------------------------
// On encode, user can specify omitEmpty. This means that the value will be omitted
// if the zero value. The problem may occur during decode, where omitted values do not affect
// the value being decoded into. This means that if decoding into a struct with an
// int field with current value=5, and the field is omitted in the stream, then after
// decoding, the value will still be 5 (not 0).
// omitEmpty only works if you guarantee that you always decode into zero-values.
//
// ------------------------------------
// We could have truncated a map to remove keys not available in the stream,
// or set values in the struct which are not in the stream to their zero values.
// We decided against it because there is no efficient way to do it.
// We may introduce it as an option later.
// However, that will require enabling it for both runtime and code generation modes.
//
// To support truncate, we need to do 2 passes over the container:
// map
// - first collect all keys (e.g. in k1)
// - for each key in stream, mark k1 that the key should not be removed
// - after updating map, do second pass and call delete for all keys in k1 which are not marked
// struct:
// - for each field, track the *typeInfo s1
// - iterate through all s1, and for each one not marked, set value to zero
// - this involves checking the possible anonymous fields which are nil ptrs.
// too much work.
//
// ------------------------------------------
// Error Handling is done within the library using panic.
//
// This way, the code doesn't have to keep checking if an error has happened,
// and we don't have to keep sending the error value along with each call
// or storing it in the En|Decoder and checking it constantly along the way.
//
// We considered storing the error is En|Decoder.
// - once it has its err field set, it cannot be used again.
// - panicing will be optional, controlled by const flag.
// - code should always check error first and return early.
//
// We eventually decided against it as it makes the code clumsier to always
// check for these error conditions.
//
// ------------------------------------------
// We use sync.Pool only for the aid of long-lived objects shared across multiple goroutines.
// Encoder, Decoder, enc|decDriver, reader|writer, etc do not fall into this bucket.
//
// Also, GC is much better now, eliminating some of the reasons to use a shared pool structure.
// Instead, the short-lived objects use free-lists that live as long as the object exists.
//
// ------------------------------------------
// Performance is affected by the following:
// - Bounds Checking
// - Inlining
// - Pointer chasing
// This package tries hard to manage the performance impact of these.
//
// ------------------------------------------
// To alleviate performance due to pointer-chasing:
// - Prefer non-pointer values in a struct field
// - Refer to these directly within helper classes
// e.g. json.go refers directly to d.d.decRd
//
// We made the changes to embed En/Decoder in en/decDriver,
// but we had to explicitly reference the fields as opposed to using a function
// to get the better performance that we were looking for.
// For example, we explicitly call d.d.decRd.fn() instead of d.d.r().fn().
//
// ------------------------------------------
// Bounds Checking
// - Allow bytesDecReader to incur "bounds check error", and
// recover that as an io.EOF.
// This allows the bounds check branch to always be taken by the branch predictor,
// giving better performance (in theory), while ensuring that the code is shorter.
//
// ------------------------------------------
// Escape Analysis
// - Prefer to return non-pointers if the value is used right away.
// Newly allocated values returned as pointers will be heap-allocated as they escape.
//
// Prefer functions and methods that
// - take no parameters and
// - return no results and
// - do not allocate.
// These are optimized by the runtime.
// For example, in json, we have dedicated functions for ReadMapElemKey, etc
// which do not delegate to readDelim, as readDelim takes a parameter.
// The difference in runtime was as much as 5%.
//
// ------------------------------------------
// Handling Nil
// - In dynamic (reflection) mode, decodeValue and encodeValue handle nil at the top
// - Consequently, methods used with them as a parent in the chain e.g. kXXX
// methods do not handle nil.
// - Fastpath methods also do not handle nil.
// The switch called in (en|de)code(...) handles it so the dependent calls don't have to.
// - codecgen will handle nil before calling into the library for further work also.
//
// ------------------------------------------
// Passing reflect.Kind to functions that take a reflect.Value
// - Note that reflect.Value.Kind() is very cheap, as its fundamentally a binary AND of 2 numbers
import (
"encoding"
"encoding/binary"
"errors"
"fmt"
"io"
"math"
"reflect"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"unicode/utf8"
)
// if debugging is true, then
// - within Encode/Decode, do not recover from panic's
// - etc
//
// Note: Negative tests that check for errors will fail, so only use this
// when debugging, and run only one test at a time preferably.
const debugging = false
const (
// containerLenUnknown is length returned from Read(Map|Array)Len
// when a format doesn't know apiori.
// For example, json doesn't pre-determine the length of a container (sequence/map).
containerLenUnknown = -1
// containerLenNil is length returned from Read(Map|Array)Len
// when a 'nil' was encountered in the stream.
containerLenNil = math.MinInt32
// [N]byte is handled by converting to []byte first,
// and sending to the dedicated fast-path function for []byte.
//
// Code exists in case our understanding is wrong.
// keep the defensive code behind this flag, so we can remove/hide it if needed.
// For now, we enable the defensive code (ie set it to true).
handleBytesWithinKArray = true
// Support encoding.(Binary|Text)(Unm|M)arshaler.
// This constant flag will enable or disable it.
supportMarshalInterfaces = true
// bytesFreeListNoCache is used for debugging, when we want to skip using a cache of []byte.
bytesFreeListNoCache = false
// size of the cacheline: defaulting to value for archs: amd64, arm64, 386
// should use "runtime/internal/sys".CacheLineSize, but that is not exposed.
cacheLineSize = 64
wordSizeBits = 32 << (^uint(0) >> 63) // strconv.IntSize
wordSize = wordSizeBits / 8
// MARKER: determines whether to skip calling fastpath(En|De)codeTypeSwitch.
// Calling the fastpath switch in encode() or decode() could be redundant,
// as we still have to introspect it again within fnLoad
// to determine the function to use for values of that type.
skipFastpathTypeSwitchInDirectCall = false
)
// keep in sync with
// $GOROOT/src/cmd/compile/internal/gc/reflect.go: MAXKEYSIZE, MAXELEMSIZE
// $GOROOT/src/runtime/map.go: maxKeySize, maxElemSize
// $GOROOT/src/reflect/type.go: maxKeySize, maxElemSize
//
// We use these to determine whether the type is stored indirectly in the map or not.
const (
mapMaxKeySize = 128
mapMaxElemSize = 128
)
const cpu32Bit = ^uint(0)>>32 == 0
type rkind byte
const (
rkindPtr = byte(reflect.Ptr)
rkindString = byte(reflect.String)
rkindChan = byte(reflect.Chan)
)
type mapKeyFastKind uint8
const (
mapKeyFastKind32 = iota + 1
mapKeyFastKind32ptr
mapKeyFastKind64
mapKeyFastKind64ptr
mapKeyFastKindStr
)
var (
must mustHdl
halt panicHdl
digitCharBitset bitset256
numCharBitset bitset256
whitespaceCharBitset bitset256
asciiAlphaNumBitset bitset256
// numCharWithExpBitset64 bitset64
// numCharNoExpBitset64 bitset64
// whitespaceCharBitset64 bitset64
// refBitset sets bit for all kinds which are direct internal references
refBitset bitset32
// isnilBitset sets bit for all kinds which can be compared to nil
isnilBitset bitset32
// scalarBitset sets bit for all kinds which are scalars/primitives and thus immutable
scalarBitset bitset32
mapKeyFastKindVals [32]mapKeyFastKind
// codecgen is set to true by codecgen, so that tests, etc can use this information as needed.
codecgen bool
oneByteArr [1]byte
zeroByteSlice = oneByteArr[:0:0]
eofReader devNullReader
)
var (
errMapTypeNotMapKind = errors.New("MapType MUST be of Map Kind")
errSliceTypeNotSliceKind = errors.New("SliceType MUST be of Slice Kind")
errExtFnWriteExtUnsupported = errors.New("BytesExt.WriteExt is not supported")
errExtFnReadExtUnsupported = errors.New("BytesExt.ReadExt is not supported")
errExtFnConvertExtUnsupported = errors.New("InterfaceExt.ConvertExt is not supported")
errExtFnUpdateExtUnsupported = errors.New("InterfaceExt.UpdateExt is not supported")
errPanicUndefined = errors.New("panic: undefined error")
errHandleInited = errors.New("cannot modify initialized Handle")
errNoFormatHandle = errors.New("no handle (cannot identify format)")
)
var pool4tiload = sync.Pool{
New: func() interface{} {
return &typeInfoLoad{
etypes: make([]uintptr, 0, 4),
sfis: make([]structFieldInfo, 0, 4),
sfiNames: make(map[string]uint16, 4),
}
},
}
func init() {
xx := func(f mapKeyFastKind, k ...reflect.Kind) {
for _, v := range k {
mapKeyFastKindVals[byte(v)&31] = f // 'v % 32' equal to 'v & 31'
}
}
var f mapKeyFastKind
f = mapKeyFastKind64
if wordSizeBits == 32 {
f = mapKeyFastKind32
}
xx(f, reflect.Int, reflect.Uint, reflect.Uintptr)
f = mapKeyFastKind64ptr
if wordSizeBits == 32 {
f = mapKeyFastKind32ptr
}
xx(f, reflect.Ptr)
xx(mapKeyFastKindStr, reflect.String)
xx(mapKeyFastKind32, reflect.Uint32, reflect.Int32, reflect.Float32)
xx(mapKeyFastKind64, reflect.Uint64, reflect.Int64, reflect.Float64)
scalarBitset.
set(byte(reflect.Bool)).
set(byte(reflect.Int)).
set(byte(reflect.Int8)).
set(byte(reflect.Int16)).
set(byte(reflect.Int32)).
set(byte(reflect.Int64)).
set(byte(reflect.Uint)).
set(byte(reflect.Uint8)).
set(byte(reflect.Uint16)).
set(byte(reflect.Uint32)).
set(byte(reflect.Uint64)).
set(byte(reflect.Uintptr)).
set(byte(reflect.Float32)).
set(byte(reflect.Float64)).
set(byte(reflect.Complex64)).
set(byte(reflect.Complex128)).
set(byte(reflect.String))
// MARKER: reflect.Array is not a scalar, as its contents can be modified
refBitset.
set(byte(reflect.Map)).
set(byte(reflect.Ptr)).
set(byte(reflect.Func)).
set(byte(reflect.Chan)).
set(byte(reflect.UnsafePointer))
isnilBitset = refBitset
isnilBitset.
set(byte(reflect.Interface)).
set(byte(reflect.Slice))
for i := byte(0); i <= utf8.RuneSelf; i++ {
if (i >= '0' && i <= '9') || (i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z') {
asciiAlphaNumBitset.set(i)
}
switch i {
case ' ', '\t', '\r', '\n':
whitespaceCharBitset.set(i)
// whitespaceCharBitset64.set(i)
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
digitCharBitset.set(i)
numCharBitset.set(i)
// numCharWithExpBitset64.set(i - 42)
// numCharNoExpBitset64.set(i)
case '.', '+', '-':
numCharBitset.set(i)
// numCharWithExpBitset64.set(i - 42)
// numCharNoExpBitset64.set(i)
case 'e', 'E':
numCharBitset.set(i)
// numCharWithExpBitset64.set(i - 42)
}
}
}
type handleFlag uint8
const (
initedHandleFlag handleFlag = 1 << iota
binaryHandleFlag
jsonHandleFlag
)
type clsErr struct {
err error // error on closing
closed bool // is it closed?
}
type charEncoding uint8
const (
_ charEncoding = iota // make 0 unset
cUTF8
cUTF16LE
cUTF16BE
cUTF32LE
cUTF32BE
// Deprecated: not a true char encoding value
cRAW charEncoding = 255
)
// valueType is the stream type
type valueType uint8
const (
valueTypeUnset valueType = iota
valueTypeNil
valueTypeInt
valueTypeUint
valueTypeFloat
valueTypeBool
valueTypeString
valueTypeSymbol
valueTypeBytes
valueTypeMap
valueTypeArray
valueTypeTime
valueTypeExt
// valueTypeInvalid = 0xff
)
var valueTypeStrings = [...]string{
"Unset",
"Nil",
"Int",
"Uint",
"Float",
"Bool",
"String",
"Symbol",
"Bytes",
"Map",
"Array",
"Timestamp",
"Ext",
}
func (x valueType) String() string {
if int(x) < len(valueTypeStrings) {
return valueTypeStrings[x]
}
return strconv.FormatInt(int64(x), 10)
}
// seqType is no longer used, as we now use dedicated methods for decoding slice vs chan vs array
// type seqType uint8
// const (
// _ seqType = iota
// seqTypeArray
// seqTypeSlice
// // seqTypeChan
// )
// note that containerMapStart and containerArraySend are not sent.
// This is because the ReadXXXStart and EncodeXXXStart already does these.
type containerState uint8
const (
_ containerState = iota
containerMapStart
containerMapKey
containerMapValue
containerMapEnd
containerArrayStart
containerArrayElem
containerArrayEnd
)
// do not recurse if a containing type refers to an embedded type
// which refers back to its containing type (via a pointer).
// The second time this back-reference happens, break out,
// so as not to cause an infinite loop.
const rgetMaxRecursion = 2
// fauxUnion is used to keep track of the primitives decoded.
//
// Without it, we would have to decode each primitive and wrap it
// in an interface{}, causing an allocation.
// In this model, the primitives are decoded in a "pseudo-atomic" fashion,
// so we can rest assured that no other decoding happens while these
// primitives are being decoded.
//
// maps and arrays are not handled by this mechanism.
type fauxUnion struct {
// r RawExt // used for RawExt, uint, []byte.
// primitives below
u uint64
i int64
f float64
l []byte
s string
// ---- cpu cache line boundary?
t time.Time
b bool
// state
v valueType
}
// typeInfoLoad is a transient object used while loading up a typeInfo.
type typeInfoLoad struct {
etypes []uintptr
sfis []structFieldInfo
sfiNames map[string]uint16
}
func (x *typeInfoLoad) reset() {
x.etypes = x.etypes[:0]
x.sfis = x.sfis[:0]
for k := range x.sfiNames { // optimized to zero the map
delete(x.sfiNames, k)
}
}
// mirror json.Marshaler and json.Unmarshaler here,
// so we don't import the encoding/json package
type jsonMarshaler interface {
MarshalJSON() ([]byte, error)
}
type jsonUnmarshaler interface {
UnmarshalJSON([]byte) error
}
type isZeroer interface {
IsZero() bool
}
type isCodecEmptyer interface {
IsCodecEmpty() bool
}
type codecError struct {
err error
name string
pos int
encode bool
}
func (e *codecError) Cause() error {
return e.err
}
func (e *codecError) Error() string {
if e.encode {
return fmt.Sprintf("%s encode error: %v", e.name, e.err)
}
return fmt.Sprintf("%s decode error [pos %d]: %v", e.name, e.pos, e.err)
}
func wrapCodecErr(in error, name string, numbytesread int, encode bool) (out error) {
if x, ok := in.(*codecError); ok && x.pos == numbytesread && x.name == name && x.encode == encode {
return in
}
return &codecError{in, name, numbytesread, encode}
}
var (
bigen bigenHelper
bigenstd = binary.BigEndian
structInfoFieldName = "_struct"
mapStrIntfTyp = reflect.TypeOf(map[string]interface{}(nil))
mapIntfIntfTyp = reflect.TypeOf(map[interface{}]interface{}(nil))
intfSliceTyp = reflect.TypeOf([]interface{}(nil))
intfTyp = intfSliceTyp.Elem()
reflectValTyp = reflect.TypeOf((*reflect.Value)(nil)).Elem()
stringTyp = reflect.TypeOf("")
timeTyp = reflect.TypeOf(time.Time{})
rawExtTyp = reflect.TypeOf(RawExt{})
rawTyp = reflect.TypeOf(Raw{})
uintptrTyp = reflect.TypeOf(uintptr(0))
uint8Typ = reflect.TypeOf(uint8(0))
uint8SliceTyp = reflect.TypeOf([]uint8(nil))
uintTyp = reflect.TypeOf(uint(0))
intTyp = reflect.TypeOf(int(0))
mapBySliceTyp = reflect.TypeOf((*MapBySlice)(nil)).Elem()
binaryMarshalerTyp = reflect.TypeOf((*encoding.BinaryMarshaler)(nil)).Elem()
binaryUnmarshalerTyp = reflect.TypeOf((*encoding.BinaryUnmarshaler)(nil)).Elem()
textMarshalerTyp = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
textUnmarshalerTyp = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
jsonMarshalerTyp = reflect.TypeOf((*jsonMarshaler)(nil)).Elem()
jsonUnmarshalerTyp = reflect.TypeOf((*jsonUnmarshaler)(nil)).Elem()
selferTyp = reflect.TypeOf((*Selfer)(nil)).Elem()
missingFielderTyp = reflect.TypeOf((*MissingFielder)(nil)).Elem()
iszeroTyp = reflect.TypeOf((*isZeroer)(nil)).Elem()
isCodecEmptyerTyp = reflect.TypeOf((*isCodecEmptyer)(nil)).Elem()
uint8TypId = rt2id(uint8Typ)
uint8SliceTypId = rt2id(uint8SliceTyp)
rawExtTypId = rt2id(rawExtTyp)
rawTypId = rt2id(rawTyp)
intfTypId = rt2id(intfTyp)
timeTypId = rt2id(timeTyp)
stringTypId = rt2id(stringTyp)
mapStrIntfTypId = rt2id(mapStrIntfTyp)
mapIntfIntfTypId = rt2id(mapIntfIntfTyp)
intfSliceTypId = rt2id(intfSliceTyp)
// mapBySliceTypId = rt2id(mapBySliceTyp)
intBitsize = uint8(intTyp.Bits())
uintBitsize = uint8(uintTyp.Bits())
// bsAll0x00 = []byte{0, 0, 0, 0, 0, 0, 0, 0}
bsAll0xff = []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
chkOvf checkOverflow
)
var defTypeInfos = NewTypeInfos([]string{"codec", "json"})
// SelfExt is a sentinel extension signifying that types
// registered with it SHOULD be encoded and decoded
// based on the native mode of the format.
//
// This allows users to define a tag for an extension,
// but signify that the types should be encoded/decoded as the native encoding.
// This way, users need not also define how to encode or decode the extension.
var SelfExt = &extFailWrapper{}
// Selfer defines methods by which a value can encode or decode itself.
//
// Any type which implements Selfer will be able to encode or decode itself.
// Consequently, during (en|de)code, this takes precedence over
// (text|binary)(M|Unm)arshal or extension support.
//
// By definition, it is not allowed for a Selfer to directly call Encode or Decode on itself.
// If that is done, Encode/Decode will rightfully fail with a Stack Overflow style error.
// For example, the snippet below will cause such an error.
// type testSelferRecur struct{}
// func (s *testSelferRecur) CodecEncodeSelf(e *Encoder) { e.MustEncode(s) }
// func (s *testSelferRecur) CodecDecodeSelf(d *Decoder) { d.MustDecode(s) }
//
// Note: *the first set of bytes of any value MUST NOT represent nil in the format*.
// This is because, during each decode, we first check the the next set of bytes
// represent nil, and if so, we just set the value to nil.
type Selfer interface {
CodecEncodeSelf(*Encoder)
CodecDecodeSelf(*Decoder)
}
// MissingFielder defines the interface allowing structs to internally decode or encode
// values which do not map to struct fields.
//
// We expect that this interface is bound to a pointer type (so the mutation function works).
//
// A use-case is if a version of a type unexports a field, but you want compatibility between
// both versions during encoding and decoding.
//
// Note that the interface is completely ignored during codecgen.
type MissingFielder interface {
// CodecMissingField is called to set a missing field and value pair.
//
// It returns true if the missing field was set on the struct.
CodecMissingField(field []byte, value interface{}) bool
// CodecMissingFields returns the set of fields which are not struct fields.
//
// Note that the returned map may be mutated by the caller.
CodecMissingFields() map[string]interface{}
}
// MapBySlice is a tag interface that denotes the slice or array value should encode as a map
// in the stream, and can be decoded from a map in the stream.
//
// The slice or array must contain a sequence of key-value pairs.
// The length of the slice or array must be even (fully divisible by 2).
//
// This affords storing a map in a specific sequence in the stream.
//
// Example usage:
// type T1 []string // or []int or []Point or any other "slice" type
// func (_ T1) MapBySlice{} // T1 now implements MapBySlice, and will be encoded as a map
// type T2 struct { KeyValues T1 }
//
// var kvs = []string{"one", "1", "two", "2", "three", "3"}
// var v2 = T2{ KeyValues: T1(kvs) }
// // v2 will be encoded like the map: {"KeyValues": {"one": "1", "two": "2", "three": "3"} }
//
// The support of MapBySlice affords the following:
// - A slice or array type which implements MapBySlice will be encoded as a map
// - A slice can be decoded from a map in the stream
type MapBySlice interface {
MapBySlice()
}
// BasicHandle encapsulates the common options and extension functions.
//
// Deprecated: DO NOT USE DIRECTLY. EXPORTED FOR GODOC BENEFIT. WILL BE REMOVED.
type BasicHandle struct {
// BasicHandle is always a part of a different type.
// It doesn't have to fit into it own cache lines.
// TypeInfos is used to get the type info for any type.
//
// If not configured, the default TypeInfos is used, which uses struct tag keys: codec, json
TypeInfos *TypeInfos
// Note: BasicHandle is not comparable, due to these slices here (extHandle, intf2impls).
// If *[]T is used instead, this becomes comparable, at the cost of extra indirection.
// Thses slices are used all the time, so keep as slices (not pointers).
extHandle
// these are used during runtime.
// At init time, they should have nothing in them.
rtidFns atomicRtidFnSlice
rtidFnsNoExt atomicRtidFnSlice
// ---- cache line
DecodeOptions
// ---- cache line
EncodeOptions
intf2impls
mu sync.Mutex
inited uint32 // holds if inited, and also handle flags (binary encoding, json handler, etc)
RPCOptions
// TimeNotBuiltin configures whether time.Time should be treated as a builtin type.
//
// All Handlers should know how to encode/decode time.Time as part of the core
// format specification, or as a standard extension defined by the format.
//
// However, users can elect to handle time.Time as a custom extension, or via the
// standard library's encoding.Binary(M|Unm)arshaler or Text(M|Unm)arshaler interface.
// To elect this behavior, users can set TimeNotBuiltin=true.
//
// Note: Setting TimeNotBuiltin=true can be used to enable the legacy behavior
// (for Cbor and Msgpack), where time.Time was not a builtin supported type.
//
// Note: DO NOT CHANGE AFTER FIRST USE.
//
// Once a Handle has been initialized (used), do not modify this option. It will be ignored.
TimeNotBuiltin bool
// timeBuiltin is initialized from TimeNotBuiltin, and used internally.
// once initialized, it cannot be changed, as the function for encoding/decoding time.Time
// will have been cached and the TimeNotBuiltin value will not be consulted thereafter.
timeBuiltin bool
// ExplicitRelease configures whether Release() is implicitly called after an encode or
// decode call.
//
// If you will hold onto an Encoder or Decoder for re-use, by calling Reset(...)
// on it or calling (Must)Encode repeatedly into a given []byte or io.Writer,
// then you do not want it to be implicitly closed after each Encode/Decode call.
// Doing so will unnecessarily return resources to the shared pool, only for you to
// grab them right after again to do another Encode/Decode call.
//
// Instead, you configure ExplicitRelease=true, and you explicitly call Release() when
// you are truly done.
//
// As an alternative, you can explicitly set a finalizer - so its resources
// are returned to the shared pool before it is garbage-collected. Do it as below:
// runtime.SetFinalizer(e, (*Encoder).Release)
// runtime.SetFinalizer(d, (*Decoder).Release)
//
// Deprecated: This is not longer used as pools are only used for long-lived objects
// which are shared across goroutines.
// Setting this value has no effect. It is maintained for backward compatibility.
ExplicitRelease bool
// ---- cache line
}
// initHandle does a one-time initialization of the handle.
// After this is run, do not modify the Handle, as some modifications are ignored
// e.g. extensions, registered interfaces, TimeNotBuiltIn, etc
func initHandle(hh Handle) {
x := hh.getBasicHandle()
// ** We need to simulate once.Do, to ensure no data race within the block.
// ** Consequently, below would not work.
// if atomic.CompareAndSwapUint32(&x.inited, 0, 1) {
// x.be = hh.isBinary()
// _, x.js = hh.(*JsonHandle)
// x.n = hh.Name()[0]
// }
// simulate once.Do using our own stored flag and mutex as a CompareAndSwap
// is not sufficient, since a race condition can occur within init(Handle) function.
// init is made noinline, so that this function can be inlined by its caller.
if atomic.LoadUint32(&x.inited) == 0 {
x.initHandle(hh)
}
}
func (x *BasicHandle) basicInit() {
x.rtidFns.store(nil)
x.rtidFnsNoExt.store(nil)
x.timeBuiltin = !x.TimeNotBuiltin
}
func (x *BasicHandle) init() {}
func (x *BasicHandle) isInited() bool {
return atomic.LoadUint32(&x.inited) != 0
}
// clearInited: DANGEROUS - only use in testing, etc
func (x *BasicHandle) clearInited() {
atomic.StoreUint32(&x.inited, 0)
}
// TimeBuiltin returns whether time.Time OOTB support is used,
// based on the initial configuration of TimeNotBuiltin
func (x *BasicHandle) TimeBuiltin() bool {
return x.timeBuiltin
}
func (x *BasicHandle) isJs() bool {
return handleFlag(x.inited)&jsonHandleFlag != 0
}
func (x *BasicHandle) isBe() bool {
return handleFlag(x.inited)&binaryHandleFlag != 0
}
//go:noinline
func (x *BasicHandle) initHandle(hh Handle) {
// make it uninlineable, as it is called at most once
x.mu.Lock()
defer x.mu.Unlock() // use defer, as halt may panic below
if x.inited == 0 {
var f = initedHandleFlag
if hh.isBinary() {
f |= binaryHandleFlag
}
if _, b := hh.(*JsonHandle); b {
f |= jsonHandleFlag
}
// ensure MapType and SliceType are of correct type
if x.MapType != nil && x.MapType.Kind() != reflect.Map {
halt.onerror(errMapTypeNotMapKind)
}
if x.SliceType != nil && x.SliceType.Kind() != reflect.Slice {
halt.onerror(errSliceTypeNotSliceKind)
}
x.basicInit()
hh.init()
atomic.StoreUint32(&x.inited, uint32(f))
}
}
func (x *BasicHandle) getBasicHandle() *BasicHandle {
return x
}
func (x *BasicHandle) getTypeInfo(rtid uintptr, rt reflect.Type) (pti *typeInfo) {
if x.TypeInfos == nil {
return defTypeInfos.get(rtid, rt)
}
return x.TypeInfos.get(rtid, rt)
}
func findRtidFn(s []codecRtidFn, rtid uintptr) (i uint, fn *codecFn) {
// binary search. adapted from sort/search.go.
// Note: we use goto (instead of for loop) so this can be inlined.
// h, i, j := 0, 0, len(s)
var h uint // var h, i uint
var j = uint(len(s))
LOOP:
if i < j {
h = (i + j) >> 1 // avoid overflow when computing h // h = i + (j-i)/2
if s[h].rtid < rtid {
i = h + 1
} else {
j = h
}
goto LOOP
}
if i < uint(len(s)) && s[i].rtid == rtid {
fn = s[i].fn
}
return
}
func (x *BasicHandle) fn(rt reflect.Type) (fn *codecFn) {
return x.fnVia(rt, &x.rtidFns, true)
}
func (x *BasicHandle) fnNoExt(rt reflect.Type) (fn *codecFn) {
return x.fnVia(rt, &x.rtidFnsNoExt, false)
}
func (x *BasicHandle) fnVia(rt reflect.Type, fs *atomicRtidFnSlice, checkExt bool) (fn *codecFn) {
rtid := rt2id(rt)
sp := fs.load()
if sp != nil {
if _, fn = findRtidFn(sp, rtid); fn != nil {
return
}
}
fn = x.fnLoad(rt, rtid, checkExt)
x.mu.Lock()
sp = fs.load()
// since this is an atomic load/store, we MUST use a different array each time,
// else we have a data race when a store is happening simultaneously with a findRtidFn call.
if sp == nil {
sp = []codecRtidFn{{rtid, fn}}
fs.store(sp)
} else {
idx, fn2 := findRtidFn(sp, rtid)
if fn2 == nil {
sp2 := make([]codecRtidFn, len(sp)+1)
copy(sp2[idx+1:], sp[idx:])
copy(sp2, sp[:idx])
sp2[idx] = codecRtidFn{rtid, fn}
fs.store(sp2)
}
}
x.mu.Unlock()
return
}
func (x *BasicHandle) fnLoad(rt reflect.Type, rtid uintptr, checkExt bool) (fn *codecFn) {
fn = new(codecFn)
fi := &(fn.i)
ti := x.getTypeInfo(rtid, rt)
fi.ti = ti
rk := reflect.Kind(ti.kind)
// anything can be an extension except the built-in ones: time, raw and rawext.
// ensure we check for these types, then if extension, before checking if
// it implementes one of the pre-declared interfaces.
fi.addrDf = true
fi.addrEf = true
if rtid == timeTypId && x.timeBuiltin {
fn.fe = (*Encoder).kTime
fn.fd = (*Decoder).kTime
} else if rtid == rawTypId {
fn.fe = (*Encoder).raw
fn.fd = (*Decoder).raw
} else if rtid == rawExtTypId {
fn.fe = (*Encoder).rawExt
fn.fd = (*Decoder).rawExt
fi.addrD = true
fi.addrE = true
} else if xfFn := x.getExt(rtid, checkExt); xfFn != nil {
fi.xfTag, fi.xfFn = xfFn.tag, xfFn.ext
fn.fe = (*Encoder).ext
fn.fd = (*Decoder).ext
fi.addrD = true
if rk == reflect.Struct || rk == reflect.Array {
fi.addrE = true
}
} else if ti.flagSelfer || ti.flagSelferPtr {
fn.fe = (*Encoder).selferMarshal
fn.fd = (*Decoder).selferUnmarshal
fi.addrD = ti.flagSelferPtr
fi.addrE = ti.flagSelferPtr
} else if supportMarshalInterfaces && x.isBe() &&
(ti.flagBinaryMarshaler || ti.flagBinaryMarshalerPtr) &&
(ti.flagBinaryUnmarshaler || ti.flagBinaryUnmarshalerPtr) {
fn.fe = (*Encoder).binaryMarshal
fn.fd = (*Decoder).binaryUnmarshal
fi.addrD = ti.flagBinaryUnmarshalerPtr
fi.addrE = ti.flagBinaryMarshalerPtr
} else if supportMarshalInterfaces && !x.isBe() && x.isJs() &&
(ti.flagJsonMarshaler || ti.flagJsonMarshalerPtr) &&
(ti.flagJsonUnmarshaler || ti.flagJsonUnmarshalerPtr) {
//If JSON, we should check JSONMarshal before textMarshal
fn.fe = (*Encoder).jsonMarshal
fn.fd = (*Decoder).jsonUnmarshal
fi.addrD = ti.flagJsonUnmarshalerPtr
fi.addrE = ti.flagJsonMarshalerPtr
} else if supportMarshalInterfaces && !x.isBe() &&
(ti.flagTextMarshaler || ti.flagTextMarshalerPtr) &&
(ti.flagTextUnmarshaler || ti.flagTextUnmarshalerPtr) {