Skip to content

Commit ac69732

Browse files
committed
core/vm: reduced big int allocations
Reduced big int allocation by making stack items modifiable. Instead of adding items such as `common.Big0` to the stack, `new(big.Int)` is added instead. One must expect that any item that is added to the stack might change.
1 parent 184e9ae commit ac69732

4 files changed

Lines changed: 78 additions & 123 deletions

File tree

‎core/vm/instructions.go‎

Lines changed: 60 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -46,40 +46,33 @@ func opStaticJump(instr instruction, ret *big.Int, env Environment, context *Con
4646

4747
func opAdd(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
4848
x, y := stack.pop(), stack.pop()
49-
50-
stack.push(U256(new(big.Int).Add(x, y)))
49+
stack.push(U256(x.Add(x, y)))
5150
}
5251

5352
func opSub(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
5453
x, y := stack.pop(), stack.pop()
55-
56-
stack.push(U256(new(big.Int).Sub(x, y)))
54+
stack.push(U256(x.Sub(x, y)))
5755
}
5856

5957
func opMul(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
6058
x, y := stack.pop(), stack.pop()
61-
62-
stack.push(U256(new(big.Int).Mul(x, y)))
59+
stack.push(U256(x.Mul(x, y)))
6360
}
6461

6562
func opDiv(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
66-
base := new(big.Int)
6763
x, y := stack.pop(), stack.pop()
68-
6964
if y.Cmp(common.Big0) != 0 {
70-
base.Div(x, y)
65+
stack.push(U256(x.Div(x, y)))
66+
} else {
67+
stack.push(new(big.Int))
7168
}
72-
73-
// pop result back on the stack
74-
stack.push(U256(base))
7569
}
7670

7771
func opSdiv(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
78-
base := new(big.Int)
7972
x, y := S256(stack.pop()), S256(stack.pop())
80-
8173
if y.Cmp(common.Big0) == 0 {
82-
base.Set(common.Big0)
74+
stack.push(new(big.Int))
75+
return
8376
} else {
8477
n := new(big.Int)
8578
if new(big.Int).Mul(x, y).Cmp(common.Big0) < 0 {
@@ -88,35 +81,27 @@ func opSdiv(instr instruction, env Environment, context *Context, memory *Memory
8881
n.SetInt64(1)
8982
}
9083

91-
base.Div(x.Abs(x), y.Abs(y)).Mul(base, n)
84+
res := x.Div(x.Abs(x), y.Abs(y))
85+
res.Mul(res, n)
9286

93-
U256(base)
87+
stack.push(U256(res))
9488
}
95-
96-
stack.push(base)
9789
}
9890

9991
func opMod(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
100-
base := new(big.Int)
10192
x, y := stack.pop(), stack.pop()
102-
10393
if y.Cmp(common.Big0) == 0 {
104-
base.Set(common.Big0)
94+
stack.push(new(big.Int))
10595
} else {
106-
base.Mod(x, y)
96+
stack.push(U256(x.Mod(x, y)))
10797
}
108-
109-
U256(base)
110-
111-
stack.push(base)
11298
}
11399

114100
func opSmod(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
115-
base := new(big.Int)
116101
x, y := S256(stack.pop()), S256(stack.pop())
117102

118103
if y.Cmp(common.Big0) == 0 {
119-
base.Set(common.Big0)
104+
stack.push(new(big.Int))
120105
} else {
121106
n := new(big.Int)
122107
if x.Cmp(common.Big0) < 0 {
@@ -125,177 +110,141 @@ func opSmod(instr instruction, env Environment, context *Context, memory *Memory
125110
n.SetInt64(1)
126111
}
127112

128-
base.Mod(x.Abs(x), y.Abs(y)).Mul(base, n)
113+
res := x.Mod(x.Abs(x), y.Abs(y))
114+
res.Mul(res, n)
129115

130-
U256(base)
116+
stack.push(U256(res))
131117
}
132-
133-
stack.push(base)
134118
}
135119

136120
func opExp(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
137-
base := new(big.Int)
138121
x, y := stack.pop(), stack.pop()
139-
140-
base.Exp(x, y, Pow256)
141-
142-
U256(base)
143-
144-
stack.push(base)
122+
stack.push(U256(x.Exp(x, y, Pow256)))
145123
}
146124

147125
func opSignExtend(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
148126
back := stack.pop()
149127
if back.Cmp(big.NewInt(31)) < 0 {
150128
bit := uint(back.Uint64()*8 + 7)
151129
num := stack.pop()
152-
mask := new(big.Int).Lsh(common.Big1, bit)
130+
mask := back.Lsh(common.Big1, bit)
153131
mask.Sub(mask, common.Big1)
154132
if common.BitTest(num, int(bit)) {
155133
num.Or(num, mask.Not(mask))
156134
} else {
157135
num.And(num, mask)
158136
}
159137

160-
num = U256(num)
161-
162-
stack.push(num)
138+
stack.push(U256(num))
163139
}
164140
}
165141

166142
func opNot(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
167-
stack.push(U256(new(big.Int).Not(stack.pop())))
143+
x := stack.pop()
144+
stack.push(U256(x.Not(x)))
168145
}
169146

170147
func opLt(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
171148
x, y := stack.pop(), stack.pop()
172-
173-
// x < y
174149
if x.Cmp(y) < 0 {
175-
stack.push(common.BigTrue)
150+
stack.push(big.NewInt(1))
176151
} else {
177-
stack.push(common.BigFalse)
152+
stack.push(new(big.Int))
178153
}
179154
}
180155

181156
func opGt(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
182157
x, y := stack.pop(), stack.pop()
183-
184-
// x > y
185158
if x.Cmp(y) > 0 {
186-
stack.push(common.BigTrue)
159+
stack.push(big.NewInt(1))
187160
} else {
188-
stack.push(common.BigFalse)
161+
stack.push(new(big.Int))
189162
}
190163
}
191164

192165
func opSlt(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
193166
x, y := S256(stack.pop()), S256(stack.pop())
194-
195-
// x < y
196167
if x.Cmp(S256(y)) < 0 {
197-
stack.push(common.BigTrue)
168+
stack.push(big.NewInt(1))
198169
} else {
199-
stack.push(common.BigFalse)
170+
stack.push(new(big.Int))
200171
}
201172
}
202173

203174
func opSgt(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
204175
x, y := S256(stack.pop()), S256(stack.pop())
205-
206-
// x > y
207176
if x.Cmp(y) > 0 {
208-
stack.push(common.BigTrue)
177+
stack.push(big.NewInt(1))
209178
} else {
210-
stack.push(common.BigFalse)
179+
stack.push(new(big.Int))
211180
}
212181
}
213182

214183
func opEq(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
215184
x, y := stack.pop(), stack.pop()
216-
217-
// x == y
218185
if x.Cmp(y) == 0 {
219-
stack.push(common.BigTrue)
186+
stack.push(big.NewInt(1))
220187
} else {
221-
stack.push(common.BigFalse)
188+
stack.push(new(big.Int))
222189
}
223190
}
224191

225192
func opIszero(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
226193
x := stack.pop()
227-
if x.Cmp(common.BigFalse) > 0 {
228-
stack.push(common.BigFalse)
194+
if x.Cmp(common.Big0) > 0 {
195+
stack.push(new(big.Int))
229196
} else {
230-
stack.push(common.BigTrue)
197+
stack.push(big.NewInt(1))
231198
}
232199
}
233200

234201
func opAnd(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
235202
x, y := stack.pop(), stack.pop()
236-
237-
stack.push(new(big.Int).And(x, y))
203+
stack.push(x.And(x, y))
238204
}
239205
func opOr(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
240206
x, y := stack.pop(), stack.pop()
241-
242-
stack.push(new(big.Int).Or(x, y))
207+
stack.push(x.Or(x, y))
243208
}
244209
func opXor(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
245210
x, y := stack.pop(), stack.pop()
246-
247-
stack.push(new(big.Int).Xor(x, y))
211+
stack.push(x.Xor(x, y))
248212
}
249213
func opByte(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
250-
base := new(big.Int)
251214
th, val := stack.pop(), stack.pop()
252-
253215
if th.Cmp(big.NewInt(32)) < 0 {
254-
byt := big.NewInt(int64(common.LeftPadBytes(val.Bytes(), 32)[th.Int64()]))
255-
256-
base.Set(byt)
216+
byte := big.NewInt(int64(common.LeftPadBytes(val.Bytes(), 32)[th.Int64()]))
217+
stack.push(byte)
257218
} else {
258-
base.Set(common.BigFalse)
219+
stack.push(new(big.Int))
259220
}
260-
261-
stack.push(base)
262221
}
263222
func opAddmod(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
264-
base := new(big.Int)
265-
x := stack.pop()
266-
y := stack.pop()
267-
z := stack.pop()
268-
223+
x, y, z := stack.pop(), stack.pop(), stack.pop()
269224
if z.Cmp(Zero) > 0 {
270-
add := new(big.Int).Add(x, y)
271-
base.Mod(add, z)
272-
273-
base = U256(base)
225+
add := x.Add(x, y)
226+
add.Mod(add, z)
227+
stack.push(U256(add))
228+
} else {
229+
stack.push(new(big.Int))
274230
}
275-
276-
stack.push(base)
277231
}
278232
func opMulmod(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
279-
base := new(big.Int)
280-
x := stack.pop()
281-
y := stack.pop()
282-
z := stack.pop()
283-
233+
x, y, z := stack.pop(), stack.pop(), stack.pop()
284234
if z.Cmp(Zero) > 0 {
285-
mul := new(big.Int).Mul(x, y)
286-
base.Mod(mul, z)
287-
288-
U256(base)
235+
mul := x.Mul(x, y)
236+
mul.Mod(mul, z)
237+
stack.push(U256(mul))
238+
} else {
239+
stack.push(new(big.Int))
289240
}
290-
291-
stack.push(base)
292241
}
293242

294243
func opSha3(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
295244
offset, size := stack.pop(), stack.pop()
296245
hash := crypto.Sha3(memory.Get(offset.Int64(), size.Int64()))
297246

298-
stack.push(common.BigD(hash))
247+
stack.push(common.BytesToBig(hash))
299248
}
300249

301250
func opAddress(instr instruction, env Environment, context *Context, memory *Memory, stack *stack) {
@@ -383,7 +332,7 @@ func opBlockhash(instr instruction, env Environment, context *Context, memory *M
383332
if num.Cmp(n) > 0 && num.Cmp(env.BlockNumber()) < 0 {
384333
stack.push(env.GetHash(num.Uint64()).Big())
385334
} else {
386-
stack.push(common.Big0)
335+
stack.push(new(big.Int))
387336
}
388337
}
389338

@@ -497,7 +446,7 @@ func opCreate(instr instruction, env Environment, context *Context, memory *Memo
497446
context.UseGas(context.Gas)
498447
ret, suberr, ref := env.Create(context, input, gas, context.Price, value)
499448
if suberr != nil {
500-
stack.push(common.BigFalse)
449+
stack.push(new(big.Int))
501450

502451
} else {
503452
// gas < len(ret) * Createinstr.dataGas == NO_CODE
@@ -535,10 +484,10 @@ func opCall(instr instruction, env Environment, context *Context, memory *Memory
535484
ret, err := env.Call(context, address, args, gas, context.Price, value)
536485

537486
if err != nil {
538-
stack.push(common.BigFalse)
487+
stack.push(new(big.Int))
539488

540489
} else {
541-
stack.push(common.BigTrue)
490+
stack.push(big.NewInt(1))
542491

543492
memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
544493
}
@@ -566,10 +515,10 @@ func opCallCode(instr instruction, env Environment, context *Context, memory *Me
566515
ret, err := env.CallCode(context, address, args, gas, context.Price, value)
567516

568517
if err != nil {
569-
stack.push(common.BigFalse)
518+
stack.push(new(big.Int))
570519

571520
} else {
572-
stack.push(common.BigTrue)
521+
stack.push(big.NewInt(1))
573522

574523
memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
575524
}

‎core/vm/jit.go‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,10 @@ func jitCalculateGasAndSize(env Environment, context *Context, caller ContextRef
404404

405405
mSize, mStart := stack.data[stack.len()-2], stack.data[stack.len()-1]
406406

407+
add := new(big.Int)
407408
gas.Add(gas, params.LogGas)
408-
gas.Add(gas, new(big.Int).Mul(big.NewInt(int64(n)), params.LogTopicGas))
409-
gas.Add(gas, new(big.Int).Mul(mSize, params.LogDataGas))
409+
gas.Add(gas, add.Mul(big.NewInt(int64(n)), params.LogTopicGas))
410+
gas.Add(gas, add.Mul(mSize, params.LogDataGas))
410411

411412
newMemSize = calcMemSize(mStart, mSize)
412413
case EXP:
@@ -496,18 +497,20 @@ func jitCalculateGasAndSize(env Environment, context *Context, caller ContextRef
496497
newMemSize.Mul(newMemSizeWords, u256(32))
497498

498499
if newMemSize.Cmp(u256(int64(mem.Len()))) > 0 {
500+
// be careful reusing variables here when changing.
501+
// The order has been optimised to reduce allocation
499502
oldSize := toWordSize(big.NewInt(int64(mem.Len())))
500503
pow := new(big.Int).Exp(oldSize, common.Big2, Zero)
501-
linCoef := new(big.Int).Mul(oldSize, params.MemoryGas)
504+
linCoef := oldSize.Mul(oldSize, params.MemoryGas)
502505
quadCoef := new(big.Int).Div(pow, params.QuadCoeffDiv)
503506
oldTotalFee := new(big.Int).Add(linCoef, quadCoef)
504507

505508
pow.Exp(newMemSizeWords, common.Big2, Zero)
506-
linCoef = new(big.Int).Mul(newMemSizeWords, params.MemoryGas)
507-
quadCoef = new(big.Int).Div(pow, params.QuadCoeffDiv)
508-
newTotalFee := new(big.Int).Add(linCoef, quadCoef)
509+
linCoef = linCoef.Mul(newMemSizeWords, params.MemoryGas)
510+
quadCoef = quadCoef.Div(pow, params.QuadCoeffDiv)
511+
newTotalFee := linCoef.Add(linCoef, quadCoef)
509512

510-
fee := new(big.Int).Sub(newTotalFee, oldTotalFee)
513+
fee := newTotalFee.Sub(newTotalFee, oldTotalFee)
511514
gas.Add(gas, fee)
512515
}
513516
}

0 commit comments

Comments
 (0)