Skip to content

Commit

Permalink
Marshal/unmarshal
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed May 14, 2024
1 parent 10fc6df commit 3f8680d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
8 changes: 8 additions & 0 deletions math/dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,11 @@ func (x Dec) Reduce() (Dec, int) {
_, n := y.dec.Reduce(&x.dec)
return y, n
}

func (d Dec) Marshal() ([]byte, error) {
return d.dec.MarshalText()
}

func (d *Dec) Unmarshal(data []byte) error {
return d.dec.UnmarshalText(data)
}
44 changes: 44 additions & 0 deletions math/dec_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,50 @@ func BenchmarkCompareLegacyDecAndNewDecMul(b *testing.B) {
}
}

func BenchmarkCompareLegacyDecAndNewDecMarshalUnmarshal(b *testing.B) {
specs := map[string]struct {
src string
}{
"small": {
src: "1",
},
"big18": {
src: "999999999999999999",
},
"negative big34": {
src: "9999999999999999999999999999999999",
},
"decimal": {
src: "12345.678901234341",
},
}
for name, spec := range specs {
b.Run(name, func(b *testing.B) {
b.Run("LegacyDec", func(b *testing.B) {
src := LegacyMustNewDecFromStr(spec.src)
b.ResetTimer()
for i := 0; i < b.N; i++ {
bz, err := src.Marshal()
require.NoError(b, err)
var d LegacyDec
require.NoError(b, d.Unmarshal(bz))
}
})

b.Run("NewDec", func(b *testing.B) {
src := must(NewDecFromString(spec.src))
b.ResetTimer()
for i := 0; i < b.N; i++ {
bz, err := src.Marshal()
require.NoError(b, err)
var d Dec
require.NoError(b, d.Unmarshal(bz))
}
})
})
}
}

func BenchmarkCompareLegacyDecAndNewDecQuoInteger(b *testing.B) {
legacyB1 := LegacyNewDec(100)
newB1 := NewDecFromInt64(100)
Expand Down

0 comments on commit 3f8680d

Please sign in to comment.