1// Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved. 2// Use of this source code is governed by a MIT license found in the LICENSE file. 3 4package codec 5 6// Contains code shared by both encode and decode. 7 8// Some shared ideas around encoding/decoding 9// ------------------------------------------ 10// 11// If an interface{} is passed, we first do a type assertion to see if it is 12// a primitive type or a map/slice of primitive types, and use a fastpath to handle it. 13// 14// If we start with a reflect.Value, we are already in reflect.Value land and 15// will try to grab the function for the underlying Type and directly call that function. 16// This is more performant than calling reflect.Value.Interface(). 17// 18// This still helps us bypass many layers of reflection, and give best performance. 19// 20// Containers 21// ------------ 22// Containers in the stream are either associative arrays (key-value pairs) or 23// regular arrays (indexed by incrementing integers). 24// 25// Some streams support indefinite-length containers, and use a breaking 26// byte-sequence to denote that the container has come to an end. 27// 28// Some streams also are text-based, and use explicit separators to denote the 29// end/beginning of different values. 30// 31// During encode, we use a high-level condition to determine how to iterate through 32// the container. That decision is based on whether the container is text-based (with 33// separators) or binary (without separators). If binary, we do not even call the 34// encoding of separators. 35// 36// During decode, we use a different high-level condition to determine how to iterate 37// through the containers. That decision is based on whether the stream contained 38// a length prefix, or if it used explicit breaks. If length-prefixed, we assume that 39// it has to be binary, and we do not even try to read separators. 40// 41// Philosophy 42// ------------ 43// On decode, this codec will update containers appropriately: 44// - If struct, update fields from stream into fields of struct. 45// If field in stream not found in struct, handle appropriately (based on option). 46// If a struct field has no corresponding value in the stream, leave it AS IS. 47// If nil in stream, set value to nil/zero value. 48// - If map, update map from stream. 49// If the stream value is NIL, set the map to nil. 50// - if slice, try to update up to length of array in stream. 51// if container len is less than stream array length, 52// and container cannot be expanded, handled (based on option). 53// This means you can decode 4-element stream array into 1-element array. 54// 55// ------------------------------------ 56// On encode, user can specify omitEmpty. This means that the value will be omitted 57// if the zero value. The problem may occur during decode, where omitted values do not affect 58// the value being decoded into. This means that if decoding into a struct with an 59// int field with current value=5, and the field is omitted in the stream, then after 60// decoding, the value will still be 5 (not 0). 61// omitEmpty only works if you guarantee that you always decode into zero-values. 62// 63// ------------------------------------ 64// We could have truncated a map to remove keys not available in the stream, 65// or set values in the struct which are not in the stream to their zero values. 66// We decided against it because there is no efficient way to do it. 67// We may introduce it as an option later. 68// However, that will require enabling it for both runtime and code generation modes. 69// 70// To support truncate, we need to do 2 passes over the container: 71// map 72// - first collect all keys (e.g. in k1) 73// - for each key in stream, mark k1 that the key should not be removed 74// - after updating map, do second pass and call delete for all keys in k1 which are not marked 75// struct: 76// - for each field, track the *typeInfo s1 77// - iterate through all s1, and for each one not marked, set value to zero 78// - this involves checking the possible anonymous fields which are nil ptrs. 79// too much work. 80// 81// ------------------------------------------ 82// Error Handling is done within the library using panic. 83// 84// This way, the code doesn't have to keep checking if an error has happened, 85// and we don't have to keep sending the error value along with each call 86// or storing it in the En|Decoder and checking it constantly along the way. 87// 88// The disadvantage is that small functions which use panics cannot be inlined. 89// The code accounts for that by only using panics behind an interface; 90// since interface calls cannot be inlined, this is irrelevant. 91// 92// We considered storing the error is En|Decoder. 93// - once it has its err field set, it cannot be used again. 94// - panicing will be optional, controlled by const flag. 95// - code should always check error first and return early. 96// We eventually decided against it as it makes the code clumsier to always 97// check for these error conditions. 98 99import ( 100 "bytes" 101 "encoding" 102 "encoding/binary" 103 "errors" 104 "fmt" 105 "io" 106 "math" 107 "reflect" 108 "sort" 109 "strconv" 110 "strings" 111 "sync" 112 "sync/atomic" 113 "time" 114) 115 116const ( 117 scratchByteArrayLen = 32 118 // initCollectionCap = 16 // 32 is defensive. 16 is preferred. 119 120 // Support encoding.(Binary|Text)(Unm|M)arshaler. 121 // This constant flag will enable or disable it. 122 supportMarshalInterfaces = true 123 124 // for debugging, set this to false, to catch panic traces. 125 // Note that this will always cause rpc tests to fail, since they need io.EOF sent via panic. 126 recoverPanicToErr = true 127 128 // arrayCacheLen is the length of the cache used in encoder or decoder for 129 // allowing zero-alloc initialization. 130 // arrayCacheLen = 8 131 132 // size of the cacheline: defaulting to value for archs: amd64, arm64, 386 133 // should use "runtime/internal/sys".CacheLineSize, but that is not exposed. 134 cacheLineSize = 64 135 136 wordSizeBits = 32 << (^uint(0) >> 63) // strconv.IntSize 137 wordSize = wordSizeBits / 8 138 139 // so structFieldInfo fits into 8 bytes 140 maxLevelsEmbedding = 14 141 142 // useFinalizers=true configures finalizers to release pool'ed resources 143 // acquired by Encoder/Decoder during their GC. 144 // 145 // Note that calling SetFinalizer is always expensive, 146 // as code must be run on the systemstack even for SetFinalizer(t, nil). 147 // 148 // We document that folks SHOULD call Release() when done, or they can 149 // explicitly call SetFinalizer themselves e.g. 150 // runtime.SetFinalizer(e, (*Encoder).Release) 151 // runtime.SetFinalizer(d, (*Decoder).Release) 152 useFinalizers = false 153) 154 155var oneByteArr [1]byte 156var zeroByteSlice = oneByteArr[:0:0] 157 158var codecgen bool 159 160var refBitset bitset256 161var pool pooler 162var panicv panicHdl 163 164func init() { 165 pool.init() 166 167 refBitset.set(byte(reflect.Map)) 168 refBitset.set(byte(reflect.Ptr)) 169 refBitset.set(byte(reflect.Func)) 170 refBitset.set(byte(reflect.Chan)) 171} 172 173type clsErr struct { 174 closed bool // is it closed? 175 errClosed error // error on closing 176} 177 178// type entryType uint8 179 180// const ( 181// entryTypeBytes entryType = iota // make this 0, so a comparison is cheap 182// entryTypeIo 183// entryTypeBufio 184// entryTypeUnset = 255 185// ) 186 187type charEncoding uint8 188 189const ( 190 _ charEncoding = iota // make 0 unset 191 cUTF8 192 cUTF16LE 193 cUTF16BE 194 cUTF32LE 195 cUTF32BE 196 // Deprecated: not a true char encoding value 197 cRAW charEncoding = 255 198) 199 200// valueType is the stream type 201type valueType uint8 202 203const ( 204 valueTypeUnset valueType = iota 205 valueTypeNil 206 valueTypeInt 207 valueTypeUint 208 valueTypeFloat 209 valueTypeBool 210 valueTypeString 211 valueTypeSymbol 212 valueTypeBytes 213 valueTypeMap 214 valueTypeArray 215 valueTypeTime 216 valueTypeExt 217 218 // valueTypeInvalid = 0xff 219) 220 221var valueTypeStrings = [...]string{ 222 "Unset", 223 "Nil", 224 "Int", 225 "Uint", 226 "Float", 227 "Bool", 228 "String", 229 "Symbol", 230 "Bytes", 231 "Map", 232 "Array", 233 "Timestamp", 234 "Ext", 235} 236 237func (x valueType) String() string { 238 if int(x) < len(valueTypeStrings) { 239 return valueTypeStrings[x] 240 } 241 return strconv.FormatInt(int64(x), 10) 242} 243 244type seqType uint8 245 246const ( 247 _ seqType = iota 248 seqTypeArray 249 seqTypeSlice 250 seqTypeChan 251) 252 253// note that containerMapStart and containerArraySend are not sent. 254// This is because the ReadXXXStart and EncodeXXXStart already does these. 255type containerState uint8 256 257const ( 258 _ containerState = iota 259 260 containerMapStart // slot left open, since Driver method already covers it 261 containerMapKey 262 containerMapValue 263 containerMapEnd 264 containerArrayStart // slot left open, since Driver methods already cover it 265 containerArrayElem 266 containerArrayEnd 267) 268 269// // sfiIdx used for tracking where a (field/enc)Name is seen in a []*structFieldInfo 270// type sfiIdx struct { 271// name string 272// index int 273// } 274 275// do not recurse if a containing type refers to an embedded type 276// which refers back to its containing type (via a pointer). 277// The second time this back-reference happens, break out, 278// so as not to cause an infinite loop. 279const rgetMaxRecursion = 2 280 281// Anecdotally, we believe most types have <= 12 fields. 282// - even Java's PMD rules set TooManyFields threshold to 15. 283// However, go has embedded fields, which should be regarded as 284// top level, allowing structs to possibly double or triple. 285// In addition, we don't want to keep creating transient arrays, 286// especially for the sfi index tracking, and the evtypes tracking. 287// 288// So - try to keep typeInfoLoadArray within 2K bytes 289const ( 290 typeInfoLoadArraySfisLen = 16 291 typeInfoLoadArraySfiidxLen = 8 * 112 292 typeInfoLoadArrayEtypesLen = 12 293 typeInfoLoadArrayBLen = 8 * 4 294) 295 296type typeInfoLoad struct { 297 // fNames []string 298 // encNames []string 299 etypes []uintptr 300 sfis []structFieldInfo 301} 302 303type typeInfoLoadArray struct { 304 // fNames [typeInfoLoadArrayLen]string 305 // encNames [typeInfoLoadArrayLen]string 306 sfis [typeInfoLoadArraySfisLen]structFieldInfo 307 sfiidx [typeInfoLoadArraySfiidxLen]byte 308 etypes [typeInfoLoadArrayEtypesLen]uintptr 309 b [typeInfoLoadArrayBLen]byte // scratch - used for struct field names 310} 311 312// mirror json.Marshaler and json.Unmarshaler here, 313// so we don't import the encoding/json package 314 315type jsonMarshaler interface { 316 MarshalJSON() ([]byte, error) 317} 318type jsonUnmarshaler interface { 319 UnmarshalJSON([]byte) error 320} 321 322type isZeroer interface { 323 IsZero() bool 324} 325 326type codecError struct { 327 name string 328 err interface{} 329} 330 331func (e codecError) Cause() error { 332 switch xerr := e.err.(type) { 333 case nil: 334 return nil 335 case error: 336 return xerr 337 case string: 338 return errors.New(xerr) 339 case fmt.Stringer: 340 return errors.New(xerr.String()) 341 default: 342 return fmt.Errorf("%v", e.err) 343 } 344} 345 346func (e codecError) Error() string { 347 return fmt.Sprintf("%s error: %v", e.name, e.err) 348} 349 350// type byteAccepter func(byte) bool 351 352var ( 353 bigen = binary.BigEndian 354 structInfoFieldName = "_struct" 355 356 mapStrIntfTyp = reflect.TypeOf(map[string]interface{}(nil)) 357 mapIntfIntfTyp = reflect.TypeOf(map[interface{}]interface{}(nil)) 358 intfSliceTyp = reflect.TypeOf([]interface{}(nil)) 359 intfTyp = intfSliceTyp.Elem() 360 361 reflectValTyp = reflect.TypeOf((*reflect.Value)(nil)).Elem() 362 363 stringTyp = reflect.TypeOf("") 364 timeTyp = reflect.TypeOf(time.Time{}) 365 rawExtTyp = reflect.TypeOf(RawExt{}) 366 rawTyp = reflect.TypeOf(Raw{}) 367 uintptrTyp = reflect.TypeOf(uintptr(0)) 368 uint8Typ = reflect.TypeOf(uint8(0)) 369 uint8SliceTyp = reflect.TypeOf([]uint8(nil)) 370 uintTyp = reflect.TypeOf(uint(0)) 371 intTyp = reflect.TypeOf(int(0)) 372 373 mapBySliceTyp = reflect.TypeOf((*MapBySlice)(nil)).Elem() 374 375 binaryMarshalerTyp = reflect.TypeOf((*encoding.BinaryMarshaler)(nil)).Elem() 376 binaryUnmarshalerTyp = reflect.TypeOf((*encoding.BinaryUnmarshaler)(nil)).Elem() 377 378 textMarshalerTyp = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem() 379 textUnmarshalerTyp = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem() 380 381 jsonMarshalerTyp = reflect.TypeOf((*jsonMarshaler)(nil)).Elem() 382 jsonUnmarshalerTyp = reflect.TypeOf((*jsonUnmarshaler)(nil)).Elem() 383 384 selferTyp = reflect.TypeOf((*Selfer)(nil)).Elem() 385 missingFielderTyp = reflect.TypeOf((*MissingFielder)(nil)).Elem() 386 iszeroTyp = reflect.TypeOf((*isZeroer)(nil)).Elem() 387 388 uint8TypId = rt2id(uint8Typ) 389 uint8SliceTypId = rt2id(uint8SliceTyp) 390 rawExtTypId = rt2id(rawExtTyp) 391 rawTypId = rt2id(rawTyp) 392 intfTypId = rt2id(intfTyp) 393 timeTypId = rt2id(timeTyp) 394 stringTypId = rt2id(stringTyp) 395 396 mapStrIntfTypId = rt2id(mapStrIntfTyp) 397 mapIntfIntfTypId = rt2id(mapIntfIntfTyp) 398 intfSliceTypId = rt2id(intfSliceTyp) 399 // mapBySliceTypId = rt2id(mapBySliceTyp) 400 401 intBitsize = uint8(intTyp.Bits()) 402 uintBitsize = uint8(uintTyp.Bits()) 403 404 // bsAll0x00 = []byte{0, 0, 0, 0, 0, 0, 0, 0} 405 bsAll0xff = []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff} 406 407 chkOvf checkOverflow 408 409 errNoFieldNameToStructFieldInfo = errors.New("no field name passed to parseStructFieldInfo") 410) 411 412var defTypeInfos = NewTypeInfos([]string{"codec", "json"}) 413 414var immutableKindsSet = [32]bool{ 415 // reflect.Invalid: , 416 reflect.Bool: true, 417 reflect.Int: true, 418 reflect.Int8: true, 419 reflect.Int16: true, 420 reflect.Int32: true, 421 reflect.Int64: true, 422 reflect.Uint: true, 423 reflect.Uint8: true, 424 reflect.Uint16: true, 425 reflect.Uint32: true, 426 reflect.Uint64: true, 427 reflect.Uintptr: true, 428 reflect.Float32: true, 429 reflect.Float64: true, 430 reflect.Complex64: true, 431 reflect.Complex128: true, 432 // reflect.Array 433 // reflect.Chan 434 // reflect.Func: true, 435 // reflect.Interface 436 // reflect.Map 437 // reflect.Ptr 438 // reflect.Slice 439 reflect.String: true, 440 // reflect.Struct 441 // reflect.UnsafePointer 442} 443 444// Selfer defines methods by which a value can encode or decode itself. 445// 446// Any type which implements Selfer will be able to encode or decode itself. 447// Consequently, during (en|de)code, this takes precedence over 448// (text|binary)(M|Unm)arshal or extension support. 449// 450// By definition, it is not allowed for a Selfer to directly call Encode or Decode on itself. 451// If that is done, Encode/Decode will rightfully fail with a Stack Overflow style error. 452// For example, the snippet below will cause such an error. 453// type testSelferRecur struct{} 454// func (s *testSelferRecur) CodecEncodeSelf(e *Encoder) { e.MustEncode(s) } 455// func (s *testSelferRecur) CodecDecodeSelf(d *Decoder) { d.MustDecode(s) } 456// 457// Note: *the first set of bytes of any value MUST NOT represent nil in the format*. 458// This is because, during each decode, we first check the the next set of bytes 459// represent nil, and if so, we just set the value to nil. 460type Selfer interface { 461 CodecEncodeSelf(*Encoder) 462 CodecDecodeSelf(*Decoder) 463} 464 465// MissingFielder defines the interface allowing structs to internally decode or encode 466// values which do not map to struct fields. 467// 468// We expect that this interface is bound to a pointer type (so the mutation function works). 469// 470// A use-case is if a version of a type unexports a field, but you want compatibility between 471// both versions during encoding and decoding. 472// 473// Note that the interface is completely ignored during codecgen. 474type MissingFielder interface { 475 // CodecMissingField is called to set a missing field and value pair. 476 // 477 // It returns true if the missing field was set on the struct. 478 CodecMissingField(field []byte, value interface{}) bool 479 480 // CodecMissingFields returns the set of fields which are not struct fields 481 CodecMissingFields() map[string]interface{} 482} 483 484// MapBySlice is a tag interface that denotes wrapped slice should encode as a map in the stream. 485// The slice contains a sequence of key-value pairs. 486// This affords storing a map in a specific sequence in the stream. 487// 488// Example usage: 489// type T1 []string // or []int or []Point or any other "slice" type 490// func (_ T1) MapBySlice{} // T1 now implements MapBySlice, and will be encoded as a map 491// type T2 struct { KeyValues T1 } 492// 493// var kvs = []string{"one", "1", "two", "2", "three", "3"} 494// var v2 = T2{ KeyValues: T1(kvs) } 495// // v2 will be encoded like the map: {"KeyValues": {"one": "1", "two": "2", "three": "3"} } 496// 497// The support of MapBySlice affords the following: 498// - A slice type which implements MapBySlice will be encoded as a map 499// - A slice can be decoded from a map in the stream 500// - It MUST be a slice type (not a pointer receiver) that implements MapBySlice 501type MapBySlice interface { 502 MapBySlice() 503} 504 505// BasicHandle encapsulates the common options and extension functions. 506// 507// Deprecated: DO NOT USE DIRECTLY. EXPORTED FOR GODOC BENEFIT. WILL BE REMOVED. 508type BasicHandle struct { 509 // BasicHandle is always a part of a different type. 510 // It doesn't have to fit into it own cache lines. 511 512 // TypeInfos is used to get the type info for any type. 513 // 514 // If not configured, the default TypeInfos is used, which uses struct tag keys: codec, json 515 TypeInfos *TypeInfos 516 517 // Note: BasicHandle is not comparable, due to these slices here (extHandle, intf2impls). 518 // If *[]T is used instead, this becomes comparable, at the cost of extra indirection. 519 // Thses slices are used all the time, so keep as slices (not pointers). 520 521 extHandle 522 523 intf2impls 524 525 inited uint32 526 _ uint32 // padding 527 528 // ---- cache line 529 530 RPCOptions 531 532 // TimeNotBuiltin configures whether time.Time should be treated as a builtin type. 533 // 534 // All Handlers should know how to encode/decode time.Time as part of the core 535 // format specification, or as a standard extension defined by the format. 536 // 537 // However, users can elect to handle time.Time as a custom extension, or via the 538 // standard library's encoding.Binary(M|Unm)arshaler or Text(M|Unm)arshaler interface. 539 // To elect this behavior, users can set TimeNotBuiltin=true. 540 // Note: Setting TimeNotBuiltin=true can be used to enable the legacy behavior 541 // (for Cbor and Msgpack), where time.Time was not a builtin supported type. 542 TimeNotBuiltin bool 543 544 // ExplicitRelease configures whether Release() is implicitly called after an encode or 545 // decode call. 546 // 547 // If you will hold onto an Encoder or Decoder for re-use, by calling Reset(...) 548 // on it or calling (Must)Encode repeatedly into a given []byte or io.Writer, 549 // then you do not want it to be implicitly closed after each Encode/Decode call. 550 // Doing so will unnecessarily return resources to the shared pool, only for you to 551 // grab them right after again to do another Encode/Decode call. 552 // 553 // Instead, you configure ExplicitRelease=true, and you explicitly call Release() when 554 // you are truly done. 555 // 556 // As an alternative, you can explicitly set a finalizer - so its resources 557 // are returned to the shared pool before it is garbage-collected. Do it as below: 558 // runtime.SetFinalizer(e, (*Encoder).Release) 559 // runtime.SetFinalizer(d, (*Decoder).Release) 560 ExplicitRelease bool 561 562 be bool // is handle a binary encoding? 563 js bool // is handle javascript handler? 564 n byte // first letter of handle name 565 _ uint16 // padding 566 567 // ---- cache line 568 569 DecodeOptions 570 571 // ---- cache line 572 573 EncodeOptions 574 575 // noBuiltInTypeChecker 576 577 rtidFns atomicRtidFnSlice 578 mu sync.Mutex 579 // r []uintptr // rtids mapped to s above 580} 581 582// basicHandle returns an initialized BasicHandle from the Handle. 583func basicHandle(hh Handle) (x *BasicHandle) { 584 x = hh.getBasicHandle() 585 if atomic.CompareAndSwapUint32(&x.inited, 0, 1) { 586 x.be = hh.isBinary() 587 _, x.js = hh.(*JsonHandle) 588 x.n = hh.Name()[0] 589 } 590 return 591} 592 593func (x *BasicHandle) getBasicHandle() *BasicHandle { 594 return x 595} 596 597func (x *BasicHandle) getTypeInfo(rtid uintptr, rt reflect.Type) (pti *typeInfo) { 598 if x.TypeInfos == nil { 599 return defTypeInfos.get(rtid, rt) 600 } 601 return x.TypeInfos.get(rtid, rt) 602} 603 604func findFn(s []codecRtidFn, rtid uintptr) (i uint, fn *codecFn) { 605 // binary search. adapted from sort/search.go. 606 // Note: we use goto (instead of for loop) so this can be inlined. 607 608 // h, i, j := 0, 0, len(s) 609 var h uint // var h, i uint 610 var j = uint(len(s)) 611LOOP: 612 if i < j { 613 h = i + (j-i)/2 614 if s[h].rtid < rtid { 615 i = h + 1 616 } else { 617 j = h 618 } 619 goto LOOP 620 } 621 if i < uint(len(s)) && s[i].rtid == rtid { 622 fn = s[i].fn 623 } 624 return 625} 626 627func (x *BasicHandle) fn(rt reflect.Type, checkFastpath, checkCodecSelfer bool) (fn *codecFn) { 628 rtid := rt2id(rt) 629 sp := x.rtidFns.load() 630 if sp != nil { 631 if _, fn = findFn(sp, rtid); fn != nil { 632 // xdebugf("<<<< %c: found fn for %v in rtidfns of size: %v", c.n, rt, len(sp)) 633 return 634 } 635 } 636 c := x 637 // xdebugf("#### for %c: load fn for %v in rtidfns of size: %v", c.n, rt, len(sp)) 638 fn = new(codecFn) 639 fi := &(fn.i) 640 ti := c.getTypeInfo(rtid, rt) 641 fi.ti = ti 642 643 rk := reflect.Kind(ti.kind) 644 645 if checkCodecSelfer && (ti.cs || ti.csp) { 646 fn.fe = (*Encoder).selferMarshal 647 fn.fd = (*Decoder).selferUnmarshal 648 fi.addrF = true 649 fi.addrD = ti.csp 650 fi.addrE = ti.csp 651 } else if rtid == timeTypId && !c.TimeNotBuiltin { 652 fn.fe = (*Encoder).kTime 653 fn.fd = (*Decoder).kTime 654 } else if rtid == rawTypId { 655 fn.fe = (*Encoder).raw 656 fn.fd = (*Decoder).raw 657 } else if rtid == rawExtTypId { 658 fn.fe = (*Encoder).rawExt 659 fn.fd = (*Decoder).rawExt 660 fi.addrF = true 661 fi.addrD = true 662 fi.addrE = true 663 } else if xfFn := c.getExt(rtid); xfFn != nil { 664 fi.xfTag, fi.xfFn = xfFn.tag, xfFn.ext 665 fn.fe = (*Encoder).ext 666 fn.fd = (*Decoder).ext 667 fi.addrF = true 668 fi.addrD = true 669 if rk == reflect.Struct || rk == reflect.Array { 670 fi.addrE = true 671 } 672 } else if supportMarshalInterfaces && c.be && (ti.bm || ti.bmp) && (ti.bu || ti.bup) { 673 fn.fe = (*Encoder).binaryMarshal 674 fn.fd = (*Decoder).binaryUnmarshal 675 fi.addrF = true 676 fi.addrD = ti.bup 677 fi.addrE = ti.bmp 678 } else if supportMarshalInterfaces && !c.be && c.js && (ti.jm || ti.jmp) && (ti.ju || ti.jup) { 679 //If JSON, we should check JSONMarshal before textMarshal 680 fn.fe = (*Encoder).jsonMarshal 681 fn.fd = (*Decoder).jsonUnmarshal 682 fi.addrF = true 683 fi.addrD = ti.jup 684 fi.addrE = ti.jmp 685 } else if supportMarshalInterfaces && !c.be && (ti.tm || ti.tmp) && (ti.tu || ti.tup) { 686 fn.fe = (*Encoder).textMarshal 687 fn.fd = (*Decoder).textUnmarshal 688 fi.addrF = true 689 fi.addrD = ti.tup 690 fi.addrE = ti.tmp 691 } else { 692 if fastpathEnabled && checkFastpath && (rk == reflect.Map || rk == reflect.Slice) { 693 if ti.pkgpath == "" { // un-named slice or map 694 if idx := fastpathAV.index(rtid); idx != -1 { 695 fn.fe = fastpathAV[idx].encfn 696 fn.fd = fastpathAV[idx].decfn 697 fi.addrD = true 698 fi.addrF = false 699 } 700 } else { 701 // use mapping for underlying type if there 702 var rtu reflect.Type 703 if rk == reflect.Map { 704 rtu = reflect.MapOf(ti.key, ti.elem) 705 } else { 706 rtu = reflect.SliceOf(ti.elem) 707 } 708 rtuid := rt2id(rtu) 709 if idx := fastpathAV.index(rtuid); idx != -1 { 710 xfnf := fastpathAV[idx].encfn 711 xrt := fastpathAV[idx].rt 712 fn.fe = func(e *Encoder, xf *codecFnInfo, xrv reflect.Value) { 713 xfnf(e, xf, xrv.Convert(xrt)) 714 } 715 fi.addrD = true 716 fi.addrF = false // meaning it can be an address(ptr) or a value 717 xfnf2 := fastpathAV[idx].decfn 718 fn.fd = func(d *Decoder, xf *codecFnInfo, xrv reflect.Value) { 719 if xrv.Kind() == reflect.Ptr { 720 xfnf2(d, xf, xrv.Convert(reflect.PtrTo(xrt))) 721 } else { 722 xfnf2(d, xf, xrv.Convert(xrt)) 723 } 724 } 725 } 726 } 727 } 728 if fn.fe == nil && fn.fd == nil { 729 switch rk { 730 case reflect.Bool: 731 fn.fe = (*Encoder).kBool 732 fn.fd = (*Decoder).kBool 733 case reflect.String: 734 fn.fe = (*Encoder).kString 735 fn.fd = (*Decoder).kString 736 case reflect.Int: 737 fn.fd = (*Decoder).kInt 738 fn.fe = (*Encoder).kInt 739 case reflect.Int8: 740 fn.fe = (*Encoder).kInt8 741 fn.fd = (*Decoder).kInt8 742 case reflect.Int16: 743 fn.fe = (*Encoder).kInt16 744 fn.fd = (*Decoder).kInt16 745 case reflect.Int32: 746 fn.fe = (*Encoder).kInt32 747 fn.fd = (*Decoder).kInt32 748 case reflect.Int64: 749 fn.fe = (*Encoder).kInt64 750 fn.fd = (*Decoder).kInt64 751 case reflect.Uint: 752 fn.fd = (*Decoder).kUint 753 fn.fe = (*Encoder).kUint 754 case reflect.Uint8: 755 fn.fe = (*Encoder).kUint8 756 fn.fd = (*Decoder).kUint8 757 case reflect.Uint16: 758 fn.fe = (*Encoder).kUint16 759 fn.fd = (*Decoder).kUint16 760 case reflect.Uint32: 761 fn.fe = (*Encoder).kUint32 762 fn.fd = (*Decoder).kUint32 763 case reflect.Uint64: 764 fn.fe = (*Encoder).kUint64 765 fn.fd = (*Decoder).kUint64 766 case reflect.Uintptr: 767 fn.fe = (*Encoder).kUintptr 768 fn.fd = (*Decoder).kUintptr 769 case reflect.Float32: 770 fn.fe = (*Encoder).kFloat32 771 fn.fd = (*Decoder).kFloat32 772 case reflect.Float64: 773 fn.fe = (*Encoder).kFloat64 774 fn.fd = (*Decoder).kFloat64 775 case reflect.Invalid: 776 fn.fe = (*Encoder).kInvalid 777 fn.fd = (*Decoder).kErr 778 case reflect.Chan: 779 fi.seq = seqTypeChan 780 fn.fe = (*Encoder).kSlice 781 fn.fd = (*Decoder).kSlice 782 case reflect.Slice: 783 fi.seq = seqTypeSlice 784 fn.fe = (*Encoder).kSlice 785 fn.fd = (*Decoder).kSlice 786 case reflect.Array: 787 fi.seq = seqTypeArray 788 fn.fe = (*Encoder).kSlice 789 fi.addrF = false 790 fi.addrD = false 791 rt2 := reflect.SliceOf(ti.elem) 792 fn.fd = func(d *Decoder, xf *codecFnInfo, xrv reflect.Value) { 793 d.h.fn(rt2, true, false).fd(d, xf, xrv.Slice(0, xrv.Len())) 794 } 795 // fn.fd = (*Decoder).kArray 796 case reflect.Struct: 797 if ti.anyOmitEmpty || ti.mf || ti.mfp { 798 fn.fe = (*Encoder).kStruct 799 } else { 800 fn.fe = (*Encoder).kStructNoOmitempty 801 } 802 fn.fd = (*Decoder).kStruct 803 case reflect.Map: 804 fn.fe = (*Encoder).kMap 805 fn.fd = (*Decoder).kMap 806 case reflect.Interface: 807 // encode: reflect.Interface are handled already by preEncodeValue 808 fn.fd = (*Decoder).kInterface 809 fn.fe = (*Encoder).kErr 810 default: 811 // reflect.Ptr and reflect.Interface are handled already by preEncodeValue 812 fn.fe = (*Encoder).kErr 813 fn.fd = (*Decoder).kErr 814 } 815 } 816 } 817 818 c.mu.Lock() 819 var sp2 []codecRtidFn 820 sp = c.rtidFns.load() 821 if sp == nil { 822 sp2 = []codecRtidFn{{rtid, fn}} 823 c.rtidFns.store(sp2) 824 // xdebugf(">>>> adding rt: %v to rtidfns of size: %v", rt, len(sp2)) 825 // xdebugf(">>>> loading stored rtidfns of size: %v", len(c.rtidFns.load())) 826 } else { 827 idx, fn2 := findFn(sp, rtid) 828 if fn2 == nil { 829 sp2 = make([]codecRtidFn, len(sp)+1) 830 copy(sp2, sp[:idx]) 831 copy(sp2[idx+1:], sp[idx:]) 832 sp2[idx] = codecRtidFn{rtid, fn} 833 c.rtidFns.store(sp2) 834 // xdebugf(">>>> adding rt: %v to rtidfns of size: %v", rt, len(sp2)) 835 836 } 837 } 838 c.mu.Unlock() 839 return 840} 841 842// Handle defines a specific encoding format. It also stores any runtime state 843// used during an Encoding or Decoding session e.g. stored state about Types, etc. 844// 845// Once a handle is configured, it can be shared across multiple Encoders and Decoders. 846// 847// Note that a Handle is NOT safe for concurrent modification. 848// Consequently, do not modify it after it is configured if shared among 849// multiple Encoders and Decoders in different goroutines. 850// 851// Consequently, the typical usage model is that a Handle is pre-configured 852// before first time use, and not modified while in use. 853// Such a pre-configured Handle is safe for concurrent access. 854type Handle interface { 855 Name() string 856 // return the basic handle. It may not have been inited. 857 // Prefer to use basicHandle() helper function that ensures it has been inited. 858 getBasicHandle() *BasicHandle 859 recreateEncDriver(encDriver) bool 860 newEncDriver(w *Encoder) encDriver 861 newDecDriver(r *Decoder) decDriver 862 isBinary() bool 863 hasElemSeparators() bool 864 // IsBuiltinType(rtid uintptr) bool 865} 866 867// Raw represents raw formatted bytes. 868// We "blindly" store it during encode and retrieve the raw bytes during decode. 869// Note: it is dangerous during encode, so we may gate the behaviour 870// behind an Encode flag which must be explicitly set. 871type Raw []byte 872 873// RawExt represents raw unprocessed extension data. 874// Some codecs will decode extension data as a *RawExt 875// if there is no registered extension for the tag. 876// 877// Only one of Data or Value is nil. 878// If Data is nil, then the content of the RawExt is in the Value. 879type RawExt struct { 880 Tag uint64 881 // Data is the []byte which represents the raw ext. If nil, ext is exposed in Value. 882 // Data is used by codecs (e.g. binc, msgpack, simple) which do custom serialization of types 883 Data []byte 884 // Value represents the extension, if Data is nil. 885 // Value is used by codecs (e.g. cbor, json) which leverage the format to do 886 // custom serialization of the types. 887 Value interface{} 888} 889 890// BytesExt handles custom (de)serialization of types to/from []byte. 891// It is used by codecs (e.g. binc, msgpack, simple) which do custom serialization of the types. 892type BytesExt interface { 893 // WriteExt converts a value to a []byte. 894 // 895 // Note: v is a pointer iff the registered extension type is a struct or array kind. 896 WriteExt(v interface{}) []byte 897 898 // ReadExt updates a value from a []byte. 899 // 900 // Note: dst is always a pointer kind to the registered extension type. 901 ReadExt(dst interface{}, src []byte) 902} 903 904// InterfaceExt handles custom (de)serialization of types to/from another interface{} value. 905// The Encoder or Decoder will then handle the further (de)serialization of that known type. 906// 907// It is used by codecs (e.g. cbor, json) which use the format to do custom serialization of types. 908type InterfaceExt interface { 909 // ConvertExt converts a value into a simpler interface for easy encoding 910 // e.g. convert time.Time to int64. 911 // 912 // Note: v is a pointer iff the registered extension type is a struct or array kind. 913 ConvertExt(v interface{}) interface{} 914 915 // UpdateExt updates a value from a simpler interface for easy decoding 916 // e.g. convert int64 to time.Time. 917 // 918 // Note: dst is always a pointer kind to the registered extension type. 919 UpdateExt(dst interface{}, src interface{}) 920} 921 922// Ext handles custom (de)serialization of custom types / extensions. 923type Ext interface { 924 BytesExt 925 InterfaceExt 926} 927 928// addExtWrapper is a wrapper implementation to support former AddExt exported method. 929type addExtWrapper struct { 930 encFn func(reflect.Value) ([]byte, error) 931 decFn func(reflect.Value, []byte) error 932} 933 934func (x addExtWrapper) WriteExt(v interface{}) []byte { 935 bs, err := x.encFn(reflect.ValueOf(v)) 936 if err != nil { 937 panic(err) 938 } 939 return bs 940} 941 942func (x addExtWrapper) ReadExt(v interface{}, bs []byte) { 943 if err := x.decFn(reflect.ValueOf(v), bs); err != nil { 944 panic(err) 945 } 946} 947 948func (x addExtWrapper) ConvertExt(v interface{}) interface{} { 949 return x.WriteExt(v) 950} 951 952func (x addExtWrapper) UpdateExt(dest interface{}, v interface{}) { 953 x.ReadExt(dest, v.([]byte)) 954} 955 956type extWrapper struct { 957 BytesExt 958 InterfaceExt 959} 960 961type bytesExtFailer struct{} 962 963func (bytesExtFailer) WriteExt(v interface{}) []byte { 964 panicv.errorstr("BytesExt.WriteExt is not supported") 965 return nil 966} 967func (bytesExtFailer) ReadExt(v interface{}, bs []byte) { 968 panicv.errorstr("BytesExt.ReadExt is not supported") 969} 970 971type interfaceExtFailer struct{} 972 973func (interfaceExtFailer) ConvertExt(v interface{}) interface{} { 974 panicv.errorstr("InterfaceExt.ConvertExt is not supported") 975 return nil 976} 977func (interfaceExtFailer) UpdateExt(dest interface{}, v interface{}) { 978 panicv.errorstr("InterfaceExt.UpdateExt is not supported") 979} 980 981type binaryEncodingType struct{} 982 983func (binaryEncodingType) isBinary() bool { return true } 984 985type textEncodingType struct{} 986 987func (textEncodingType) isBinary() bool { return false } 988 989// noBuiltInTypes is embedded into many types which do not support builtins 990// e.g. msgpack, simple, cbor. 991 992// type noBuiltInTypeChecker struct{} 993// func (noBuiltInTypeChecker) IsBuiltinType(rt uintptr) bool { return false } 994// type noBuiltInTypes struct{ noBuiltInTypeChecker } 995 996type noBuiltInTypes struct{} 997 998func (noBuiltInTypes) EncodeBuiltin(rt uintptr, v interface{}) {} 999func (noBuiltInTypes) DecodeBuiltin(rt uintptr, v interface{}) {} 1000 1001// type noStreamingCodec struct{} 1002// func (noStreamingCodec) CheckBreak() bool { return false } 1003// func (noStreamingCodec) hasElemSeparators() bool { return false } 1004 1005type noElemSeparators struct{} 1006 1007func (noElemSeparators) hasElemSeparators() (v bool) { return } 1008func (noElemSeparators) recreateEncDriver(e encDriver) (v bool) { return } 1009 1010// bigenHelper. 1011// Users must already slice the x completely, because we will not reslice. 1012type bigenHelper struct { 1013 x []byte // must be correctly sliced to appropriate len. slicing is a cost. 1014 w *encWriterSwitch 1015} 1016 1017func (z bigenHelper) writeUint16(v uint16) { 1018 bigen.PutUint16(z.x, v) 1019 z.w.writeb(z.x) 1020} 1021 1022func (z bigenHelper) writeUint32(v uint32) { 1023 bigen.PutUint32(z.x, v) 1024 z.w.writeb(z.x) 1025} 1026 1027func (z bigenHelper) writeUint64(v uint64) { 1028 bigen.PutUint64(z.x, v) 1029 z.w.writeb(z.x) 1030} 1031 1032type extTypeTagFn struct { 1033 rtid uintptr 1034 rtidptr uintptr 1035 rt reflect.Type 1036 tag uint64 1037 ext Ext 1038 _ [1]uint64 // padding 1039} 1040 1041type extHandle []extTypeTagFn 1042 1043// AddExt registes an encode and decode function for a reflect.Type. 1044// To deregister an Ext, call AddExt with nil encfn and/or nil decfn. 1045// 1046// Deprecated: Use SetBytesExt or SetInterfaceExt on the Handle instead. 1047func (o *extHandle) AddExt(rt reflect.Type, tag byte, 1048 encfn func(reflect.Value) ([]byte, error), 1049 decfn func(reflect.Value, []byte) error) (err error) { 1050 if encfn == nil || decfn == nil { 1051 return o.SetExt(rt, uint64(tag), nil) 1052 } 1053 return o.SetExt(rt, uint64(tag), addExtWrapper{encfn, decfn}) 1054} 1055 1056// SetExt will set the extension for a tag and reflect.Type. 1057// Note that the type must be a named type, and specifically not a pointer or Interface. 1058// An error is returned if that is not honored. 1059// To Deregister an ext, call SetExt with nil Ext. 1060// 1061// Deprecated: Use SetBytesExt or SetInterfaceExt on the Handle instead. 1062func (o *extHandle) SetExt(rt reflect.Type, tag uint64, ext Ext) (err error) { 1063 // o is a pointer, because we may need to initialize it 1064 rk := rt.Kind() 1065 for rk == reflect.Ptr { 1066 rt = rt.Elem() 1067 rk = rt.Kind() 1068 } 1069 1070 if rt.PkgPath() == "" || rk == reflect.Interface { // || rk == reflect.Ptr { 1071 return fmt.Errorf("codec.Handle.SetExt: Takes named type, not a pointer or interface: %v", rt) 1072 } 1073 1074 rtid := rt2id(rt) 1075 switch rtid { 1076 case timeTypId, rawTypId, rawExtTypId: 1077 // all natively supported type, so cannot have an extension 1078 return // TODO: should we silently ignore, or return an error??? 1079 } 1080 // if o == nil { 1081 // return errors.New("codec.Handle.SetExt: extHandle not initialized") 1082 // } 1083 o2 := *o 1084 // if o2 == nil { 1085 // return errors.New("codec.Handle.SetExt: extHandle not initialized") 1086 // } 1087 for i := range o2 { 1088 v := &o2[i] 1089 if v.rtid == rtid { 1090 v.tag, v.ext = tag, ext 1091 return 1092 } 1093 } 1094 rtidptr := rt2id(reflect.PtrTo(rt)) 1095 *o = append(o2, extTypeTagFn{rtid, rtidptr, rt, tag, ext, [1]uint64{}}) 1096 return 1097} 1098 1099func (o extHandle) getExt(rtid uintptr) (v *extTypeTagFn) { 1100 for i := range o { 1101 v = &o[i] 1102 if v.rtid == rtid || v.rtidptr == rtid { 1103 return 1104 } 1105 } 1106 return nil 1107} 1108 1109func (o extHandle) getExtForTag(tag uint64) (v *extTypeTagFn) { 1110 for i := range o { 1111 v = &o[i] 1112 if v.tag == tag { 1113 return 1114 } 1115 } 1116 return nil 1117} 1118 1119type intf2impl struct { 1120 rtid uintptr // for intf 1121 impl reflect.Type 1122 // _ [1]uint64 // padding // not-needed, as *intf2impl is never returned. 1123} 1124 1125type intf2impls []intf2impl 1126 1127// Intf2Impl maps an interface to an implementing type. 1128// This allows us support infering the concrete type 1129// and populating it when passed an interface. 1130// e.g. var v io.Reader can be decoded as a bytes.Buffer, etc. 1131// 1132// Passing a nil impl will clear the mapping. 1133func (o *intf2impls) Intf2Impl(intf, impl reflect.Type) (err error) { 1134 if impl != nil && !impl.Implements(intf) { 1135 return fmt.Errorf("Intf2Impl: %v does not implement %v", impl, intf) 1136 } 1137 rtid := rt2id(intf) 1138 o2 := *o 1139 for i := range o2 { 1140 v := &o2[i] 1141 if v.rtid == rtid { 1142 v.impl = impl 1143 return 1144 } 1145 } 1146 *o = append(o2, intf2impl{rtid, impl}) 1147 return 1148} 1149 1150func (o intf2impls) intf2impl(rtid uintptr) (rv reflect.Value) { 1151 for i := range o { 1152 v := &o[i] 1153 if v.rtid == rtid { 1154 if v.impl == nil { 1155 return 1156 } 1157 if v.impl.Kind() == reflect.Ptr { 1158 return reflect.New(v.impl.Elem()) 1159 } 1160 return reflect.New(v.impl).Elem() 1161 } 1162 } 1163 return 1164} 1165 1166type structFieldInfoFlag uint8 1167 1168const ( 1169 _ structFieldInfoFlag = 1 << iota 1170 structFieldInfoFlagReady 1171 structFieldInfoFlagOmitEmpty 1172) 1173 1174func (x *structFieldInfoFlag) flagSet(f structFieldInfoFlag) { 1175 *x = *x | f 1176} 1177 1178func (x *structFieldInfoFlag) flagClr(f structFieldInfoFlag) { 1179 *x = *x &^ f 1180} 1181 1182func (x structFieldInfoFlag) flagGet(f structFieldInfoFlag) bool { 1183 return x&f != 0 1184} 1185 1186func (x structFieldInfoFlag) omitEmpty() bool { 1187 return x.flagGet(structFieldInfoFlagOmitEmpty) 1188} 1189 1190func (x structFieldInfoFlag) ready() bool { 1191 return x.flagGet(structFieldInfoFlagReady) 1192} 1193 1194type structFieldInfo struct { 1195 encName string // encode name 1196 fieldName string // field name 1197 1198 is [maxLevelsEmbedding]uint16 // (recursive/embedded) field index in struct 1199 nis uint8 // num levels of embedding. if 1, then it's not embedded. 1200 1201 encNameAsciiAlphaNum bool // the encName only contains ascii alphabet and numbers 1202 structFieldInfoFlag 1203 _ [1]byte // padding 1204} 1205 1206func (si *structFieldInfo) setToZeroValue(v reflect.Value) { 1207 if v, valid := si.field(v, false); valid { 1208 v.Set(reflect.Zero(v.Type())) 1209 } 1210} 1211 1212// rv returns the field of the struct. 1213// If anonymous, it returns an Invalid 1214func (si *structFieldInfo) field(v reflect.Value, update bool) (rv2 reflect.Value, valid bool) { 1215 // replicate FieldByIndex 1216 for i, x := range si.is { 1217 if uint8(i) == si.nis { 1218 break 1219 } 1220 if v, valid = baseStructRv(v, update); !valid { 1221 return 1222 } 1223 v = v.Field(int(x)) 1224 } 1225 1226 return v, true 1227} 1228 1229// func (si *structFieldInfo) fieldval(v reflect.Value, update bool) reflect.Value { 1230// v, _ = si.field(v, update) 1231// return v 1232// } 1233 1234func parseStructInfo(stag string) (toArray, omitEmpty bool, keytype valueType) { 1235 keytype = valueTypeString // default 1236 if stag == "" { 1237 return 1238 } 1239 for i, s := range strings.Split(stag, ",") { 1240 if i == 0 { 1241 } else { 1242 switch s { 1243 case "omitempty": 1244 omitEmpty = true 1245 case "toarray": 1246 toArray = true 1247 case "int": 1248 keytype = valueTypeInt 1249 case "uint": 1250 keytype = valueTypeUint 1251 case "float": 1252 keytype = valueTypeFloat 1253 // case "bool": 1254 // keytype = valueTypeBool 1255 case "string": 1256 keytype = valueTypeString 1257 } 1258 } 1259 } 1260 return 1261} 1262 1263func (si *structFieldInfo) parseTag(stag string) { 1264 // if fname == "" { 1265 // panic(errNoFieldNameToStructFieldInfo) 1266 // } 1267 1268 if stag == "" { 1269 return 1270 } 1271 for i, s := range strings.Split(stag, ",") { 1272 if i == 0 { 1273 if s != "" { 1274 si.encName = s 1275 } 1276 } else { 1277 switch s { 1278 case "omitempty": 1279 si.flagSet(structFieldInfoFlagOmitEmpty) 1280 // si.omitEmpty = true 1281 // case "toarray": 1282 // si.toArray = true 1283 } 1284 } 1285 } 1286} 1287 1288type sfiSortedByEncName []*structFieldInfo 1289 1290func (p sfiSortedByEncName) Len() int { return len(p) } 1291func (p sfiSortedByEncName) Less(i, j int) bool { return p[uint(i)].encName < p[uint(j)].encName } 1292func (p sfiSortedByEncName) Swap(i, j int) { p[uint(i)], p[uint(j)] = p[uint(j)], p[uint(i)] } 1293 1294const structFieldNodeNumToCache = 4 1295 1296type structFieldNodeCache struct { 1297 rv [structFieldNodeNumToCache]reflect.Value 1298 idx [structFieldNodeNumToCache]uint32 1299 num uint8 1300} 1301 1302func (x *structFieldNodeCache) get(key uint32) (fv reflect.Value, valid bool) { 1303 for i, k := range &x.idx { 1304 if uint8(i) == x.num { 1305 return // break 1306 } 1307 if key == k { 1308 return x.rv[i], true 1309 } 1310 } 1311 return 1312} 1313 1314func (x *structFieldNodeCache) tryAdd(fv reflect.Value, key uint32) { 1315 if x.num < structFieldNodeNumToCache { 1316 x.rv[x.num] = fv 1317 x.idx[x.num] = key 1318 x.num++ 1319 return 1320 } 1321} 1322 1323type structFieldNode struct { 1324 v reflect.Value 1325 cache2 structFieldNodeCache 1326 cache3 structFieldNodeCache 1327 update bool 1328} 1329 1330func (x *structFieldNode) field(si *structFieldInfo) (fv reflect.Value) { 1331 // return si.fieldval(x.v, x.update) 1332 // Note: we only cache if nis=2 or nis=3 i.e. up to 2 levels of embedding 1333 // This mostly saves us time on the repeated calls to v.Elem, v.Field, etc. 1334 var valid bool 1335 switch si.nis { 1336 case 1: 1337 fv = x.v.Field(int(si.is[0])) 1338 case 2: 1339 if fv, valid = x.cache2.get(uint32(si.is[0])); valid { 1340 fv = fv.Field(int(si.is[1])) 1341 return 1342 } 1343 fv = x.v.Field(int(si.is[0])) 1344 if fv, valid = baseStructRv(fv, x.update); !valid { 1345 return 1346 } 1347 x.cache2.tryAdd(fv, uint32(si.is[0])) 1348 fv = fv.Field(int(si.is[1])) 1349 case 3: 1350 var key uint32 = uint32(si.is[0])<<16 | uint32(si.is[1]) 1351 if fv, valid = x.cache3.get(key); valid { 1352 fv = fv.Field(int(si.is[2])) 1353 return 1354 } 1355 fv = x.v.Field(int(si.is[0])) 1356 if fv, valid = baseStructRv(fv, x.update); !valid { 1357 return 1358 } 1359 fv = fv.Field(int(si.is[1])) 1360 if fv, valid = baseStructRv(fv, x.update); !valid { 1361 return 1362 } 1363 x.cache3.tryAdd(fv, key) 1364 fv = fv.Field(int(si.is[2])) 1365 default: 1366 fv, _ = si.field(x.v, x.update) 1367 } 1368 return 1369} 1370 1371func baseStructRv(v reflect.Value, update bool) (v2 reflect.Value, valid bool) { 1372 for v.Kind() == reflect.Ptr { 1373 if v.IsNil() { 1374 if !update { 1375 return 1376 } 1377 v.Set(reflect.New(v.Type().Elem())) 1378 } 1379 v = v.Elem() 1380 } 1381 return v, true 1382} 1383 1384type typeInfoFlag uint8 1385 1386const ( 1387 typeInfoFlagComparable = 1 << iota 1388 typeInfoFlagIsZeroer 1389 typeInfoFlagIsZeroerPtr 1390) 1391 1392// typeInfo keeps information about each (non-ptr) type referenced in the encode/decode sequence. 1393// 1394// During an encode/decode sequence, we work as below: 1395// - If base is a built in type, en/decode base value 1396// - If base is registered as an extension, en/decode base value 1397// - If type is binary(M/Unm)arshaler, call Binary(M/Unm)arshal method 1398// - If type is text(M/Unm)arshaler, call Text(M/Unm)arshal method 1399// - Else decode appropriately based on the reflect.Kind 1400type typeInfo struct { 1401 rt reflect.Type 1402 elem reflect.Type 1403 pkgpath string 1404 1405 rtid uintptr 1406 // rv0 reflect.Value // saved zero value, used if immutableKind 1407 1408 numMeth uint16 // number of methods 1409 kind uint8 1410 chandir uint8 1411 1412 anyOmitEmpty bool // true if a struct, and any of the fields are tagged "omitempty" 1413 toArray bool // whether this (struct) type should be encoded as an array 1414 keyType valueType // if struct, how is the field name stored in a stream? default is string 1415 mbs bool // base type (T or *T) is a MapBySlice 1416 1417 // ---- cpu cache line boundary? 1418 sfiSort []*structFieldInfo // sorted. Used when enc/dec struct to map. 1419 sfiSrc []*structFieldInfo // unsorted. Used when enc/dec struct to array. 1420 1421 key reflect.Type 1422 1423 // ---- cpu cache line boundary? 1424 // sfis []structFieldInfo // all sfi, in src order, as created. 1425 sfiNamesSort []byte // all names, with indexes into the sfiSort 1426 1427 // format of marshal type fields below: [btj][mu]p? OR csp? 1428 1429 bm bool // T is a binaryMarshaler 1430 bmp bool // *T is a binaryMarshaler 1431 bu bool // T is a binaryUnmarshaler 1432 bup bool // *T is a binaryUnmarshaler 1433 tm bool // T is a textMarshaler 1434 tmp bool // *T is a textMarshaler 1435 tu bool // T is a textUnmarshaler 1436 tup bool // *T is a textUnmarshaler 1437 1438 jm bool // T is a jsonMarshaler 1439 jmp bool // *T is a jsonMarshaler 1440 ju bool // T is a jsonUnmarshaler 1441 jup bool // *T is a jsonUnmarshaler 1442 cs bool // T is a Selfer 1443 csp bool // *T is a Selfer 1444 mf bool // T is a MissingFielder 1445 mfp bool // *T is a MissingFielder 1446 1447 // other flags, with individual bits representing if set. 1448 flags typeInfoFlag 1449 infoFieldOmitempty bool 1450 1451 _ [6]byte // padding 1452 _ [2]uint64 // padding 1453} 1454 1455func (ti *typeInfo) isFlag(f typeInfoFlag) bool { 1456 return ti.flags&f != 0 1457} 1458 1459func (ti *typeInfo) indexForEncName(name []byte) (index int16) { 1460 var sn []byte 1461 if len(name)+2 <= 32 { 1462 var buf [32]byte // should not escape to heap 1463 sn = buf[:len(name)+2] 1464 } else { 1465 sn = make([]byte, len(name)+2) 1466 } 1467 copy(sn[1:], name) 1468 sn[0], sn[len(sn)-1] = tiSep2(name), 0xff 1469 j := bytes.Index(ti.sfiNamesSort, sn) 1470 if j < 0 { 1471 return -1 1472 } 1473 index = int16(uint16(ti.sfiNamesSort[j+len(sn)+1]) | uint16(ti.sfiNamesSort[j+len(sn)])<<8) 1474 return 1475} 1476 1477type rtid2ti struct { 1478 rtid uintptr 1479 ti *typeInfo 1480} 1481 1482// TypeInfos caches typeInfo for each type on first inspection. 1483// 1484// It is configured with a set of tag keys, which are used to get 1485// configuration for the type. 1486type TypeInfos struct { 1487 // infos: formerly map[uintptr]*typeInfo, now *[]rtid2ti, 2 words expected 1488 infos atomicTypeInfoSlice 1489 mu sync.Mutex 1490 tags []string 1491 _ [2]uint64 // padding 1492} 1493 1494// NewTypeInfos creates a TypeInfos given a set of struct tags keys. 1495// 1496// This allows users customize the struct tag keys which contain configuration 1497// of their types. 1498func NewTypeInfos(tags []string) *TypeInfos { 1499 return &TypeInfos{tags: tags} 1500} 1501 1502func (x *TypeInfos) structTag(t reflect.StructTag) (s string) { 1503 // check for tags: codec, json, in that order. 1504 // this allows seamless support for many configured structs. 1505 for _, x := range x.tags { 1506 s = t.Get(x) 1507 if s != "" { 1508 return s 1509 } 1510 } 1511 return 1512} 1513 1514func findTypeInfo(s []rtid2ti, rtid uintptr) (i uint, ti *typeInfo) { 1515 // binary search. adapted from sort/search.go. 1516 // Note: we use goto (instead of for loop) so this can be inlined. 1517 1518 // if sp == nil { 1519 // return -1, nil 1520 // } 1521 // s := *sp 1522 1523 // h, i, j := 0, 0, len(s) 1524 var h uint // var h, i uint 1525 var j = uint(len(s)) 1526LOOP: 1527 if i < j { 1528 h = i + (j-i)/2 1529 if s[h].rtid < rtid { 1530 i = h + 1 1531 } else { 1532 j = h 1533 } 1534 goto LOOP 1535 } 1536 if i < uint(len(s)) && s[i].rtid == rtid { 1537 ti = s[i].ti 1538 } 1539 return 1540} 1541 1542func (x *TypeInfos) get(rtid uintptr, rt reflect.Type) (pti *typeInfo) { 1543 sp := x.infos.load() 1544 if sp != nil { 1545 _, pti = findTypeInfo(sp, rtid) 1546 if pti != nil { 1547 return 1548 } 1549 } 1550 1551 rk := rt.Kind() 1552 1553 if rk == reflect.Ptr { // || (rk == reflect.Interface && rtid != intfTypId) { 1554 panicv.errorf("invalid kind passed to TypeInfos.get: %v - %v", rk, rt) 1555 } 1556 1557 // do not hold lock while computing this. 1558 // it may lead to duplication, but that's ok. 1559 ti := typeInfo{ 1560 rt: rt, 1561 rtid: rtid, 1562 kind: uint8(rk), 1563 pkgpath: rt.PkgPath(), 1564 keyType: valueTypeString, // default it - so it's never 0 1565 } 1566 // ti.rv0 = reflect.Zero(rt) 1567 1568 // ti.comparable = rt.Comparable() 1569 ti.numMeth = uint16(rt.NumMethod()) 1570 1571 ti.bm, ti.bmp = implIntf(rt, binaryMarshalerTyp) 1572 ti.bu, ti.bup = implIntf(rt, binaryUnmarshalerTyp) 1573 ti.tm, ti.tmp = implIntf(rt, textMarshalerTyp) 1574 ti.tu, ti.tup = implIntf(rt, textUnmarshalerTyp) 1575 ti.jm, ti.jmp = implIntf(rt, jsonMarshalerTyp) 1576 ti.ju, ti.jup = implIntf(rt, jsonUnmarshalerTyp) 1577 ti.cs, ti.csp = implIntf(rt, selferTyp) 1578 ti.mf, ti.mfp = implIntf(rt, missingFielderTyp) 1579 1580 b1, b2 := implIntf(rt, iszeroTyp) 1581 if b1 { 1582 ti.flags |= typeInfoFlagIsZeroer 1583 } 1584 if b2 { 1585 ti.flags |= typeInfoFlagIsZeroerPtr 1586 } 1587 if rt.Comparable() { 1588 ti.flags |= typeInfoFlagComparable 1589 } 1590 1591 switch rk { 1592 case reflect.Struct: 1593 var omitEmpty bool 1594 if f, ok := rt.FieldByName(structInfoFieldName); ok { 1595 ti.toArray, omitEmpty, ti.keyType = parseStructInfo(x.structTag(f.Tag)) 1596 ti.infoFieldOmitempty = omitEmpty 1597 } else { 1598 ti.keyType = valueTypeString 1599 } 1600 pp, pi := &pool.tiload, pool.tiload.Get() // pool.tiLoad() 1601 pv := pi.(*typeInfoLoadArray) 1602 pv.etypes[0] = ti.rtid 1603 // vv := typeInfoLoad{pv.fNames[:0], pv.encNames[:0], pv.etypes[:1], pv.sfis[:0]} 1604 vv := typeInfoLoad{pv.etypes[:1], pv.sfis[:0]} 1605 x.rget(rt, rtid, omitEmpty, nil, &vv) 1606 // ti.sfis = vv.sfis 1607 ti.sfiSrc, ti.sfiSort, ti.sfiNamesSort, ti.anyOmitEmpty = rgetResolveSFI(rt, vv.sfis, pv) 1608 pp.Put(pi) 1609 case reflect.Map: 1610 ti.elem = rt.Elem() 1611 ti.key = rt.Key() 1612 case reflect.Slice: 1613 ti.mbs, _ = implIntf(rt, mapBySliceTyp) 1614 ti.elem = rt.Elem() 1615 case reflect.Chan: 1616 ti.elem = rt.Elem() 1617 ti.chandir = uint8(rt.ChanDir()) 1618 case reflect.Array, reflect.Ptr: 1619 ti.elem = rt.Elem() 1620 } 1621 // sfi = sfiSrc 1622 1623 x.mu.Lock() 1624 sp = x.infos.load() 1625 var sp2 []rtid2ti 1626 if sp == nil { 1627 pti = &ti 1628 sp2 = []rtid2ti{{rtid, pti}} 1629 x.infos.store(sp2) 1630 } else { 1631 var idx uint 1632 idx, pti = findTypeInfo(sp, rtid) 1633 if pti == nil { 1634 pti = &ti 1635 sp2 = make([]rtid2ti, len(sp)+1) 1636 copy(sp2, sp[:idx]) 1637 copy(sp2[idx+1:], sp[idx:]) 1638 sp2[idx] = rtid2ti{rtid, pti} 1639 x.infos.store(sp2) 1640 } 1641 } 1642 x.mu.Unlock() 1643 return 1644} 1645 1646func (x *TypeInfos) rget(rt reflect.Type, rtid uintptr, omitEmpty bool, 1647 indexstack []uint16, pv *typeInfoLoad) { 1648 // Read up fields and store how to access the value. 1649 // 1650 // It uses go's rules for message selectors, 1651 // which say that the field with the shallowest depth is selected. 1652 // 1653 // Note: we consciously use slices, not a map, to simulate a set. 1654 // Typically, types have < 16 fields, 1655 // and iteration using equals is faster than maps there 1656 flen := rt.NumField() 1657 if flen > (1<<maxLevelsEmbedding - 1) { 1658 panicv.errorf("codec: types with > %v fields are not supported - has %v fields", 1659 (1<<maxLevelsEmbedding - 1), flen) 1660 } 1661 // pv.sfis = make([]structFieldInfo, flen) 1662LOOP: 1663 for j, jlen := uint16(0), uint16(flen); j < jlen; j++ { 1664 f := rt.Field(int(j)) 1665 fkind := f.Type.Kind() 1666 // skip if a func type, or is unexported, or structTag value == "-" 1667 switch fkind { 1668 case reflect.Func, reflect.Complex64, reflect.Complex128, reflect.UnsafePointer: 1669 continue LOOP 1670 } 1671 1672 isUnexported := f.PkgPath != "" 1673 if isUnexported && !f.Anonymous { 1674 continue 1675 } 1676 stag := x.structTag(f.Tag) 1677 if stag == "-" { 1678 continue 1679 } 1680 var si structFieldInfo 1681 var parsed bool 1682 // if anonymous and no struct tag (or it's blank), 1683 // and a struct (or pointer to struct), inline it. 1684 if f.Anonymous && fkind != reflect.Interface { 1685 // ^^ redundant but ok: per go spec, an embedded pointer type cannot be to an interface 1686 ft := f.Type 1687 isPtr := ft.Kind() == reflect.Ptr 1688 for ft.Kind() == reflect.Ptr { 1689 ft = ft.Elem() 1690 } 1691 isStruct := ft.Kind() == reflect.Struct 1692 1693 // Ignore embedded fields of unexported non-struct types. 1694 // Also, from go1.10, ignore pointers to unexported struct types 1695 // because unmarshal cannot assign a new struct to an unexported field. 1696 // See https://golang.org/issue/21357 1697 if (isUnexported && !isStruct) || (!allowSetUnexportedEmbeddedPtr && isUnexported && isPtr) { 1698 continue 1699 } 1700 doInline := stag == "" 1701 if !doInline { 1702 si.parseTag(stag) 1703 parsed = true 1704 doInline = si.encName == "" 1705 // doInline = si.isZero() 1706 } 1707 if doInline && isStruct { 1708 // if etypes contains this, don't call rget again (as fields are already seen here) 1709 ftid := rt2id(ft) 1710 // We cannot recurse forever, but we need to track other field depths. 1711 // So - we break if we see a type twice (not the first time). 1712 // This should be sufficient to handle an embedded type that refers to its 1713 // owning type, which then refers to its embedded type. 1714 processIt := true 1715 numk := 0 1716 for _, k := range pv.etypes { 1717 if k == ftid { 1718 numk++ 1719 if numk == rgetMaxRecursion { 1720 processIt = false 1721 break 1722 } 1723 } 1724 } 1725 if processIt { 1726 pv.etypes = append(pv.etypes, ftid) 1727 indexstack2 := make([]uint16, len(indexstack)+1) 1728 copy(indexstack2, indexstack) 1729 indexstack2[len(indexstack)] = j 1730 // indexstack2 := append(append(make([]int, 0, len(indexstack)+4), indexstack...), j) 1731 x.rget(ft, ftid, omitEmpty, indexstack2, pv) 1732 } 1733 continue 1734 } 1735 } 1736 1737 // after the anonymous dance: if an unexported field, skip 1738 if isUnexported { 1739 continue 1740 } 1741 1742 if f.Name == "" { 1743 panic(errNoFieldNameToStructFieldInfo) 1744 } 1745 1746 // pv.fNames = append(pv.fNames, f.Name) 1747 // if si.encName == "" { 1748 1749 if !parsed { 1750 si.encName = f.Name 1751 si.parseTag(stag) 1752 parsed = true 1753 } else if si.encName == "" { 1754 si.encName = f.Name 1755 } 1756 si.encNameAsciiAlphaNum = true 1757 for i := len(si.encName) - 1; i >= 0; i-- { // bounds-check elimination 1758 b := si.encName[i] 1759 if (b >= '0' && b <= '9') || (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') { 1760 continue 1761 } 1762 si.encNameAsciiAlphaNum = false 1763 break 1764 } 1765 si.fieldName = f.Name 1766 si.flagSet(structFieldInfoFlagReady) 1767 1768 // pv.encNames = append(pv.encNames, si.encName) 1769 1770 // si.ikind = int(f.Type.Kind()) 1771 if len(indexstack) > maxLevelsEmbedding-1 { 1772 panicv.errorf("codec: only supports up to %v depth of embedding - type has %v depth", 1773 maxLevelsEmbedding-1, len(indexstack)) 1774 } 1775 si.nis = uint8(len(indexstack)) + 1 1776 copy(si.is[:], indexstack) 1777 si.is[len(indexstack)] = j 1778 1779 if omitEmpty { 1780 si.flagSet(structFieldInfoFlagOmitEmpty) 1781 } 1782 pv.sfis = append(pv.sfis, si) 1783 } 1784} 1785 1786func tiSep(name string) uint8 { 1787 // (xn[0]%64) // (between 192-255 - outside ascii BMP) 1788 // return 0xfe - (name[0] & 63) 1789 // return 0xfe - (name[0] & 63) - uint8(len(name)) 1790 // return 0xfe - (name[0] & 63) - uint8(len(name)&63) 1791 // return ((0xfe - (name[0] & 63)) & 0xf8) | (uint8(len(name) & 0x07)) 1792 return 0xfe - (name[0] & 63) - uint8(len(name)&63) 1793} 1794 1795func tiSep2(name []byte) uint8 { 1796 return 0xfe - (name[0] & 63) - uint8(len(name)&63) 1797} 1798 1799// resolves the struct field info got from a call to rget. 1800// Returns a trimmed, unsorted and sorted []*structFieldInfo. 1801func rgetResolveSFI(rt reflect.Type, x []structFieldInfo, pv *typeInfoLoadArray) ( 1802 y, z []*structFieldInfo, ss []byte, anyOmitEmpty bool) { 1803 sa := pv.sfiidx[:0] 1804 sn := pv.b[:] 1805 n := len(x) 1806 1807 var xn string 1808 var ui uint16 1809 var sep byte 1810 1811 for i := range x { 1812 ui = uint16(i) 1813 xn = x[i].encName // fieldName or encName? use encName for now. 1814 if len(xn)+2 > cap(pv.b) { 1815 sn = make([]byte, len(xn)+2) 1816 } else { 1817 sn = sn[:len(xn)+2] 1818 } 1819 // use a custom sep, so that misses are less frequent, 1820 // since the sep (first char in search) is as unique as first char in field name. 1821 sep = tiSep(xn) 1822 sn[0], sn[len(sn)-1] = sep, 0xff 1823 copy(sn[1:], xn) 1824 j := bytes.Index(sa, sn) 1825 if j == -1 { 1826 sa = append(sa, sep) 1827 sa = append(sa, xn...) 1828 sa = append(sa, 0xff, byte(ui>>8), byte(ui)) 1829 } else { 1830 index := uint16(sa[j+len(sn)+1]) | uint16(sa[j+len(sn)])<<8 1831 // one of them must be reset to nil, 1832 // and the index updated appropriately to the other one 1833 if x[i].nis == x[index].nis { 1834 } else if x[i].nis < x[index].nis { 1835 sa[j+len(sn)], sa[j+len(sn)+1] = byte(ui>>8), byte(ui) 1836 if x[index].ready() { 1837 x[index].flagClr(structFieldInfoFlagReady) 1838 n-- 1839 } 1840 } else { 1841 if x[i].ready() { 1842 x[i].flagClr(structFieldInfoFlagReady) 1843 n-- 1844 } 1845 } 1846 } 1847 1848 } 1849 var w []structFieldInfo 1850 sharingArray := len(x) <= typeInfoLoadArraySfisLen // sharing array with typeInfoLoadArray 1851 if sharingArray { 1852 w = make([]structFieldInfo, n) 1853 } 1854 1855 // remove all the nils (non-ready) 1856 y = make([]*structFieldInfo, n) 1857 n = 0 1858 var sslen int 1859 for i := range x { 1860 if !x[i].ready() { 1861 continue 1862 } 1863 if !anyOmitEmpty && x[i].omitEmpty() { 1864 anyOmitEmpty = true 1865 } 1866 if sharingArray { 1867 w[n] = x[i] 1868 y[n] = &w[n] 1869 } else { 1870 y[n] = &x[i] 1871 } 1872 sslen = sslen + len(x[i].encName) + 4 1873 n++ 1874 } 1875 if n != len(y) { 1876 panicv.errorf("failure reading struct %v - expecting %d of %d valid fields, got %d", 1877 rt, len(y), len(x), n) 1878 } 1879 1880 z = make([]*structFieldInfo, len(y)) 1881 copy(z, y) 1882 sort.Sort(sfiSortedByEncName(z)) 1883 1884 sharingArray = len(sa) <= typeInfoLoadArraySfiidxLen 1885 if sharingArray { 1886 ss = make([]byte, 0, sslen) 1887 } else { 1888 ss = sa[:0] // reuse the newly made sa array if necessary 1889 } 1890 for i := range z { 1891 xn = z[i].encName 1892 sep = tiSep(xn) 1893 ui = uint16(i) 1894 ss = append(ss, sep) 1895 ss = append(ss, xn...) 1896 ss = append(ss, 0xff, byte(ui>>8), byte(ui)) 1897 } 1898 return 1899} 1900 1901func implIntf(rt, iTyp reflect.Type) (base bool, indir bool) { 1902 return rt.Implements(iTyp), reflect.PtrTo(rt).Implements(iTyp) 1903} 1904 1905// isEmptyStruct is only called from isEmptyValue, and checks if a struct is empty: 1906// - does it implement IsZero() bool 1907// - is it comparable, and can i compare directly using == 1908// - if checkStruct, then walk through the encodable fields 1909// and check if they are empty or not. 1910func isEmptyStruct(v reflect.Value, tinfos *TypeInfos, deref, checkStruct bool) bool { 1911 // v is a struct kind - no need to check again. 1912 // We only check isZero on a struct kind, to reduce the amount of times 1913 // that we lookup the rtid and typeInfo for each type as we walk the tree. 1914 1915 vt := v.Type() 1916 rtid := rt2id(vt) 1917 if tinfos == nil { 1918 tinfos = defTypeInfos 1919 } 1920 ti := tinfos.get(rtid, vt) 1921 if ti.rtid == timeTypId { 1922 return rv2i(v).(time.Time).IsZero() 1923 } 1924 if ti.isFlag(typeInfoFlagIsZeroerPtr) && v.CanAddr() { 1925 return rv2i(v.Addr()).(isZeroer).IsZero() 1926 } 1927 if ti.isFlag(typeInfoFlagIsZeroer) { 1928 return rv2i(v).(isZeroer).IsZero() 1929 } 1930 if ti.isFlag(typeInfoFlagComparable) { 1931 return rv2i(v) == rv2i(reflect.Zero(vt)) 1932 } 1933 if !checkStruct { 1934 return false 1935 } 1936 // We only care about what we can encode/decode, 1937 // so that is what we use to check omitEmpty. 1938 for _, si := range ti.sfiSrc { 1939 sfv, valid := si.field(v, false) 1940 if valid && !isEmptyValue(sfv, tinfos, deref, checkStruct) { 1941 return false 1942 } 1943 } 1944 return true 1945} 1946 1947// func roundFloat(x float64) float64 { 1948// t := math.Trunc(x) 1949// if math.Abs(x-t) >= 0.5 { 1950// return t + math.Copysign(1, x) 1951// } 1952// return t 1953// } 1954 1955func panicToErr(h errDecorator, err *error) { 1956 // Note: This method MUST be called directly from defer i.e. defer panicToErr ... 1957 // else it seems the recover is not fully handled 1958 if recoverPanicToErr { 1959 if x := recover(); x != nil { 1960 // fmt.Printf("panic'ing with: %v\n", x) 1961 // debug.PrintStack() 1962 panicValToErr(h, x, err) 1963 } 1964 } 1965} 1966 1967func panicValToErr(h errDecorator, v interface{}, err *error) { 1968 switch xerr := v.(type) { 1969 case nil: 1970 case error: 1971 switch xerr { 1972 case nil: 1973 case io.EOF, io.ErrUnexpectedEOF, errEncoderNotInitialized, errDecoderNotInitialized: 1974 // treat as special (bubble up) 1975 *err = xerr 1976 default: 1977 h.wrapErr(xerr, err) 1978 } 1979 case string: 1980 if xerr != "" { 1981 h.wrapErr(xerr, err) 1982 } 1983 case fmt.Stringer: 1984 if xerr != nil { 1985 h.wrapErr(xerr, err) 1986 } 1987 default: 1988 h.wrapErr(v, err) 1989 } 1990} 1991 1992func isImmutableKind(k reflect.Kind) (v bool) { 1993 // return immutableKindsSet[k] 1994 // since we know reflect.Kind is in range 0..31, then use the k%32 == k constraint 1995 return immutableKindsSet[k%reflect.Kind(len(immutableKindsSet))] // bounds-check-elimination 1996} 1997 1998// ---- 1999 2000type codecFnInfo struct { 2001 ti *typeInfo 2002 xfFn Ext 2003 xfTag uint64 2004 seq seqType 2005 addrD bool 2006 addrF bool // if addrD, this says whether decode function can take a value or a ptr 2007 addrE bool 2008} 2009 2010// codecFn encapsulates the captured variables and the encode function. 2011// This way, we only do some calculations one times, and pass to the 2012// code block that should be called (encapsulated in a function) 2013// instead of executing the checks every time. 2014type codecFn struct { 2015 i codecFnInfo 2016 fe func(*Encoder, *codecFnInfo, reflect.Value) 2017 fd func(*Decoder, *codecFnInfo, reflect.Value) 2018 _ [1]uint64 // padding 2019} 2020 2021type codecRtidFn struct { 2022 rtid uintptr 2023 fn *codecFn 2024} 2025 2026// ---- 2027 2028// these "checkOverflow" functions must be inlinable, and not call anybody. 2029// Overflow means that the value cannot be represented without wrapping/overflow. 2030// Overflow=false does not mean that the value can be represented without losing precision 2031// (especially for floating point). 2032 2033type checkOverflow struct{} 2034 2035// func (checkOverflow) Float16(f float64) (overflow bool) { 2036// panicv.errorf("unimplemented") 2037// if f < 0 { 2038// f = -f 2039// } 2040// return math.MaxFloat32 < f && f <= math.MaxFloat64 2041// } 2042 2043func (checkOverflow) Float32(v float64) (overflow bool) { 2044 if v < 0 { 2045 v = -v 2046 } 2047 return math.MaxFloat32 < v && v <= math.MaxFloat64 2048} 2049func (checkOverflow) Uint(v uint64, bitsize uint8) (overflow bool) { 2050 if bitsize == 0 || bitsize >= 64 || v == 0 { 2051 return 2052 } 2053 if trunc := (v << (64 - bitsize)) >> (64 - bitsize); v != trunc { 2054 overflow = true 2055 } 2056 return 2057} 2058func (checkOverflow) Int(v int64, bitsize uint8) (overflow bool) { 2059 if bitsize == 0 || bitsize >= 64 || v == 0 { 2060 return 2061 } 2062 if trunc := (v << (64 - bitsize)) >> (64 - bitsize); v != trunc { 2063 overflow = true 2064 } 2065 return 2066} 2067func (checkOverflow) SignedInt(v uint64) (overflow bool) { 2068 //e.g. -127 to 128 for int8 2069 pos := (v >> 63) == 0 2070 ui2 := v & 0x7fffffffffffffff 2071 if pos { 2072 if ui2 > math.MaxInt64 { 2073 overflow = true 2074 } 2075 } else { 2076 if ui2 > math.MaxInt64-1 { 2077 overflow = true 2078 } 2079 } 2080 return 2081} 2082 2083func (x checkOverflow) Float32V(v float64) float64 { 2084 if x.Float32(v) { 2085 panicv.errorf("float32 overflow: %v", v) 2086 } 2087 return v 2088} 2089func (x checkOverflow) UintV(v uint64, bitsize uint8) uint64 { 2090 if x.Uint(v, bitsize) { 2091 panicv.errorf("uint64 overflow: %v", v) 2092 } 2093 return v 2094} 2095func (x checkOverflow) IntV(v int64, bitsize uint8) int64 { 2096 if x.Int(v, bitsize) { 2097 panicv.errorf("int64 overflow: %v", v) 2098 } 2099 return v 2100} 2101func (x checkOverflow) SignedIntV(v uint64) int64 { 2102 if x.SignedInt(v) { 2103 panicv.errorf("uint64 to int64 overflow: %v", v) 2104 } 2105 return int64(v) 2106} 2107 2108// ------------------ SORT ----------------- 2109 2110func isNaN(f float64) bool { return f != f } 2111 2112// ----------------------- 2113 2114type ioFlusher interface { 2115 Flush() error 2116} 2117 2118type ioPeeker interface { 2119 Peek(int) ([]byte, error) 2120} 2121 2122type ioBuffered interface { 2123 Buffered() int 2124} 2125 2126// ----------------------- 2127 2128type intSlice []int64 2129type uintSlice []uint64 2130 2131// type uintptrSlice []uintptr 2132type floatSlice []float64 2133type boolSlice []bool 2134type stringSlice []string 2135 2136// type bytesSlice [][]byte 2137 2138func (p intSlice) Len() int { return len(p) } 2139func (p intSlice) Less(i, j int) bool { return p[uint(i)] < p[uint(j)] } 2140func (p intSlice) Swap(i, j int) { p[uint(i)], p[uint(j)] = p[uint(j)], p[uint(i)] } 2141 2142func (p uintSlice) Len() int { return len(p) } 2143func (p uintSlice) Less(i, j int) bool { return p[uint(i)] < p[uint(j)] } 2144func (p uintSlice) Swap(i, j int) { p[uint(i)], p[uint(j)] = p[uint(j)], p[uint(i)] } 2145 2146// func (p uintptrSlice) Len() int { return len(p) } 2147// func (p uintptrSlice) Less(i, j int) bool { return p[uint(i)] < p[uint(j)] } 2148// func (p uintptrSlice) Swap(i, j int) { p[uint(i)], p[uint(j)] = p[uint(j)], p[uint(i)] } 2149 2150func (p floatSlice) Len() int { return len(p) } 2151func (p floatSlice) Less(i, j int) bool { 2152 return p[uint(i)] < p[uint(j)] || isNaN(p[uint(i)]) && !isNaN(p[uint(j)]) 2153} 2154func (p floatSlice) Swap(i, j int) { p[uint(i)], p[uint(j)] = p[uint(j)], p[uint(i)] } 2155 2156func (p stringSlice) Len() int { return len(p) } 2157func (p stringSlice) Less(i, j int) bool { return p[uint(i)] < p[uint(j)] } 2158func (p stringSlice) Swap(i, j int) { p[uint(i)], p[uint(j)] = p[uint(j)], p[uint(i)] } 2159 2160// func (p bytesSlice) Len() int { return len(p) } 2161// func (p bytesSlice) Less(i, j int) bool { return bytes.Compare(p[uint(i)], p[uint(j)]) == -1 } 2162// func (p bytesSlice) Swap(i, j int) { p[uint(i)], p[uint(j)] = p[uint(j)], p[uint(i)] } 2163 2164func (p boolSlice) Len() int { return len(p) } 2165func (p boolSlice) Less(i, j int) bool { return !p[uint(i)] && p[uint(j)] } 2166func (p boolSlice) Swap(i, j int) { p[uint(i)], p[uint(j)] = p[uint(j)], p[uint(i)] } 2167 2168// --------------------- 2169 2170type sfiRv struct { 2171 v *structFieldInfo 2172 r reflect.Value 2173} 2174 2175type intRv struct { 2176 v int64 2177 r reflect.Value 2178} 2179type intRvSlice []intRv 2180type uintRv struct { 2181 v uint64 2182 r reflect.Value 2183} 2184type uintRvSlice []uintRv 2185type floatRv struct { 2186 v float64 2187 r reflect.Value 2188} 2189type floatRvSlice []floatRv 2190type boolRv struct { 2191 v bool 2192 r reflect.Value 2193} 2194type boolRvSlice []boolRv 2195type stringRv struct { 2196 v string 2197 r reflect.Value 2198} 2199type stringRvSlice []stringRv 2200type bytesRv struct { 2201 v []byte 2202 r reflect.Value 2203} 2204type bytesRvSlice []bytesRv 2205type timeRv struct { 2206 v time.Time 2207 r reflect.Value 2208} 2209type timeRvSlice []timeRv 2210 2211func (p intRvSlice) Len() int { return len(p) } 2212func (p intRvSlice) Less(i, j int) bool { return p[uint(i)].v < p[uint(j)].v } 2213func (p intRvSlice) Swap(i, j int) { p[uint(i)], p[uint(j)] = p[uint(j)], p[uint(i)] } 2214 2215func (p uintRvSlice) Len() int { return len(p) } 2216func (p uintRvSlice) Less(i, j int) bool { return p[uint(i)].v < p[uint(j)].v } 2217func (p uintRvSlice) Swap(i, j int) { p[uint(i)], p[uint(j)] = p[uint(j)], p[uint(i)] } 2218 2219func (p floatRvSlice) Len() int { return len(p) } 2220func (p floatRvSlice) Less(i, j int) bool { 2221 return p[uint(i)].v < p[uint(j)].v || isNaN(p[uint(i)].v) && !isNaN(p[uint(j)].v) 2222} 2223func (p floatRvSlice) Swap(i, j int) { p[uint(i)], p[uint(j)] = p[uint(j)], p[uint(i)] } 2224 2225func (p stringRvSlice) Len() int { return len(p) } 2226func (p stringRvSlice) Less(i, j int) bool { return p[uint(i)].v < p[uint(j)].v } 2227func (p stringRvSlice) Swap(i, j int) { p[uint(i)], p[uint(j)] = p[uint(j)], p[uint(i)] } 2228 2229func (p bytesRvSlice) Len() int { return len(p) } 2230func (p bytesRvSlice) Less(i, j int) bool { return bytes.Compare(p[uint(i)].v, p[uint(j)].v) == -1 } 2231func (p bytesRvSlice) Swap(i, j int) { p[uint(i)], p[uint(j)] = p[uint(j)], p[uint(i)] } 2232 2233func (p boolRvSlice) Len() int { return len(p) } 2234func (p boolRvSlice) Less(i, j int) bool { return !p[uint(i)].v && p[uint(j)].v } 2235func (p boolRvSlice) Swap(i, j int) { p[uint(i)], p[uint(j)] = p[uint(j)], p[uint(i)] } 2236 2237func (p timeRvSlice) Len() int { return len(p) } 2238func (p timeRvSlice) Less(i, j int) bool { return p[uint(i)].v.Before(p[uint(j)].v) } 2239func (p timeRvSlice) Swap(i, j int) { p[uint(i)], p[uint(j)] = p[uint(j)], p[uint(i)] } 2240 2241// ----------------- 2242 2243type bytesI struct { 2244 v []byte 2245 i interface{} 2246} 2247 2248type bytesISlice []bytesI 2249 2250func (p bytesISlice) Len() int { return len(p) } 2251func (p bytesISlice) Less(i, j int) bool { return bytes.Compare(p[uint(i)].v, p[uint(j)].v) == -1 } 2252func (p bytesISlice) Swap(i, j int) { p[uint(i)], p[uint(j)] = p[uint(j)], p[uint(i)] } 2253 2254// ----------------- 2255 2256type set []uintptr 2257 2258func (s *set) add(v uintptr) (exists bool) { 2259 // e.ci is always nil, or len >= 1 2260 x := *s 2261 if x == nil { 2262 x = make([]uintptr, 1, 8) 2263 x[0] = v 2264 *s = x 2265 return 2266 } 2267 // typically, length will be 1. make this perform. 2268 if len(x) == 1 { 2269 if j := x[0]; j == 0 { 2270 x[0] = v 2271 } else if j == v { 2272 exists = true 2273 } else { 2274 x = append(x, v) 2275 *s = x 2276 } 2277 return 2278 } 2279 // check if it exists 2280 for _, j := range x { 2281 if j == v { 2282 exists = true 2283 return 2284 } 2285 } 2286 // try to replace a "deleted" slot 2287 for i, j := range x { 2288 if j == 0 { 2289 x[i] = v 2290 return 2291 } 2292 } 2293 // if unable to replace deleted slot, just append it. 2294 x = append(x, v) 2295 *s = x 2296 return 2297} 2298 2299func (s *set) remove(v uintptr) (exists bool) { 2300 x := *s 2301 if len(x) == 0 { 2302 return 2303 } 2304 if len(x) == 1 { 2305 if x[0] == v { 2306 x[0] = 0 2307 } 2308 return 2309 } 2310 for i, j := range x { 2311 if j == v { 2312 exists = true 2313 x[i] = 0 // set it to 0, as way to delete it. 2314 // copy(x[i:], x[i+1:]) 2315 // x = x[:len(x)-1] 2316 return 2317 } 2318 } 2319 return 2320} 2321 2322// ------ 2323 2324// bitset types are better than [256]bool, because they permit the whole 2325// bitset array being on a single cache line and use less memory. 2326// 2327// Also, since pos is a byte (0-255), there's no bounds checks on indexing (cheap). 2328// 2329// We previously had bitset128 [16]byte, and bitset32 [4]byte, but those introduces 2330// bounds checking, so we discarded them, and everyone uses bitset256. 2331// 2332// given x > 0 and n > 0 and x is exactly 2^n, then pos/x === pos>>n AND pos%x === pos&(x-1). 2333// consequently, pos/32 === pos>>5, pos/16 === pos>>4, pos/8 === pos>>3, pos%8 == pos&7 2334 2335type bitset256 [32]byte 2336 2337func (x *bitset256) isset(pos byte) bool { 2338 return x[pos>>3]&(1<<(pos&7)) != 0 2339} 2340 2341// func (x *bitset256) issetv(pos byte) byte { 2342// return x[pos>>3] & (1 << (pos & 7)) 2343// } 2344 2345func (x *bitset256) set(pos byte) { 2346 x[pos>>3] |= (1 << (pos & 7)) 2347} 2348 2349// func (x *bitset256) unset(pos byte) { 2350// x[pos>>3] &^= (1 << (pos & 7)) 2351// } 2352 2353// type bit2set256 [64]byte 2354 2355// func (x *bit2set256) set(pos byte, v1, v2 bool) { 2356// var pos2 uint8 = (pos & 3) << 1 // returning 0, 2, 4 or 6 2357// if v1 { 2358// x[pos>>2] |= 1 << (pos2 + 1) 2359// } 2360// if v2 { 2361// x[pos>>2] |= 1 << pos2 2362// } 2363// } 2364// func (x *bit2set256) get(pos byte) uint8 { 2365// var pos2 uint8 = (pos & 3) << 1 // returning 0, 2, 4 or 6 2366// return x[pos>>2] << (6 - pos2) >> 6 // 11000000 -> 00000011 2367// } 2368 2369// ------------ 2370 2371type pooler struct { 2372 // function-scoped pooled resources 2373 tiload sync.Pool // for type info loading 2374 sfiRv8, sfiRv16, sfiRv32, sfiRv64, sfiRv128 sync.Pool // for struct encoding 2375 2376 // lifetime-scoped pooled resources 2377 // dn sync.Pool // for decNaked 2378 buf1k, buf2k, buf4k, buf8k, buf16k, buf32k, buf64k sync.Pool // for [N]byte 2379} 2380 2381func (p *pooler) init() { 2382 p.tiload.New = func() interface{} { return new(typeInfoLoadArray) } 2383 2384 p.sfiRv8.New = func() interface{} { return new([8]sfiRv) } 2385 p.sfiRv16.New = func() interface{} { return new([16]sfiRv) } 2386 p.sfiRv32.New = func() interface{} { return new([32]sfiRv) } 2387 p.sfiRv64.New = func() interface{} { return new([64]sfiRv) } 2388 p.sfiRv128.New = func() interface{} { return new([128]sfiRv) } 2389 2390 // p.dn.New = func() interface{} { x := new(decNaked); x.init(); return x } 2391 2392 p.buf1k.New = func() interface{} { return new([1 * 1024]byte) } 2393 p.buf2k.New = func() interface{} { return new([2 * 1024]byte) } 2394 p.buf4k.New = func() interface{} { return new([4 * 1024]byte) } 2395 p.buf8k.New = func() interface{} { return new([8 * 1024]byte) } 2396 p.buf16k.New = func() interface{} { return new([16 * 1024]byte) } 2397 p.buf32k.New = func() interface{} { return new([32 * 1024]byte) } 2398 p.buf64k.New = func() interface{} { return new([64 * 1024]byte) } 2399 2400} 2401 2402// func (p *pooler) sfiRv8() (sp *sync.Pool, v interface{}) { 2403// return &p.strRv8, p.strRv8.Get() 2404// } 2405// func (p *pooler) sfiRv16() (sp *sync.Pool, v interface{}) { 2406// return &p.strRv16, p.strRv16.Get() 2407// } 2408// func (p *pooler) sfiRv32() (sp *sync.Pool, v interface{}) { 2409// return &p.strRv32, p.strRv32.Get() 2410// } 2411// func (p *pooler) sfiRv64() (sp *sync.Pool, v interface{}) { 2412// return &p.strRv64, p.strRv64.Get() 2413// } 2414// func (p *pooler) sfiRv128() (sp *sync.Pool, v interface{}) { 2415// return &p.strRv128, p.strRv128.Get() 2416// } 2417 2418// func (p *pooler) bytes1k() (sp *sync.Pool, v interface{}) { 2419// return &p.buf1k, p.buf1k.Get() 2420// } 2421// func (p *pooler) bytes2k() (sp *sync.Pool, v interface{}) { 2422// return &p.buf2k, p.buf2k.Get() 2423// } 2424// func (p *pooler) bytes4k() (sp *sync.Pool, v interface{}) { 2425// return &p.buf4k, p.buf4k.Get() 2426// } 2427// func (p *pooler) bytes8k() (sp *sync.Pool, v interface{}) { 2428// return &p.buf8k, p.buf8k.Get() 2429// } 2430// func (p *pooler) bytes16k() (sp *sync.Pool, v interface{}) { 2431// return &p.buf16k, p.buf16k.Get() 2432// } 2433// func (p *pooler) bytes32k() (sp *sync.Pool, v interface{}) { 2434// return &p.buf32k, p.buf32k.Get() 2435// } 2436// func (p *pooler) bytes64k() (sp *sync.Pool, v interface{}) { 2437// return &p.buf64k, p.buf64k.Get() 2438// } 2439 2440// func (p *pooler) tiLoad() (sp *sync.Pool, v interface{}) { 2441// return &p.tiload, p.tiload.Get() 2442// } 2443 2444// func (p *pooler) decNaked() (sp *sync.Pool, v interface{}) { 2445// return &p.dn, p.dn.Get() 2446// } 2447 2448// func (p *pooler) decNaked() (v *decNaked, f func(*decNaked) ) { 2449// sp := &(p.dn) 2450// vv := sp.Get() 2451// return vv.(*decNaked), func(x *decNaked) { sp.Put(vv) } 2452// } 2453// func (p *pooler) decNakedGet() (v interface{}) { 2454// return p.dn.Get() 2455// } 2456// func (p *pooler) tiLoadGet() (v interface{}) { 2457// return p.tiload.Get() 2458// } 2459// func (p *pooler) decNakedPut(v interface{}) { 2460// p.dn.Put(v) 2461// } 2462// func (p *pooler) tiLoadPut(v interface{}) { 2463// p.tiload.Put(v) 2464// } 2465 2466// ---------------------------------------------------- 2467 2468type panicHdl struct{} 2469 2470func (panicHdl) errorv(err error) { 2471 if err != nil { 2472 panic(err) 2473 } 2474} 2475 2476func (panicHdl) errorstr(message string) { 2477 if message != "" { 2478 panic(message) 2479 } 2480} 2481 2482func (panicHdl) errorf(format string, params ...interface{}) { 2483 if format == "" { 2484 } else if len(params) == 0 { 2485 panic(format) 2486 } else { 2487 panic(fmt.Sprintf(format, params...)) 2488 } 2489} 2490 2491// ---------------------------------------------------- 2492 2493type errDecorator interface { 2494 wrapErr(in interface{}, out *error) 2495} 2496 2497type errDecoratorDef struct{} 2498 2499func (errDecoratorDef) wrapErr(v interface{}, e *error) { *e = fmt.Errorf("%v", v) } 2500 2501// ---------------------------------------------------- 2502 2503type must struct{} 2504 2505func (must) String(s string, err error) string { 2506 if err != nil { 2507 panicv.errorv(err) 2508 } 2509 return s 2510} 2511func (must) Int(s int64, err error) int64 { 2512 if err != nil { 2513 panicv.errorv(err) 2514 } 2515 return s 2516} 2517func (must) Uint(s uint64, err error) uint64 { 2518 if err != nil { 2519 panicv.errorv(err) 2520 } 2521 return s 2522} 2523func (must) Float(s float64, err error) float64 { 2524 if err != nil { 2525 panicv.errorv(err) 2526 } 2527 return s 2528} 2529 2530// ------------------- 2531 2532type bytesBufPooler struct { 2533 pool *sync.Pool 2534 poolbuf interface{} 2535} 2536 2537func (z *bytesBufPooler) end() { 2538 if z.pool != nil { 2539 z.pool.Put(z.poolbuf) 2540 z.pool, z.poolbuf = nil, nil 2541 } 2542} 2543 2544func (z *bytesBufPooler) get(bufsize int) (buf []byte) { 2545 // ensure an end is called first (if necessary) 2546 if z.pool != nil { 2547 z.pool.Put(z.poolbuf) 2548 z.pool, z.poolbuf = nil, nil 2549 } 2550 2551 // // Try to use binary search. 2552 // // This is not optimal, as most folks select 1k or 2k buffers 2553 // // so a linear search is better (sequence of if/else blocks) 2554 // if bufsize < 1 { 2555 // bufsize = 0 2556 // } else { 2557 // bufsize-- 2558 // bufsize /= 1024 2559 // } 2560 // switch bufsize { 2561 // case 0: 2562 // z.pool, z.poolbuf = pool.bytes1k() 2563 // buf = z.poolbuf.(*[1 * 1024]byte)[:] 2564 // case 1: 2565 // z.pool, z.poolbuf = pool.bytes2k() 2566 // buf = z.poolbuf.(*[2 * 1024]byte)[:] 2567 // case 2, 3: 2568 // z.pool, z.poolbuf = pool.bytes4k() 2569 // buf = z.poolbuf.(*[4 * 1024]byte)[:] 2570 // case 4, 5, 6, 7: 2571 // z.pool, z.poolbuf = pool.bytes8k() 2572 // buf = z.poolbuf.(*[8 * 1024]byte)[:] 2573 // case 8, 9, 10, 11, 12, 13, 14, 15: 2574 // z.pool, z.poolbuf = pool.bytes16k() 2575 // buf = z.poolbuf.(*[16 * 1024]byte)[:] 2576 // case 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31: 2577 // z.pool, z.poolbuf = pool.bytes32k() 2578 // buf = z.poolbuf.(*[32 * 1024]byte)[:] 2579 // default: 2580 // z.pool, z.poolbuf = pool.bytes64k() 2581 // buf = z.poolbuf.(*[64 * 1024]byte)[:] 2582 // } 2583 // return 2584 2585 if bufsize <= 1*1024 { 2586 z.pool, z.poolbuf = &pool.buf1k, pool.buf1k.Get() // pool.bytes1k() 2587 buf = z.poolbuf.(*[1 * 1024]byte)[:] 2588 } else if bufsize <= 2*1024 { 2589 z.pool, z.poolbuf = &pool.buf2k, pool.buf2k.Get() // pool.bytes2k() 2590 buf = z.poolbuf.(*[2 * 1024]byte)[:] 2591 } else if bufsize <= 4*1024 { 2592 z.pool, z.poolbuf = &pool.buf4k, pool.buf4k.Get() // pool.bytes4k() 2593 buf = z.poolbuf.(*[4 * 1024]byte)[:] 2594 } else if bufsize <= 8*1024 { 2595 z.pool, z.poolbuf = &pool.buf8k, pool.buf8k.Get() // pool.bytes8k() 2596 buf = z.poolbuf.(*[8 * 1024]byte)[:] 2597 } else if bufsize <= 16*1024 { 2598 z.pool, z.poolbuf = &pool.buf16k, pool.buf16k.Get() // pool.bytes16k() 2599 buf = z.poolbuf.(*[16 * 1024]byte)[:] 2600 } else if bufsize <= 32*1024 { 2601 z.pool, z.poolbuf = &pool.buf32k, pool.buf32k.Get() // pool.bytes32k() 2602 buf = z.poolbuf.(*[32 * 1024]byte)[:] 2603 } else { 2604 z.pool, z.poolbuf = &pool.buf64k, pool.buf64k.Get() // pool.bytes64k() 2605 buf = z.poolbuf.(*[64 * 1024]byte)[:] 2606 } 2607 return 2608} 2609 2610// ---------------- 2611 2612type sfiRvPooler struct { 2613 pool *sync.Pool 2614 poolv interface{} 2615} 2616 2617func (z *sfiRvPooler) end() { 2618 if z.pool != nil { 2619 z.pool.Put(z.poolv) 2620 z.pool, z.poolv = nil, nil 2621 } 2622} 2623 2624func (z *sfiRvPooler) get(newlen int) (fkvs []sfiRv) { 2625 if newlen < 0 { // bounds-check-elimination 2626 // cannot happen // here for bounds-check-elimination 2627 } else if newlen <= 8 { 2628 z.pool, z.poolv = &pool.sfiRv8, pool.sfiRv8.Get() // pool.sfiRv8() 2629 fkvs = z.poolv.(*[8]sfiRv)[:newlen] 2630 } else if newlen <= 16 { 2631 z.pool, z.poolv = &pool.sfiRv16, pool.sfiRv16.Get() // pool.sfiRv16() 2632 fkvs = z.poolv.(*[16]sfiRv)[:newlen] 2633 } else if newlen <= 32 { 2634 z.pool, z.poolv = &pool.sfiRv32, pool.sfiRv32.Get() // pool.sfiRv32() 2635 fkvs = z.poolv.(*[32]sfiRv)[:newlen] 2636 } else if newlen <= 64 { 2637 z.pool, z.poolv = &pool.sfiRv64, pool.sfiRv64.Get() // pool.sfiRv64() 2638 fkvs = z.poolv.(*[64]sfiRv)[:newlen] 2639 } else if newlen <= 128 { 2640 z.pool, z.poolv = &pool.sfiRv128, pool.sfiRv128.Get() // pool.sfiRv128() 2641 fkvs = z.poolv.(*[128]sfiRv)[:newlen] 2642 } else { 2643 fkvs = make([]sfiRv, newlen) 2644 } 2645 return 2646} 2647 2648// xdebugf prints the message in red on the terminal. 2649// Use it in place of fmt.Printf (which it calls internally) 2650func xdebugf(pattern string, args ...interface{}) { 2651 var delim string 2652 if len(pattern) > 0 && pattern[len(pattern)-1] != '\n' { 2653 delim = "\n" 2654 } 2655 fmt.Printf("\033[1;31m"+pattern+delim+"\033[0m", args...) 2656} 2657 2658// func isImmutableKind(k reflect.Kind) (v bool) { 2659// return false || 2660// k == reflect.Int || 2661// k == reflect.Int8 || 2662// k == reflect.Int16 || 2663// k == reflect.Int32 || 2664// k == reflect.Int64 || 2665// k == reflect.Uint || 2666// k == reflect.Uint8 || 2667// k == reflect.Uint16 || 2668// k == reflect.Uint32 || 2669// k == reflect.Uint64 || 2670// k == reflect.Uintptr || 2671// k == reflect.Float32 || 2672// k == reflect.Float64 || 2673// k == reflect.Bool || 2674// k == reflect.String 2675// } 2676 2677// func timeLocUTCName(tzint int16) string { 2678// if tzint == 0 { 2679// return "UTC" 2680// } 2681// var tzname = []byte("UTC+00:00") 2682// //tzname := fmt.Sprintf("UTC%s%02d:%02d", tzsign, tz/60, tz%60) //perf issue using Sprintf. inline below. 2683// //tzhr, tzmin := tz/60, tz%60 //faster if u convert to int first 2684// var tzhr, tzmin int16 2685// if tzint < 0 { 2686// tzname[3] = '-' // (TODO: verify. this works here) 2687// tzhr, tzmin = -tzint/60, (-tzint)%60 2688// } else { 2689// tzhr, tzmin = tzint/60, tzint%60 2690// } 2691// tzname[4] = timeDigits[tzhr/10] 2692// tzname[5] = timeDigits[tzhr%10] 2693// tzname[7] = timeDigits[tzmin/10] 2694// tzname[8] = timeDigits[tzmin%10] 2695// return string(tzname) 2696// //return time.FixedZone(string(tzname), int(tzint)*60) 2697// } 2698