Jacek Sieka b3cb51e89e
Speed up evm stack (#2881)
The EVM stack is a hot spot in EVM execution and we end up paying a nim
seq tax in several ways, adding up to ~5% of execution time:

* on initial allocation, all bytes get zeroed - this means we have to
choose between allocating a full stack or just a partial one and then
growing it
* pushing and popping introduce additional zeroing
* reallocations on growth copy + zero - expensive again!
* redundant range checking on every operation reducing inlining etc

Here a custom stack using C memory is instroduced:

* no zeroing on allocation
* full stack allocated on EVM startup -> no reallocation during
execution
* fast push/pop - no zeroing again
* 32-byte alignment - this makes it easier for the compiler to use
vector instructions
* no stack allocated for precompiles (these never use it anyway)

Of course, this change also means we have to manage memory manually -
for the EVM, this turns out to be not too bad because we already manage
database transactions the same way (they have to be freed "manually") so
we can simply latch on to this mechanism.

While we're at it, this PR also skips database lookup for known
precompiles by resolving such addresses earlier.
2024-11-30 10:07:10 +01:00

180 lines
5.2 KiB
Nim
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Nimbus
# Copyright (c) 2018-2024 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
# http://www.apache.org/licenses/LICENSE-2.0)
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
# http://opensource.org/licenses/MIT)
# at your option. This file may not be copied, modified, or distributed except
# according to those terms.
## EVM Opcode Handlers: Block Data
## ===============================
##
{.push raises: [].}
import
eth/common,
../../computation,
../../stack,
../../evm_errors,
../utils/utils_numeric,
../op_codes,
./oph_defs
when not defined(evmc_enabled):
import ../../state
# ------------------------------------------------------------------------------
# Private, op handlers implementation
# ------------------------------------------------------------------------------
proc blockhashOp(cpt: VmCpt): EvmResultVoid =
## 0x40, Get the hash of one of the 256 most recent complete blocks.
template block256(top, number, conv) =
if number > high(BlockNumber).u256:
conv(zero(UInt256), top)
else:
conv(cpt.getBlockHash(number.truncate(BlockNumber)), top)
cpt.stack.unaryWithTop(block256)
proc coinBaseOp(cpt: VmCpt): EvmResultVoid =
## 0x41, Get the block's beneficiary address.
cpt.stack.push cpt.getCoinbase
proc timestampOp(cpt: VmCpt): EvmResultVoid =
## 0x42, Get the block's timestamp.
cpt.stack.push cpt.getTimestamp
proc blocknumberOp(cpt: VmCpt): EvmResultVoid =
## 0x43, Get the block's number.
cpt.stack.push cpt.getBlockNumber
proc difficultyOp(cpt: VmCpt): EvmResultVoid =
## 0x44, Get the block's difficulty
cpt.stack.push cpt.getDifficulty
proc gasLimitOp(cpt: VmCpt): EvmResultVoid =
## 0x45, Get the block's gas limit
cpt.stack.push cpt.getGasLimit
proc chainIdOp(cpt: VmCpt): EvmResultVoid =
## 0x46, Get current chains EIP-155 unique identifier.
cpt.stack.push cpt.getChainId
proc selfBalanceOp(cpt: VmCpt): EvmResultVoid =
## 0x47, Get current contract's balance.
cpt.stack.push cpt.getBalance(cpt.msg.contractAddress)
proc baseFeeOp(cpt: VmCpt): EvmResultVoid =
## 0x48, Get the block's base fee.
cpt.stack.push cpt.getBaseFee
proc blobHashOp(cpt: VmCpt): EvmResultVoid =
## 0x49, Get current transaction's EIP-4844 versioned hash.
template blob256(top, number, conv) =
let
index = number.safeInt
len = cpt.getVersionedHashesLen
if index < len:
conv(cpt.getVersionedHash(index).data, top)
else:
conv(zero(UInt256), top)
cpt.stack.unaryWithTop(blob256)
proc blobBaseFeeOp(cpt: VmCpt): EvmResultVoid =
## 0x4a, Get the block's base fee.
cpt.stack.push cpt.getBlobBaseFee
# ------------------------------------------------------------------------------
# Public, op exec table entries
# ------------------------------------------------------------------------------
const
VmOpExecBlockData*: seq[VmOpExec] = @[
(opCode: Blockhash, ## 0x40, Hash of some most recent complete block
forks: VmOpAllForks,
name: "blockhash",
info: "Get the hash of one of the 256 most recent complete blocks",
exec: blockhashOp),
(opCode: Coinbase, ## 0x41, Beneficiary address
forks: VmOpAllForks,
name: "coinbase",
info: "Get the block's beneficiary address",
exec: coinBaseOp),
(opCode: Timestamp, ## 0x42, Block timestamp.
forks: VmOpAllForks,
name: "timestamp",
info: "Get the block's timestamp",
exec: timestampOp),
(opCode: Number, ## 0x43, Block number
forks: VmOpAllForks,
name: "blockNumber",
info: "Get the block's number",
exec: blocknumberOp),
(opCode: Difficulty, ## 0x44, Block difficulty
forks: VmOpAllForks,
name: "difficulty",
info: "Get the block's difficulty",
exec: difficultyOp),
(opCode: GasLimit, ## 0x45, Block gas limit
forks: VmOpAllForks,
name: "gasLimit",
info: "Get the block's gas limit",
exec: gasLimitOp),
(opCode: ChainIdOp, ## 0x46, EIP-155 chain identifier
forks: VmOpIstanbulAndLater,
name: "chainId",
info: "Get current chains EIP-155 unique identifier",
exec: chainIdOp),
(opCode: SelfBalance, ## 0x47, Contract balance.
forks: VmOpIstanbulAndLater,
name: "selfBalance",
info: "Get current contract's balance",
exec: selfBalanceOp),
(opCode: BaseFee, ## 0x48, EIP-1559 Block base fee.
forks: VmOpLondonAndLater,
name: "baseFee",
info: "Get current block's EIP-1559 base fee",
exec: baseFeeOp),
(opCode: BlobHash, ## 0x49, EIP-4844 Transaction versioned hash
forks: VmOpCancunAndLater,
name: "blobHash",
info: "Get current transaction's EIP-4844 versioned hash",
exec: blobHashOp),
(opCode: BlobBaseFee, ## 0x4a, EIP-7516 Returns the current data-blob base-fee
forks: VmOpCancunAndLater,
name: "blobBaseFee",
info: "Returns the current data-blob base-fee",
exec: blobBaseFeeOp)]
# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------