Skip to content

Commit bc96f54

Browse files
authored
Reduce memory allocation in case of initialization from big.Int (#252)
* reduced allocs in NewFromBigInt(...) from 3 -> 2 * remaining `big.NewInt(0).Set(...)` -> `new(big.Int).Set(...)`
1 parent cd57bf1 commit bc96f54

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

decimal.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func NewFromInt32(value int32) Decimal {
120120
// NewFromBigInt returns a new Decimal from a big.Int, value * 10 ^ exp
121121
func NewFromBigInt(value *big.Int, exp int32) Decimal {
122122
return Decimal{
123-
value: big.NewInt(0).Set(value),
123+
value: new(big.Int).Set(value),
124124
exp: exp,
125125
}
126126
}
@@ -831,7 +831,7 @@ func (d Decimal) IsInteger() bool {
831831
// When the exponent is negative we have to check every number after the decimal place
832832
// If all of them are zeroes, we are sure that given decimal can be represented as an integer
833833
var r big.Int
834-
q := big.NewInt(0).Set(d.value)
834+
q := new(big.Int).Set(d.value)
835835
for z := abs(d.exp); z > 0; z-- {
836836
q.QuoRem(q, tenInt, &r)
837837
if r.Cmp(zeroInt) != 0 {
@@ -949,7 +949,7 @@ func (d Decimal) Exponent() int32 {
949949
func (d Decimal) Coefficient() *big.Int {
950950
d.ensureInitialized()
951951
// we copy the coefficient so that mutating the result does not mutate the Decimal.
952-
return big.NewInt(0).Set(d.value)
952+
return new(big.Int).Set(d.value)
953953
}
954954

955955
// CoefficientInt64 returns the coefficient of the decimal as int64. It is scaled by 10^Exponent()

0 commit comments

Comments
 (0)