mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-23 13:41:37 +00:00
bigint: don't allocate in ToPreallocatedBytes() for negative numbers
In-place modifications are somewhat dangerous, but yet another allocation is quite costly.
This commit is contained in:
parent
3509523baf
commit
3945e81857
1 changed files with 6 additions and 8 deletions
|
@ -112,18 +112,16 @@ func ToPreallocatedBytes(n *big.Int, data []byte) []byte {
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
var ws []big.Word
|
if sign < 0 {
|
||||||
if sign == 1 {
|
n.Add(n, bigOne)
|
||||||
ws = n.Bits()
|
defer func() { n.Sub(n, bigOne) }()
|
||||||
} else {
|
if n.Sign() == 0 { // n == -1
|
||||||
n1 := new(big.Int).Add(n, bigOne)
|
|
||||||
if n1.Sign() == 0 { // n == -1
|
|
||||||
return append(data, 0xFF)
|
return append(data, 0xFF)
|
||||||
}
|
}
|
||||||
|
|
||||||
ws = n1.Bits()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var ws = n.Bits()
|
||||||
|
|
||||||
lb := len(ws) * wordSizeBytes
|
lb := len(ws) * wordSizeBytes
|
||||||
if c := cap(data); c < lb {
|
if c := cap(data); c < lb {
|
||||||
data = make([]byte, lb, lb+1)
|
data = make([]byte, lb, lb+1)
|
||||||
|
|
Loading…
Reference in a new issue