Compare commits

...

2 commits

Author SHA1 Message Date
Philip Dubé
cf775345f2
Merge 5dbfbaff9d into 2b68c56fe0 2024-04-01 23:05:16 +02:00
Philip Dubé
5dbfbaff9d GobEncode: avoid reallocating initial slice 2024-03-16 05:58:34 +00:00

View file

@ -1516,19 +1516,18 @@ func (d *Decimal) UnmarshalBinary(data []byte) error {
// MarshalBinary implements the encoding.BinaryMarshaler interface. // MarshalBinary implements the encoding.BinaryMarshaler interface.
func (d Decimal) MarshalBinary() (data []byte, err error) { func (d Decimal) MarshalBinary() (data []byte, err error) {
// Write the exponent first since it's a fixed size // exp is written first, but encode value first to know output size
v1 := make([]byte, 4) var valueData []byte
binary.BigEndian.PutUint32(v1, uint32(d.exp)) if valueData, err = d.value.GobEncode(); err != nil {
return nil, err
// Add the value
var v2 []byte
if v2, err = d.value.GobEncode(); err != nil {
return
} }
// Write the exponent in front since it's a fixed size
expData := make([]byte, 4, len(valueData)+4)
binary.BigEndian.PutUint32(expData, uint32(d.exp))
// Return the byte array // Return the byte array
data = append(v1, v2...) return append(expData, valueData...), nil
return
} }
// Scan implements the sql.Scanner interface for database deserialization. // Scan implements the sql.Scanner interface for database deserialization.