Skip to content

perf(levm): improve codecopy #3497

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

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Perf

### 2025-07-07

- Improve CODECOPY opdcode handling [#3497](https://github.com/lambdaclass/ethrex/pull/3497)

### 2025-06-30

- Use a stack pool [#3386](https://github.com/lambdaclass/ethrex/pull/3386)
Expand Down
48 changes: 35 additions & 13 deletions crates/vm/levm/src/opcode_handlers/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,27 +210,49 @@ impl<'a> VM<'a> {
return Ok(OpcodeResult::Continue { pc_increment: 1 });
}

let mut data = vec![0u8; size];
// Here we verified indexing is in bounds.
#[expect(clippy::indexing_slicing, clippy::arithmetic_side_effects)]
if code_offset < current_call_frame.bytecode.len().into() {
let code_offset: usize = code_offset
.try_into()
.map_err(|_| InternalError::TypeConversion)?;

for (i, byte) in current_call_frame
.bytecode
.iter()
.skip(code_offset)
.take(size)
.enumerate()
{
if let Some(data_byte) = data.get_mut(i) {
*data_byte = *byte;
}
Comment on lines -219 to -228
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious: did we try changing this for into calls to copy_from_slice without using fixed-size arrays? How did the performance of that differ to the current approach?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would always allocate a vec then, this would depend heavily on the bench, if we do small sized and repeated codecopies the stack fixed size array will be faster.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The project already imports tinyvec and smallvec, why not use one of those instead?

let available = size.min(
current_call_frame
.bytecode
.len()
.saturating_sub(code_offset),
);

if size <= 32 {
let mut data = [0u8; 32];

data[..available].copy_from_slice(
&current_call_frame.bytecode[code_offset..code_offset + available],
);

memory::try_store_data(
&mut current_call_frame.memory,
destination_offset,
&data[..size],
)?;
} else {
let mut data = vec![0u8; size];

data[..available].copy_from_slice(
&current_call_frame.bytecode[code_offset..code_offset + available],
);

memory::try_store_data(&mut current_call_frame.memory, destination_offset, &data)?;
}
} else {
memory::try_store_data(
&mut current_call_frame.memory,
destination_offset,
&vec![0u8; size],
)?;
}

memory::try_store_data(&mut current_call_frame.memory, destination_offset, &data)?;

Ok(OpcodeResult::Continue { pc_increment: 1 })
}

Expand Down
Loading