mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-22 20:40:48 +01:00
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(...)`
This commit is contained in:
parent
cd57bf11a4
commit
bc96f543c2
1 changed files with 3 additions and 3 deletions
|
@ -120,7 +120,7 @@ func NewFromInt32(value int32) Decimal {
|
||||||
// NewFromBigInt returns a new Decimal from a big.Int, value * 10 ^ exp
|
// NewFromBigInt returns a new Decimal from a big.Int, value * 10 ^ exp
|
||||||
func NewFromBigInt(value *big.Int, exp int32) Decimal {
|
func NewFromBigInt(value *big.Int, exp int32) Decimal {
|
||||||
return Decimal{
|
return Decimal{
|
||||||
value: big.NewInt(0).Set(value),
|
value: new(big.Int).Set(value),
|
||||||
exp: exp,
|
exp: exp,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -831,7 +831,7 @@ func (d Decimal) IsInteger() bool {
|
||||||
// When the exponent is negative we have to check every number after the decimal place
|
// When the exponent is negative we have to check every number after the decimal place
|
||||||
// If all of them are zeroes, we are sure that given decimal can be represented as an integer
|
// If all of them are zeroes, we are sure that given decimal can be represented as an integer
|
||||||
var r big.Int
|
var r big.Int
|
||||||
q := big.NewInt(0).Set(d.value)
|
q := new(big.Int).Set(d.value)
|
||||||
for z := abs(d.exp); z > 0; z-- {
|
for z := abs(d.exp); z > 0; z-- {
|
||||||
q.QuoRem(q, tenInt, &r)
|
q.QuoRem(q, tenInt, &r)
|
||||||
if r.Cmp(zeroInt) != 0 {
|
if r.Cmp(zeroInt) != 0 {
|
||||||
|
@ -949,7 +949,7 @@ func (d Decimal) Exponent() int32 {
|
||||||
func (d Decimal) Coefficient() *big.Int {
|
func (d Decimal) Coefficient() *big.Int {
|
||||||
d.ensureInitialized()
|
d.ensureInitialized()
|
||||||
// we copy the coefficient so that mutating the result does not mutate the Decimal.
|
// we copy the coefficient so that mutating the result does not mutate the Decimal.
|
||||||
return big.NewInt(0).Set(d.value)
|
return new(big.Int).Set(d.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CoefficientInt64 returns the coefficient of the decimal as int64. It is scaled by 10^Exponent()
|
// CoefficientInt64 returns the coefficient of the decimal as int64. It is scaled by 10^Exponent()
|
||||||
|
|
Loading…
Reference in a new issue