Skip to content

Commit

Permalink
Remove reflect call when marshalling an empty slice (ava-labs#2363)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Laine authored Feb 17, 2023
1 parent 2a26d89 commit a657e3a
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions codec/reflectcodec/type_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,12 @@ func (c *genericCodec) marshal(value reflect.Value, p *wrappers.Packer, maxSlice
if p.Err != nil {
return p.Err
}
if numElts == 0 {
// Returning here prevents execution of the (expensive) reflect
// calls below which check if the slice is []byte and, if it is,
// the call of value.Bytes()
return nil
}
// If this is a slice of bytes, manually pack the bytes rather
// than calling marshal on each byte. This improves performance.
if elemKind := value.Type().Elem().Kind(); elemKind == reflect.Uint8 {
Expand Down

0 comments on commit a657e3a

Please sign in to comment.