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

feat(math): Upstream GDA based decimal type #20085

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1,073 changes: 196 additions & 877 deletions math/dec.go

Large diffs are not rendered by default.

98 changes: 98 additions & 0 deletions math/dec_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package math

import (
"testing"
)

func BenchmarkCompareLegacyDecAndNewDec(b *testing.B) {
samricotta marked this conversation as resolved.
Show resolved Hide resolved
legacyB1 := LegacyNewDec(100)
legacyB2 := LegacyNewDec(5)
newB1 := NewDecFromInt64(100)
newB2 := NewDecFromInt64(5)

b.Run("LegacyDec", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = legacyB1.Quo(legacyB2)
}
})

b.Run("NewDec", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = newB1.Quo(newB2)
}
})
}

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

b.Run("LegacyDec", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = legacyB1.Quo(LegacyNewDec(1))
}
})

b.Run("NewDec", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = newB1.QuoInteger(NewDecFromInt64(1))
}
})
Comment on lines +32 to +42
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These benchmarks are wrong, they benchmark the dec allocation along with Quo by one!

So we should allocate the one dec outside the loop!

But to do a useful benchmark, we need numbers at far bigger word sizes, not single word!

}

func BenchmarkCompareLegacyAddAndDecAdd(b *testing.B) {
legacyB1 := LegacyNewDec(100)
legacyB2 := LegacyNewDec(5)
newB1 := NewDecFromInt64(100)
newB2 := NewDecFromInt64(5)

b.Run("LegacyDec", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = legacyB1.Add(legacyB2)
}
})

b.Run("NewDec", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = newB1.Add(newB2)
}
})
}

func BenchmarkCompareLegacySubAndDecMul(b *testing.B) {
legacyB1 := LegacyNewDec(100)
legacyB2 := LegacyNewDec(5)
newB1 := NewDecFromInt64(100)
newB2 := NewDecFromInt64(5)

b.Run("LegacyDec", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = legacyB1.Mul(legacyB2)
}
})

b.Run("NewDec", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = newB1.Mul(newB2)
}
})
}

func BenchmarkCompareLegacySubAndDecSub(b *testing.B) {
legacyB1 := LegacyNewDec(100)
legacyB2 := LegacyNewDec(5)
newB1 := NewDecFromInt64(100)
newB2 := NewDecFromInt64(5)

b.Run("LegacyDec", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = legacyB1.Sub(legacyB2)
}
})

b.Run("NewDec", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = newB1.Sub(newB2)
}
})
}