Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: use a bitvec as jumpmap
  • Loading branch information
onbjerg committed Feb 27, 2023
commit 0741bf601e8df91e530c745d9e8e1a0ed9a5bd38
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion crates/interpreter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ readme = "../../README.md"

[dependencies]
revm-primitives = { path = "../primitives", version="1.0.0", default-features = false }
bitvec = { version = "1", default-features = false }

#utility
derive_more = "0.99"
Expand Down Expand Up @@ -45,10 +46,11 @@ optional_balance_check = ["revm-primitives/optional_balance_check"]
optional_block_gas_limit = ["revm-primitives/optional_block_gas_limit"]
optional_eip3607 = ["revm-primitives/optional_eip3607"]
optional_gas_refund = ["revm-primitives/optional_gas_refund"]
std = ["revm-primitives/std"]
std = ["revm-primitives/std", "bitvec/std"]
serde = [
"dep:serde",
"revm-primitives/serde",
"bitvec/serde",
]
arbitrary = [
"dep:arbitrary",
Expand Down
8 changes: 5 additions & 3 deletions crates/interpreter/src/interpreter/analysis.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::opcode;
use crate::primitives::{Bytecode, BytecodeState, Bytes, B256};
use revm_primitives::JumpMap;
use bitvec::prelude::{bitvec};
use alloc::sync::Arc;

/// Perform bytecode analysis.
///
Expand Down Expand Up @@ -29,7 +31,7 @@ pub fn to_analysed(bytecode: Bytecode) -> Bytecode {

/// Analyzs bytecode to build a jump map.
fn analyze(code: &[u8]) -> JumpMap {
let mut jumps = vec![false; code.len()];
let mut jumps = bitvec![0; code.len()];

let mut index = 0;
while index < code.len() {
Expand All @@ -38,14 +40,14 @@ fn analyze(code: &[u8]) -> JumpMap {
index += match opcode {
opcode::PUSH1..=opcode::PUSH32 => ((opcode - opcode::PUSH1) + 2) as usize,
opcode::JUMPDEST => {
jumps[index] = true;
jumps.set(index, true);
1
}
_ => 1,
};
}

JumpMap(jumps.into())
JumpMap(Arc::new(jumps))
}

#[derive(Clone)]
Expand Down
4 changes: 3 additions & 1 deletion crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ primitive-types = { version = "0.12", default-features = false }
rlp = { version = "0.5", default-features = false } # used for create2 address calculation
ruint = { version = "1.7.0", features = ["primitive-types", "rlp"] }
auto_impl = "1.0"
bitvec = { version = "1", default-features = false }

# bits B256 B160 crate
fixed-hash = { version = "0.8", default-features = false, features = [
Expand Down Expand Up @@ -63,13 +64,14 @@ optional_balance_check = []
optional_block_gas_limit = []
optional_eip3607 = []
optional_gas_refund = []
std = ["bytes/std", "rlp/std", "hex/std"]
std = ["bytes/std", "rlp/std", "hex/std", "bitvec/std"]
serde = [
"dep:serde",
"hex/serde",
"hashbrown/serde",
"ruint/serde",
"bytes/serde",
"bitvec/serde",
]
arbitrary = [
"ruint/arbitrary",
Expand Down
6 changes: 4 additions & 2 deletions crates/primitives/src/bytecode.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::{keccak256, B256, KECCAK_EMPTY};
use alloc::{sync::Arc, vec, vec::Vec};
use bitvec::vec::BitVec;
use bytes::Bytes;
use bitvec::prelude::{bitvec, Lsb0};

/// A map of valid `jump` destinations.
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct JumpMap(pub Arc<[bool]>);
pub struct JumpMap(pub Arc<BitVec>);

impl JumpMap {
/// Check if `pc` is a valid jump destination.
Expand Down Expand Up @@ -45,7 +47,7 @@ impl Bytecode {
hash: KECCAK_EMPTY,
state: BytecodeState::Analysed {
len: 0,
jump_map: JumpMap(Arc::new([false])),
jump_map: JumpMap(Arc::new(bitvec![0])),
},
}
}
Expand Down
5 changes: 2 additions & 3 deletions crates/revm/src/inspector/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ impl<DB: Database> Inspector<DB> for GasInspector {
#[cfg(not(feature = "no_gas_measuring"))]
fn step(
&mut self,
interp: &mut crate::interpreter::Interpreter,
data: &mut EVMData<'_, DB>,
_interp: &mut crate::interpreter::Interpreter,
_data: &mut EVMData<'_, DB>,
_is_static: bool,
) -> InstructionResult {
InstructionResult::Continue
Expand All @@ -54,7 +54,6 @@ impl<DB: Database> Inspector<DB> for GasInspector {
_is_static: bool,
_eval: InstructionResult,
) -> InstructionResult {
let pc = interp.program_counter();
let last_gas = self.gas_remaining;
self.gas_remaining = interp.gas.remaining();
if last_gas > self.gas_remaining {
Expand Down