Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
Value.Bytes() return value directly
Browse files Browse the repository at this point in the history
Previously the Value.Bytes() function was calling AppendBytes for it's
implementation. This causes uncessary memory to be allocated.

This changes the Bytes function to directly return the values instead of
performing an append to nil.
  • Loading branch information
thorfour committed Jun 22, 2023
1 parent 1fd7f33 commit 2cdb9cd
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion value.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,26 @@ func (v Value) Column() int { return v.column() }
// Bytes returns the binary representation of v.
//
// If v is the null value, an nil byte slice is returned.
func (v Value) Bytes() []byte { return v.AppendBytes(nil) }
func (v Value) Bytes() []byte {
switch v.Kind() {
case Boolean:
buf := [8]byte{}
binary.LittleEndian.PutUint32(buf[:4], v.uint32())
return buf[0:1]
case Int32, Float:
buf := [8]byte{}
binary.LittleEndian.PutUint32(buf[:4], v.uint32())
return buf[:4]
case Int64, Double:
buf := [8]byte{}
binary.LittleEndian.PutUint64(buf[:8], v.uint64())
return buf[:8]
case ByteArray, FixedLenByteArray, Int96:
return v.byteArray()
default:
return nil
}
}

// AppendBytes appends the binary representation of v to b.
//
Expand Down

0 comments on commit 2cdb9cd

Please sign in to comment.