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

#25 Optimize stack and calldataload #19

Merged
merged 2 commits into from
Jun 10, 2021
Merged
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
16 changes: 5 additions & 11 deletions core/src/eval/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,11 @@ pub fn calldataload(state: &mut Machine) -> Control {
pop_u256!(state, index);
trace_op!("CallDataLoad: {}", index);

let index = as_usize_or_fail!(index);
let len = if index > state.data.len() { 0 } else { min(32, state.data.len() - index) };

let mut load = [0u8; 32];
for i in 0..32 {
if let Some(p) = index.checked_add(U256::from(i)) {
if p <= U256::from(usize::max_value()) {
let p = p.as_usize();
if p < state.data.len() {
load[i] = state.data[p];
}
}
}
}
load[0..len].copy_from_slice(&state.data[index..index + len]);

push!(state, H256::from(load));
Control::Continue(1)
Expand Down Expand Up @@ -153,7 +147,7 @@ pub fn msize(state: &mut Machine) -> Control {

pub fn push(state: &mut Machine, n: usize, position: usize) -> Control {
let end = min(position + 1 + n, state.code.len());
let val = U256::from(&state.code[(position + 1)..end]);
let val = U256::from_big_endian_fast(&state.code[(position + 1)..end]);

push_u256!(state, val);
trace_op!("Push [@{}]: {}", state.stack.len() - 1, val);
Expand Down
22 changes: 22 additions & 0 deletions core/src/primitive_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,26 @@ impl<'de> serde::Deserialize<'de> for U256 {

deserializer.deserialize_bytes(Visitor)
}
}


impl U256 {
pub fn into_big_endian_fast(self, buffer: &mut [u8]) {
let data: [u8; 32] = unsafe { core::mem::transmute(self) };

let buffer = &mut buffer[0..32];
buffer.copy_from_slice(&data[..]);
buffer.reverse();
}

pub fn from_big_endian_fast(buffer: &[u8]) -> U256 {
assert!(32 >= buffer.len());

let mut data = [0u8; 32];

data[32 - buffer.len()..32].copy_from_slice(buffer);
data.reverse();

unsafe { core::mem::transmute(data) }
}
}
4 changes: 2 additions & 2 deletions core/src/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl Stack {
self.data.pop()
.map(|d| {
let mut value = H256::default();
d.to_big_endian(&mut value[..]);
d.into_big_endian_fast(&mut value[..]);
value
})
.ok_or(ExitError::StackUnderflow)
Expand All @@ -95,7 +95,7 @@ impl Stack {
if self.data.len() + 1 > self.limit {
return Err(ExitError::StackOverflow)
}
self.data.push(U256::from_big_endian(&value[..]));
self.data.push(U256::from_big_endian_fast(&value[..]));
Ok(())
}

Expand Down
8 changes: 3 additions & 5 deletions runtime/src/eval/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ macro_rules! pop {
macro_rules! pop_u256 {
( $machine:expr, $( $x:ident ),* ) => (
$(
let $x = match $machine.machine.stack_mut().pop() {
Ok(value) => U256::from_big_endian(&value[..]),
let $x = match $machine.machine.stack_mut().pop_u256() {
Ok(value) => value,
Err(e) => return Control::Exit(e.into()),
};
)*
Expand All @@ -43,9 +43,7 @@ macro_rules! push {
macro_rules! push_u256 {
( $machine:expr, $( $x:expr ),* ) => (
$(
let mut value = H256::default();
$x.to_big_endian(&mut value[..]);
match $machine.machine.stack_mut().push(value) {
match $machine.machine.stack_mut().push_u256($x) {
Ok(()) => (),
Err(e) => return Control::Exit(e.into()),
}
Expand Down