stasatdaglabs e9e1ef4772
[NOD-1006] Make use of a pool to avoid excessive allocation of big.Ints (#722)
* [NOD-1006] Make CompactToBig take an out param so that we can reuse the same big.Int in averageTarget.

* [NOD-1006] Fix merge errors.

* [NOD-1006] Use CompactToBigWithDestination only in averageTarget.

* [NOD-1006] Fix refactor errors.

* [NOD-1006] Fix refactor errors.

* [NOD-1006] Optimize averageTarget with a big.Int pool.

* [NOD-1006] Defer releasing bigInts.

* [NOD-1006] Use a pool for requiredDifficulty as well.

* [NOD-1006] Move the big int pool to utils.

* [NOD-1006] Remove unnecessary line.
2020-05-19 16:29:21 +03:00

26 lines
433 B
Go

package bigintpool
import (
"math/big"
"sync"
)
var bigIntPool = sync.Pool{
New: func() interface{} {
return big.NewInt(0)
},
}
// Acquire acquires a big.Int from the pool and
// initializes it to x.
func Acquire(x int64) *big.Int {
bigInt := bigIntPool.Get().(*big.Int)
bigInt.SetInt64(x)
return bigInt
}
// Release returns the given big.Int to the pool.
func Release(toRelease *big.Int) {
bigIntPool.Put(toRelease)
}