Optimize the MarshalMsgand allocate the memory only once.

This commit is contained in:
13981712066 2021-12-06 11:35:43 +08:00
parent c7eaaded9c
commit 6e8c4f56de
2 changed files with 12 additions and 12 deletions

View file

@ -17,7 +17,7 @@ func TestMsgPack(t *testing.T) {
// Prepare Test Decimal Data
amount, err := NewFromString(s)
if err != nil{
if err != nil {
t.Error(err)
}
s = amount.String()
@ -25,7 +25,7 @@ func TestMsgPack(t *testing.T) {
// MarshalMsg
var b []byte
out, err := amount.MarshalMsg(b)
if err != nil{
if err != nil {
t.Errorf("error marshalMsg %s: %v", s, err)
}
@ -44,9 +44,9 @@ func TestMsgPack(t *testing.T) {
// UnmarshalMsg
var unmarshalAmount Decimal
_, err = unmarshalAmount.UnmarshalMsg(out)
if err != nil{
if err != nil {
t.Errorf("error unmarshalMsg %s: %v", s, err)
}else if !unmarshalAmount.Equal(amount) {
} else if !unmarshalAmount.Equal(amount) {
t.Errorf("expected %s, got %s (%s, %d)",
amount.String(), unmarshalAmount.String(),
unmarshalAmount.value.String(), unmarshalAmount.exp)

View file

@ -26,7 +26,7 @@ func (d Decimal) MarshalMsg(b []byte) ([]byte, error) {
str = str[:sz]
}
o, n := ensure(b, 1+sz)
o, n := ensure(o, 1+sz)
o[n] = byte(0xa0 | sz)
n++
@ -54,7 +54,7 @@ func (d *Decimal) UnmarshalMsg(b []byte) ([]byte, error) {
// Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message
func (d Decimal) Msgsize() int {
return 31
return 32
}
// Require ensures that cap(old)-len(old) >= extra.