Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize mload instruction to not allocate a temporary vector #97

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion core/src/eval/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub fn mload(state: &mut Machine) -> Control {
pop_u256!(state, index);
try_or_fail!(state.memory.resize_offset(index, U256::from(32)));
let index = as_usize_or_fail!(index);
let value = H256::from_slice(&state.memory.get(index, 32)[..]);
let value = state.memory.get_h256(index);
push!(state, value);
Control::Continue(1)
}
Expand Down
19 changes: 14 additions & 5 deletions core/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{ExitError, ExitFatal};
use alloc::vec::Vec;
use core::cmp::min;
use core::ops::{BitAnd, Not};
use primitive_types::U256;
use primitive_types::{H256, U256};

/// A sequencial memory. It uses Rust's `Vec` for internal
/// representation.
Expand Down Expand Up @@ -82,18 +82,27 @@ impl Memory {
pub fn get(&self, offset: usize, size: usize) -> Vec<u8> {
let mut ret = Vec::new();
ret.resize(size, 0);
self.get_to(offset, &mut ret);
ret
}

/// Get `H256` from a specific offset in memory.
pub fn get_h256(&self, offset: usize) -> H256 {
let mut ret = [0; 32];
self.get_to(offset, &mut ret);
H256(ret)
}

fn get_to(&self, offset: usize, buf: &mut [u8]) {
#[allow(clippy::needless_range_loop)]
for index in 0..size {
for index in 0..buf.len() {
let position = offset + index;
if position >= self.data.len() {
break;
}

ret[index] = self.data[position];
buf[index] = self.data[position];
}

ret
}

/// Set memory region at given offset. The offset and value is considered
Expand Down