Skip to content

Commit

Permalink
add fast path for byte arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
magiconair committed Mar 14, 2022
1 parent 3688c80 commit b84b1f1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ua/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ func decodeArray(b []byte, val reflect.Value, name string) (int, error) {
elemType := val.Type().Elem()
// fmt.Println("elemType: ", elemType.String())

// fast path for []byte
if elemType.Kind() == reflect.Uint8 {
// fmt.Println("decode: []byte fast path")
reflect.Copy(val, reflect.ValueOf(buf.ReadN(int(n))))
return buf.Pos(), buf.Error()
}

pos := buf.Pos()
// a is a pointer to an array [n]*Foo, where n is know at compile time
a := reflect.New(val.Type()).Elem()
Expand Down
9 changes: 9 additions & 0 deletions ua/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ func writeArray(val reflect.Value, name string) ([]byte, error) {

buf.WriteUint32(uint32(val.Len()))

// fast path for []byte
if val.Type().Elem().Kind() == reflect.Uint8 {
// fmt.Println("encode: []byte fast path")
b := make([]byte, val.Len())
reflect.Copy(reflect.ValueOf(b), val)
buf.Write(b)
return buf.Bytes(), buf.Error()
}

// loop over elements
// we write all the elements, also the zero values
for i := 0; i < val.Len(); i++ {
Expand Down

0 comments on commit b84b1f1

Please sign in to comment.