mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-25 06:06:44 +00:00
remove a lot of allocations
This commit is contained in:
parent
23b254c892
commit
4bc38e05e7
1 changed files with 44 additions and 27 deletions
|
|
@ -396,24 +396,34 @@ var (
|
||||||
// else: return x ** 2 // 16 + 480 * x - 199680
|
// else: return x ** 2 // 16 + 480 * x - 199680
|
||||||
//
|
//
|
||||||
// where is x is max(length_of_MODULUS, length_of_BASE)
|
// where is x is max(length_of_MODULUS, length_of_BASE)
|
||||||
func byzantiumMultComplexity(x *big.Int) *big.Int {
|
func byzantiumMultComplexity(x uint64) *big.Int {
|
||||||
switch {
|
switch {
|
||||||
case x.Cmp(big64) <= 0:
|
case x <= 64:
|
||||||
x.Mul(x, x) // x ** 2
|
return new(big.Int).SetUint64(x * x)
|
||||||
case x.Cmp(big1024) <= 0:
|
case x <= 1024:
|
||||||
// (x ** 2 // 4 ) + ( 96 * x - 3072)
|
// x^2 / 4 + 96*x - 3072
|
||||||
x = new(big.Int).Add(
|
result := x*x/4 + 96*x - 3072
|
||||||
new(big.Int).Rsh(new(big.Int).Mul(x, x), 2),
|
return new(big.Int).SetUint64(result)
|
||||||
new(big.Int).Sub(new(big.Int).Mul(big96, x), big3072),
|
|
||||||
)
|
|
||||||
default:
|
default:
|
||||||
// (x ** 2 // 16) + (480 * x - 199680)
|
// For large x, use big.Int arithmetic to avoid overflow
|
||||||
x = new(big.Int).Add(
|
// x^2 / 16 + 480*x - 199680
|
||||||
new(big.Int).Rsh(new(big.Int).Mul(x, x), 4),
|
xBig := new(big.Int).SetUint64(x)
|
||||||
new(big.Int).Sub(new(big.Int).Mul(big480, x), big199680),
|
|
||||||
)
|
// Calculate x^2
|
||||||
|
xSquared := new(big.Int).Mul(xBig, xBig)
|
||||||
|
|
||||||
|
// Calculate x^2 / 16 (right shift by 4 bits)
|
||||||
|
xSquaredDiv16 := new(big.Int).Rsh(xSquared, 4)
|
||||||
|
|
||||||
|
// Calculate 480 * x
|
||||||
|
x480 := new(big.Int).Mul(big480, xBig)
|
||||||
|
|
||||||
|
// Calculate 480 * x - 199680
|
||||||
|
x480Minus199680 := new(big.Int).Sub(x480, big199680)
|
||||||
|
|
||||||
|
// Add the two parts together
|
||||||
|
return new(big.Int).Add(xSquaredDiv16, x480Minus199680)
|
||||||
}
|
}
|
||||||
return x
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// berlinMultComplexity implements the multiplication complexity formula for Berlin.
|
// berlinMultComplexity implements the multiplication complexity formula for Berlin.
|
||||||
|
|
@ -423,18 +433,28 @@ func byzantiumMultComplexity(x *big.Int) *big.Int {
|
||||||
// ceiling(x/8)^2
|
// ceiling(x/8)^2
|
||||||
//
|
//
|
||||||
// where is x is max(length_of_MODULUS, length_of_BASE)
|
// where is x is max(length_of_MODULUS, length_of_BASE)
|
||||||
func berlinMultComplexity(x *big.Int) *big.Int {
|
func berlinMultComplexity(x uint64) *big.Int {
|
||||||
x = new(big.Int).Add(x, big7) // x + 7
|
// TODO: The preceding line is too smart.
|
||||||
x = new(big.Int).Rsh(x, 3) // (x + 7) / 8
|
// TODO: The issue is that (x+7) / 8 can overflow
|
||||||
return new(big.Int).Mul(x, x) // ((x + 7) / 8) ^ 2
|
// TODO: if x > 2^64 - 7
|
||||||
|
ceilDiv8 := (x >> 3) + ((x&7 + 7) >> 3) // safe ceil(x / 8)
|
||||||
|
z := new(big.Int).SetUint64(ceilDiv8)
|
||||||
|
return new(big.Int).Mul(z, z) // square without overflow
|
||||||
}
|
}
|
||||||
|
// Slow Bigint way (benchmark this)
|
||||||
|
// func berlinMultComplexity(xInt uint64) *big.Int {
|
||||||
|
// x := new(big.Int).SetUint64(xInt)
|
||||||
|
// x = new(big.Int).Add(x, big7) // x + 7
|
||||||
|
// x = new(big.Int).Rsh(x, 3) // (x + 7) / 8
|
||||||
|
// return new(big.Int).Mul(x, x) // ((x + 7) / 8) ^ 2
|
||||||
|
// }
|
||||||
|
|
||||||
// osakaMultComplexity implements the multiplication complexity formula for Osaka.
|
// osakaMultComplexity implements the multiplication complexity formula for Osaka.
|
||||||
//
|
//
|
||||||
// For x <= 32: returns 16
|
// For x <= 32: returns 16
|
||||||
// For x > 32: returns 2 * ceiling(x/8)^2
|
// For x > 32: returns 2 * ceiling(x/8)^2
|
||||||
func osakaMultComplexity(x *big.Int) *big.Int {
|
func osakaMultComplexity(x uint64) *big.Int {
|
||||||
if x.Cmp(big32) <= 0 {
|
if x <= 32 {
|
||||||
return big.NewInt(16)
|
return big.NewInt(16)
|
||||||
}
|
}
|
||||||
// For x > 32, return 2 * berlinMultComplexity(x)
|
// For x > 32, return 2 * berlinMultComplexity(x)
|
||||||
|
|
@ -478,8 +498,7 @@ func byzantiumGasCalc(baseLen, expLen, modLen uint64, expHead *big.Int) uint64 {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate multiplication complexity
|
// Calculate multiplication complexity
|
||||||
// Use SetUint64 to avoid int64 overflow
|
multComplexity := byzantiumMultComplexity(maxLen)
|
||||||
multComplexity := byzantiumMultComplexity(new(big.Int).SetUint64(maxLen))
|
|
||||||
|
|
||||||
// Calculate iteration count
|
// Calculate iteration count
|
||||||
iterationCount := calculateIterationCount(expLen, expHead, byzantiumMultiplier)
|
iterationCount := calculateIterationCount(expLen, expHead, byzantiumMultiplier)
|
||||||
|
|
@ -503,8 +522,7 @@ func berlinGasCalc(baseLen, expLen, modLen uint64, expHead *big.Int) uint64 {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate multiplication complexity
|
// Calculate multiplication complexity
|
||||||
// Use SetUint64 to avoid int64 overflow
|
multComplexity := berlinMultComplexity(maxLen)
|
||||||
multComplexity := berlinMultComplexity(new(big.Int).SetUint64(maxLen))
|
|
||||||
|
|
||||||
// Calculate iteration count
|
// Calculate iteration count
|
||||||
iterationCount := calculateIterationCount(expLen, expHead, berlinMultiplier)
|
iterationCount := calculateIterationCount(expLen, expHead, berlinMultiplier)
|
||||||
|
|
@ -534,8 +552,7 @@ func osakaGasCalc(baseLen, expLen, modLen uint64, expHead *big.Int) uint64 {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate multiplication complexity
|
// Calculate multiplication complexity
|
||||||
// Use SetUint64 to avoid int64 overflow
|
multComplexity := osakaMultComplexity(maxLen)
|
||||||
multComplexity := osakaMultComplexity(new(big.Int).SetUint64(maxLen))
|
|
||||||
|
|
||||||
// Calculate iteration count
|
// Calculate iteration count
|
||||||
iterationCount := calculateIterationCount(expLen, expHead, osakaMultiplier)
|
iterationCount := calculateIterationCount(expLen, expHead, osakaMultiplier)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue