From 2cdb9cd5ac418feba907e5ecac9df35a1a716adf Mon Sep 17 00:00:00 2001 From: thorfour Date: Thu, 22 Jun 2023 16:11:59 -0500 Subject: [PATCH] Value.Bytes() return value directly 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. --- value.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/value.go b/value.go index 3c102e5e..2fb37fbe 100644 --- a/value.go +++ b/value.go @@ -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. //