@@ -124,19 +124,15 @@ func puthead(buf []byte, smalltag, largetag byte, size uint64) int {
124
124
}
125
125
126
126
type encbuf struct {
127
- str []byte // string data, contains everything except list headers
128
- lheads []listhead // all list headers
129
- lhsize int // sum of sizes of all encoded list headers
130
- sizebuf [9 ]byte // auxiliary buffer for uint encoding
131
- bufvalue reflect.Value // used in writeByteArrayCopy
127
+ str []byte // string data, contains everything except list headers
128
+ lheads []listhead // all list headers
129
+ lhsize int // sum of sizes of all encoded list headers
130
+ sizebuf [9 ]byte // auxiliary buffer for uint encoding
132
131
}
133
132
134
133
// encbufs are pooled.
135
134
var encbufPool = sync.Pool {
136
- New : func () interface {} {
137
- var bytes []byte
138
- return & encbuf {bufvalue : reflect .ValueOf (& bytes ).Elem ()}
139
- },
135
+ New : func () interface {} { return new (encbuf ) },
140
136
}
141
137
142
138
func (w * encbuf ) reset () {
@@ -429,21 +425,14 @@ func writeBytes(val reflect.Value, w *encbuf) error {
429
425
return nil
430
426
}
431
427
432
- var byteType = reflect .TypeOf (byte (0 ))
433
-
434
428
func makeByteArrayWriter (typ reflect.Type ) writer {
435
- length := typ .Len ()
436
- if length == 0 {
429
+ switch typ .Len () {
430
+ case 0 :
437
431
return writeLengthZeroByteArray
438
- } else if length == 1 {
432
+ case 1 :
439
433
return writeLengthOneByteArray
440
- }
441
- if typ .Elem () != byteType {
442
- return writeNamedByteArray
443
- }
444
- return func (val reflect.Value , w * encbuf ) error {
445
- writeByteArrayCopy (length , val , w )
446
- return nil
434
+ default :
435
+ return writeByteArray
447
436
}
448
437
}
449
438
@@ -462,29 +451,18 @@ func writeLengthOneByteArray(val reflect.Value, w *encbuf) error {
462
451
return nil
463
452
}
464
453
465
- // writeByteArrayCopy encodes byte arrays using reflect.Copy. This is
466
- // the fast path for [N]byte where N > 1.
467
- func writeByteArrayCopy (length int , val reflect.Value , w * encbuf ) {
468
- w .encodeStringHeader (length )
469
- offset := len (w .str )
470
- w .str = append (w .str , make ([]byte , length )... )
471
- w .bufvalue .SetBytes (w .str [offset :])
472
- reflect .Copy (w .bufvalue , val )
473
- }
474
-
475
- // writeNamedByteArray encodes byte arrays with named element type.
476
- // This exists because reflect.Copy can't be used with such types.
477
- func writeNamedByteArray (val reflect.Value , w * encbuf ) error {
454
+ func writeByteArray (val reflect.Value , w * encbuf ) error {
478
455
if ! val .CanAddr () {
479
- // Slice requires the value to be addressable.
480
- // Make it addressable by copying.
456
+ // Getting the byte slice of val requires it to be addressable. Make it
457
+ // addressable by copying.
481
458
copy := reflect .New (val .Type ()).Elem ()
482
459
copy .Set (val )
483
460
val = copy
484
461
}
485
- size := val .Len ()
486
- slice := val .Slice (0 , size ).Bytes ()
487
- w .encodeString (slice )
462
+
463
+ slice := byteArrayBytes (val )
464
+ w .encodeStringHeader (len (slice ))
465
+ w .str = append (w .str , slice ... )
488
466
return nil
489
467
}
490
468
0 commit comments