go-ethereum/core/vm
Marius van der Wijden 5cc9137c9c
core/vm: optimize push2 opcode (#31267)
During my benchmarks on Holesky, around 10% of all CPU time was spent in
PUSH2
```
ROUTINE ======================== github.com/ethereum/go-ethereum/core/vm.newFrontierInstructionSet.makePush.func1 in github.com/ethereum/go-ethereum/core/vm/instructions.go
    16.38s     20.35s (flat, cum) 10.31% of Total
     740ms      740ms    976:	return func(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
         .          .    977:		var (
      40ms       40ms    978:			codeLen = len(scope.Contract.Code)
     970ms      970ms    979:			start   = min(codeLen, int(*pc+1))
     200ms      200ms    980:			end     = min(codeLen, start+pushByteSize)
         .          .    981:		)
     670ms      2.39s    982:		a := new(uint256.Int).SetBytes(scope.Contract.Code[start:end])
         .          .    983:
         .          .    984:		// Missing bytes: pushByteSize - len(pushData)
     410ms      410ms    985:		if missing := pushByteSize - (end - start); missing > 0 {
         .          .    986:			a.Lsh(a, uint(8*missing))
         .          .    987:		}
    12.69s     14.94s    988:		scope.Stack.push2(*a)
      10ms       10ms    989:		*pc += size
     650ms      650ms    990:		return nil, nil
         .          .    991:	}
         .          .    992:}
```

Which is quite crazy. We have a handwritten encoder for PUSH1 already,
this PR adds one for PUSH2.

PUSH2 is the second most used opcode as shown here:
https://gist.github.com/shemnon/fb9b292a103abb02d98d64df6fbd35c8 since
it is used by solidity quite significantly. Its used ~20 times as much
as PUSH20 and PUSH32.

# Benchmarks

```
BenchmarkPush/makePush-14         	94196547	        12.27 ns/op	       0 B/op	       0 allocs/op
BenchmarkPush/push-14             	429976924	         2.829 ns/op	       0 B/op	       0 allocs/op
``` 

---------

Co-authored-by: jwasinger <j-wasinger@hotmail.com>
2025-04-08 19:57:45 +02:00
..
program all: update license comments and AUTHORS (#31133) 2025-02-05 23:01:17 +01:00
runtime core/asm: delete assembler/disassembler (#31211) 2025-02-19 06:57:08 -07:00
testdata core/vm: implement EIP-2537 spec updates (#30978) 2025-01-24 15:38:17 +01:00
analysis_eof.go core/vm, cmd/evm: implement eof validation (#30418) 2024-10-02 15:05:50 +02:00
analysis_legacy.go core/vm, cmd/evm: implement eof validation (#30418) 2024-10-02 15:05:50 +02:00
analysis_legacy_test.go core/vm, cmd/evm: implement eof validation (#30418) 2024-10-02 15:05:50 +02:00
common.go all: get rid of custom MaxUint64 and MaxUint64 (#30636) 2024-10-20 14:41:51 +03:00
contract.go core/vm: clean up EVM environmental structure (#31061) 2025-02-18 21:53:33 +08:00
contracts.go core/vm: implement EIP-2537 spec updates (#30978) 2025-01-24 15:38:17 +01:00
contracts_fuzz_test.go eth/tracers: live chain tracing with hooks (#29189) 2024-03-22 18:53:53 +01:00
contracts_test.go core/vm: implement EIP-2537 spec updates (#30978) 2025-01-24 15:38:17 +01:00
doc.go core/vm: remove JIT VM codes (#16362) 2018-03-26 13:48:04 +03:00
eips.go core/vm: clean up EVM environmental structure (#31061) 2025-02-18 21:53:33 +08:00
eof.go all: typos in comments (#30779) 2024-11-22 09:02:45 +01:00
eof_control_flow.go all: update license comments and AUTHORS (#31133) 2025-02-05 23:01:17 +01:00
eof_immediates.go core/vm, cmd/evm: implement eof validation (#30418) 2024-10-02 15:05:50 +02:00
eof_instructions.go core/vm, cmd/evm: implement eof validation (#30418) 2024-10-02 15:05:50 +02:00
eof_test.go all: update license comments and AUTHORS (#31133) 2025-02-05 23:01:17 +01:00
eof_validation.go all: fix some typos in comments and names (#31023) 2025-01-14 14:16:15 +01:00
eof_validation_test.go chore: fix various comments (#31082) 2025-01-28 16:56:23 +01:00
errors.go build: update to golangci-lint 1.61.0 (#30587) 2024-10-14 19:25:22 +02:00
evm.go core/vm: clean up EVM environmental structure (#31061) 2025-02-18 21:53:33 +08:00
gas.go core/vm, cmd/evm: implement eof validation (#30418) 2024-10-02 15:05:50 +02:00
gas_table.go core/{.,state,vm},miner,eth/tracers,tests: implement 7709 with a syscall flag (#31036) 2025-01-29 14:31:25 +01:00
gas_table_test.go core/vm: clean up EVM environmental structure (#31061) 2025-02-18 21:53:33 +08:00
instructions.go core/vm: optimize push2 opcode (#31267) 2025-04-08 19:57:45 +02:00
instructions_test.go core/vm: clean up EVM environmental structure (#31061) 2025-02-18 21:53:33 +08:00
interface.go core/vm: clean up EVM environmental structure (#31061) 2025-02-18 21:53:33 +08:00
interpreter.go core/vm: simplify tracer hook invocation in interpreter loop (#31074) 2025-02-03 18:44:26 +01:00
interpreter_test.go core/vm: clean up EVM environmental structure (#31061) 2025-02-18 21:53:33 +08:00
jump_table.go core/vm: optimize push2 opcode (#31267) 2025-04-08 19:57:45 +02:00
jump_table_export.go params: start osaka fork (#31125) 2025-02-04 15:29:51 +01:00
jump_table_test.go all: update license comments and AUTHORS (#31133) 2025-02-05 23:01:17 +01:00
memory.go core/vm, go.mod: update uint256 and use faster method to write to memory (#30868) 2024-12-17 08:58:26 +01:00
memory_table.go core/vm, cmd/evm: implement eof validation (#30418) 2024-10-02 15:05:50 +02:00
memory_test.go all: update license comments and AUTHORS (#31133) 2025-02-05 23:01:17 +01:00
opcodes.go core/vm: make all opcodes proper type (#30925) 2024-12-17 18:37:29 +01:00
operations_acl.go all: implement eip-7702 set code tx (#30078) 2024-12-16 11:29:37 +01:00
operations_verkle.go core/{.,state,vm},miner,eth/tracers,tests: implement 7709 with a syscall flag (#31036) 2025-01-29 14:31:25 +01:00
stack.go core/vm: improved stack swap performance (#30249) 2024-08-06 14:38:47 +02:00
stack_table.go core/vm: 64 bit memory and gas calculations (#19210) 2019-03-12 11:40:05 +02:00