mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-22 04:20:50 +01:00
Optimize the MarshalMsgand allocate the memory only once.
This commit is contained in:
parent
c7eaaded9c
commit
6e8c4f56de
2 changed files with 12 additions and 12 deletions
|
@ -7,7 +7,7 @@ import (
|
||||||
func TestMsgPack(t *testing.T) {
|
func TestMsgPack(t *testing.T) {
|
||||||
for _, x := range testTable {
|
for _, x := range testTable {
|
||||||
s := x.short
|
s := x.short
|
||||||
// limit to 31 digits
|
// limit to 31 digits
|
||||||
if len(s) > 31 {
|
if len(s) > 31 {
|
||||||
s = s[:31]
|
s = s[:31]
|
||||||
if s[30] == '.' {
|
if s[30] == '.' {
|
||||||
|
@ -17,15 +17,15 @@ func TestMsgPack(t *testing.T) {
|
||||||
|
|
||||||
// Prepare Test Decimal Data
|
// Prepare Test Decimal Data
|
||||||
amount, err := NewFromString(s)
|
amount, err := NewFromString(s)
|
||||||
if err != nil{
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
s = amount.String()
|
s = amount.String()
|
||||||
|
|
||||||
// MarshalMsg
|
// MarshalMsg
|
||||||
var b []byte
|
var b []byte
|
||||||
out, err := amount.MarshalMsg(b)
|
out, err := amount.MarshalMsg(b)
|
||||||
if err != nil{
|
if err != nil {
|
||||||
t.Errorf("error marshalMsg %s: %v", s, err)
|
t.Errorf("error marshalMsg %s: %v", s, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,12 +44,12 @@ func TestMsgPack(t *testing.T) {
|
||||||
// UnmarshalMsg
|
// UnmarshalMsg
|
||||||
var unmarshalAmount Decimal
|
var unmarshalAmount Decimal
|
||||||
_, err = unmarshalAmount.UnmarshalMsg(out)
|
_, err = unmarshalAmount.UnmarshalMsg(out)
|
||||||
if err != nil{
|
if err != nil {
|
||||||
t.Errorf("error unmarshalMsg %s: %v", s, err)
|
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)",
|
t.Errorf("expected %s, got %s (%s, %d)",
|
||||||
amount.String(), unmarshalAmount.String(),
|
amount.String(), unmarshalAmount.String(),
|
||||||
unmarshalAmount.value.String(), unmarshalAmount.exp)
|
unmarshalAmount.value.String(), unmarshalAmount.exp)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@ func (d Decimal) MarshalMsg(b []byte) ([]byte, error) {
|
||||||
str = str[:sz]
|
str = str[:sz]
|
||||||
}
|
}
|
||||||
|
|
||||||
o, n := ensure(b, 1+sz)
|
o, n := ensure(o, 1+sz)
|
||||||
o[n] = byte(0xa0 | sz)
|
o[n] = byte(0xa0 | sz)
|
||||||
n++
|
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
|
// Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message
|
||||||
func (d Decimal) Msgsize() int {
|
func (d Decimal) Msgsize() int {
|
||||||
return 31
|
return 32
|
||||||
}
|
}
|
||||||
|
|
||||||
// Require ensures that cap(old)-len(old) >= extra.
|
// Require ensures that cap(old)-len(old) >= extra.
|
||||||
|
|
Loading…
Reference in a new issue