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: Add public NewFromDecimal which outperforms decimal -> string -> alpacadecimal #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
35 changes: 35 additions & 0 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,38 @@ func BenchmarkRound(b *testing.B) {
_ = result
})
}

func BenchmarkNewFromDecimal(b *testing.B) {
b.Run("alpacadecimal.Decimal.NewFromDecimal", func(b *testing.B) {
d := decimal.New(123, -12)

var result alpacadecimal.Decimal

b.ResetTimer()
for n := 0; n < b.N; n++ {
result = alpacadecimal.NewFromDecimal(d)
}
_ = result
})

b.Run("alpacadecimal.Decimal.RequireFromString", func(b *testing.B) {
d := decimal.New(123, -12)

var result alpacadecimal.Decimal

b.ResetTimer()
for n := 0; n < b.N; n++ {
result = alpacadecimal.RequireFromString(d.String())
}
_ = result
})

b.Run("alpacadecimal.Decimal.New", func(b *testing.B) {
var result alpacadecimal.Decimal
for n := 0; n < b.N; n++ {
result = alpacadecimal.New(123, -12)
}
_ = result
})

}
31 changes: 28 additions & 3 deletions decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,20 +126,28 @@ func Min(first Decimal, rest ...Decimal) Decimal {
// optimized:
// New returns a new fixed-point decimal, value * 10 ^ exp.
func New(value int64, exp int32) Decimal {
d, done := tryOptNew(value, exp)
if done {
return d
}
return newFromDecimal(decimal.New(value, exp))
}

func tryOptNew(value int64, exp int32) (Decimal, bool) {
if exp >= -12 {
if exp <= 0 {
s := pow10Table[-exp]
if value >= minInt*s && value <= maxInt*s {
return Decimal{fixed: value * pow10Table[precision+exp]}
return Decimal{fixed: value * pow10Table[precision+exp]}, true
}
} else if exp <= 6 { // when exp > 6, it would be greater than maxInt
s := pow10Table[exp]
if value >= minInt/s && value <= maxInt/s {
return Decimal{fixed: value * pow10Table[precision+exp]}
return Decimal{fixed: value * pow10Table[precision+exp]}, true
}
}
}
return newFromDecimal(decimal.New(value, exp))
return Decimal{}, false
}

// fallback:
Expand Down Expand Up @@ -1100,6 +1108,23 @@ func (d NullDecimal) Value() (driver.Value, error) {
return d.Decimal.Value()
}

// optimized:
// Create a new alpacadecimal.Decimal from a decimal.Decimal.
// Attempts to set the fixed value if possible.
func NewFromDecimal(d decimal.Decimal) Decimal {
co := d.Coefficient()
if !co.IsInt64() {
return newFromDecimal(d) // fallback
}
value := co.Int64()
exp := d.Exponent()
res, done := tryOptNew(value, exp)
if done {
return res
}
return newFromDecimal(d)
}

// internal implementation
func newFromDecimal(d decimal.Decimal) Decimal {
return Decimal{fallback: &d}
Expand Down
16 changes: 16 additions & 0 deletions decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,22 @@ func TestDecimal(t *testing.T) {
require.Equal(t, x.String(), y.String())
})

t.Run("NewFromDecimal", func(t *testing.T) {
// first, with optimized decimal
x := alpacadecimal.NewFromDecimal(decimal.New(123, -2))
y := alpacadecimal.New(123, -2)
shouldEqual(t, x, y)

// the prior means of conversion from decimal commonly used
y = alpacadecimal.RequireFromString(decimal.New(123, -2).String())
shouldEqual(t, x, y)

// now, with out of optimization range decimal
x = alpacadecimal.NewFromDecimal(decimal.New(123, -13))
y = alpacadecimal.New(123, -13)
shouldEqual(t, x, y)
})

t.Run("NewFromInt", func(t *testing.T) {
x := alpacadecimal.NewFromInt(123)
y, err := alpacadecimal.NewFromString("123")
Expand Down