Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GobEncode: avoid reallocating initial slice #355

Merged
merged 2 commits into from
Apr 3, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -1506,19 +1506,18 @@ func (d *Decimal) UnmarshalBinary(data []byte) error {

// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (d Decimal) MarshalBinary() (data []byte, err error) {
// Write the exponent first since it's a fixed size
v1 := make([]byte, 4)
binary.BigEndian.PutUint32(v1, uint32(d.exp))

// Add the value
var v2 []byte
if v2, err = d.value.GobEncode(); err != nil {
return
// exp is written first, but encode value first to know output size
var valueData []byte
if valueData, err = d.value.GobEncode(); err != nil {
return nil, err
}

// Write the exponent in front since it's a fixed size
serprex marked this conversation as resolved.
Show resolved Hide resolved
expData := make([]byte, 4, len(valueData)+4)
mwoss marked this conversation as resolved.
Show resolved Hide resolved
binary.BigEndian.PutUint32(expData, uint32(d.exp))

// Return the byte array
data = append(v1, v2...)
return
return append(expData, valueData...), nil
}

// Scan implements the sql.Scanner interface for database deserialization.
Expand Down