Skip to content

Commit a816e75

Browse files
committed
core/vm: improved push instructions
Improved push instructions by removing unnecessary big int allocations and by making it int instead of big.Int
1 parent 3ee75be commit a816e75

2 files changed

Lines changed: 48 additions & 35 deletions

File tree

‎core/vm/instructions.go‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -706,10 +706,23 @@ func makeLog(size int) executionFunc {
706706
}
707707

708708
// make push instruction function
709-
func makePush(size uint64, bsize *big.Int) executionFunc {
709+
func makePush(size uint64, pushByteSize int) executionFunc {
710710
return func(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *Stack) ([]byte, error) {
711-
byts := getData(contract.Code, evm.interpreter.intPool.get().SetUint64(*pc+1), bsize)
712-
stack.push(new(big.Int).SetBytes(byts))
711+
codeLen := len(contract.Code)
712+
713+
startMin := codeLen
714+
if int(*pc+1) < startMin {
715+
startMin = int(*pc + 1)
716+
}
717+
718+
endMin := codeLen
719+
if startMin+pushByteSize < endMin {
720+
endMin = startMin + pushByteSize
721+
}
722+
723+
integer := evm.interpreter.intPool.get()
724+
stack.push(integer.SetBytes(common.RightPadBytes(contract.Code[startMin:endMin], pushByteSize)))
725+
713726
*pc += size
714727
return nil, nil
715728
}

‎core/vm/jump_table.go‎

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -421,193 +421,193 @@ func NewFrontierInstructionSet() [256]operation {
421421
valid: true,
422422
},
423423
PUSH1: {
424-
execute: makePush(1, big.NewInt(1)),
424+
execute: makePush(1, 1),
425425
gasCost: gasPush,
426426
validateStack: makeStackFunc(0, 1),
427427
valid: true,
428428
},
429429
PUSH2: {
430-
execute: makePush(2, big.NewInt(2)),
430+
execute: makePush(2, 2),
431431
gasCost: gasPush,
432432
validateStack: makeStackFunc(0, 1),
433433
valid: true,
434434
},
435435
PUSH3: {
436-
execute: makePush(3, big.NewInt(3)),
436+
execute: makePush(3, 3),
437437
gasCost: gasPush,
438438
validateStack: makeStackFunc(0, 1),
439439
valid: true,
440440
},
441441
PUSH4: {
442-
execute: makePush(4, big.NewInt(4)),
442+
execute: makePush(4, 4),
443443
gasCost: gasPush,
444444
validateStack: makeStackFunc(0, 1),
445445
valid: true,
446446
},
447447
PUSH5: {
448-
execute: makePush(5, big.NewInt(5)),
448+
execute: makePush(5, 5),
449449
gasCost: gasPush,
450450
validateStack: makeStackFunc(0, 1),
451451
valid: true,
452452
},
453453
PUSH6: {
454-
execute: makePush(6, big.NewInt(6)),
454+
execute: makePush(6, 6),
455455
gasCost: gasPush,
456456
validateStack: makeStackFunc(0, 1),
457457
valid: true,
458458
},
459459
PUSH7: {
460-
execute: makePush(7, big.NewInt(7)),
460+
execute: makePush(7, 7),
461461
gasCost: gasPush,
462462
validateStack: makeStackFunc(0, 1),
463463
valid: true,
464464
},
465465
PUSH8: {
466-
execute: makePush(8, big.NewInt(8)),
466+
execute: makePush(8, 8),
467467
gasCost: gasPush,
468468
validateStack: makeStackFunc(0, 1),
469469
valid: true,
470470
},
471471
PUSH9: {
472-
execute: makePush(9, big.NewInt(9)),
472+
execute: makePush(9, 9),
473473
gasCost: gasPush,
474474
validateStack: makeStackFunc(0, 1),
475475
valid: true,
476476
},
477477
PUSH10: {
478-
execute: makePush(10, big.NewInt(10)),
478+
execute: makePush(10, 10),
479479
gasCost: gasPush,
480480
validateStack: makeStackFunc(0, 1),
481481
valid: true,
482482
},
483483
PUSH11: {
484-
execute: makePush(11, big.NewInt(11)),
484+
execute: makePush(11, 11),
485485
gasCost: gasPush,
486486
validateStack: makeStackFunc(0, 1),
487487
valid: true,
488488
},
489489
PUSH12: {
490-
execute: makePush(12, big.NewInt(12)),
490+
execute: makePush(12, 12),
491491
gasCost: gasPush,
492492
validateStack: makeStackFunc(0, 1),
493493
valid: true,
494494
},
495495
PUSH13: {
496-
execute: makePush(13, big.NewInt(13)),
496+
execute: makePush(13, 13),
497497
gasCost: gasPush,
498498
validateStack: makeStackFunc(0, 1),
499499
valid: true,
500500
},
501501
PUSH14: {
502-
execute: makePush(14, big.NewInt(14)),
502+
execute: makePush(14, 14),
503503
gasCost: gasPush,
504504
validateStack: makeStackFunc(0, 1),
505505
valid: true,
506506
},
507507
PUSH15: {
508-
execute: makePush(15, big.NewInt(15)),
508+
execute: makePush(15, 15),
509509
gasCost: gasPush,
510510
validateStack: makeStackFunc(0, 1),
511511
valid: true,
512512
},
513513
PUSH16: {
514-
execute: makePush(16, big.NewInt(16)),
514+
execute: makePush(16, 16),
515515
gasCost: gasPush,
516516
validateStack: makeStackFunc(0, 1),
517517
valid: true,
518518
},
519519
PUSH17: {
520-
execute: makePush(17, big.NewInt(17)),
520+
execute: makePush(17, 17),
521521
gasCost: gasPush,
522522
validateStack: makeStackFunc(0, 1),
523523
valid: true,
524524
},
525525
PUSH18: {
526-
execute: makePush(18, big.NewInt(18)),
526+
execute: makePush(18, 18),
527527
gasCost: gasPush,
528528
validateStack: makeStackFunc(0, 1),
529529
valid: true,
530530
},
531531
PUSH19: {
532-
execute: makePush(19, big.NewInt(19)),
532+
execute: makePush(19, 19),
533533
gasCost: gasPush,
534534
validateStack: makeStackFunc(0, 1),
535535
valid: true,
536536
},
537537
PUSH20: {
538-
execute: makePush(20, big.NewInt(20)),
538+
execute: makePush(20, 20),
539539
gasCost: gasPush,
540540
validateStack: makeStackFunc(0, 1),
541541
valid: true,
542542
},
543543
PUSH21: {
544-
execute: makePush(21, big.NewInt(21)),
544+
execute: makePush(21, 21),
545545
gasCost: gasPush,
546546
validateStack: makeStackFunc(0, 1),
547547
valid: true,
548548
},
549549
PUSH22: {
550-
execute: makePush(22, big.NewInt(22)),
550+
execute: makePush(22, 22),
551551
gasCost: gasPush,
552552
validateStack: makeStackFunc(0, 1),
553553
valid: true,
554554
},
555555
PUSH23: {
556-
execute: makePush(23, big.NewInt(23)),
556+
execute: makePush(23, 23),
557557
gasCost: gasPush,
558558
validateStack: makeStackFunc(0, 1),
559559
valid: true,
560560
},
561561
PUSH24: {
562-
execute: makePush(24, big.NewInt(24)),
562+
execute: makePush(24, 24),
563563
gasCost: gasPush,
564564
validateStack: makeStackFunc(0, 1),
565565
valid: true,
566566
},
567567
PUSH25: {
568-
execute: makePush(25, big.NewInt(25)),
568+
execute: makePush(25, 25),
569569
gasCost: gasPush,
570570
validateStack: makeStackFunc(0, 1),
571571
valid: true,
572572
},
573573
PUSH26: {
574-
execute: makePush(26, big.NewInt(26)),
574+
execute: makePush(26, 26),
575575
gasCost: gasPush,
576576
validateStack: makeStackFunc(0, 1),
577577
valid: true,
578578
},
579579
PUSH27: {
580-
execute: makePush(27, big.NewInt(27)),
580+
execute: makePush(27, 27),
581581
gasCost: gasPush,
582582
validateStack: makeStackFunc(0, 1),
583583
valid: true,
584584
},
585585
PUSH28: {
586-
execute: makePush(28, big.NewInt(28)),
586+
execute: makePush(28, 28),
587587
gasCost: gasPush,
588588
validateStack: makeStackFunc(0, 1),
589589
valid: true,
590590
},
591591
PUSH29: {
592-
execute: makePush(29, big.NewInt(29)),
592+
execute: makePush(29, 29),
593593
gasCost: gasPush,
594594
validateStack: makeStackFunc(0, 1),
595595
valid: true,
596596
},
597597
PUSH30: {
598-
execute: makePush(30, big.NewInt(30)),
598+
execute: makePush(30, 30),
599599
gasCost: gasPush,
600600
validateStack: makeStackFunc(0, 1),
601601
valid: true,
602602
},
603603
PUSH31: {
604-
execute: makePush(31, big.NewInt(31)),
604+
execute: makePush(31, 31),
605605
gasCost: gasPush,
606606
validateStack: makeStackFunc(0, 1),
607607
valid: true,
608608
},
609609
PUSH32: {
610-
execute: makePush(32, big.NewInt(32)),
610+
execute: makePush(32, 32),
611611
gasCost: gasPush,
612612
validateStack: makeStackFunc(0, 1),
613613
valid: true,

0 commit comments

Comments
 (0)