Skip to content

Commit

Permalink
perf: use Bytes in RecordedMemory (#126)
Browse files Browse the repository at this point in the history
Makes #84 worth it,
helps with parity memory traces
  • Loading branch information
DaniPopes authored May 15, 2024
1 parent fb5d095 commit 5c47bc8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
5 changes: 1 addition & 4 deletions src/tracing/builder/parity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,10 +375,7 @@ impl ParityTraceBuilder {
let maybe_memory = if step.memory.is_empty() {
None
} else {
Some(MemoryDelta {
off: step.memory_size,
data: step.memory.as_bytes().to_vec().into(),
})
Some(MemoryDelta { off: step.memory_size, data: step.memory.as_bytes().clone() })
};

let maybe_execution = Some(VmExecutedOperation {
Expand Down
2 changes: 1 addition & 1 deletion src/tracing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ impl TracingInspector {
let memory = self
.config
.record_memory_snapshots
.then(|| RecordedMemory::new(interp.shared_memory.context_memory().to_vec()))
.then(|| RecordedMemory::new(interp.shared_memory.context_memory()))
.unwrap_or_default();
let stack = if self.config.record_stack_snapshots.is_full() {
Some(interp.stack.data().clone())
Expand Down
23 changes: 17 additions & 6 deletions src/tracing/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,23 +628,34 @@ pub struct StorageChange {
/// This is a wrapper around the [SharedMemory](revm::interpreter::SharedMemory) context memory.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct RecordedMemory(pub(crate) Vec<u8>);
pub struct RecordedMemory(pub(crate) Bytes);

impl RecordedMemory {
#[inline]
pub(crate) fn new(mem: Vec<u8>) -> Self {
Self(mem)
pub(crate) fn new(mem: &[u8]) -> Self {
Self(Bytes::copy_from_slice(mem))
}

/// Returns the memory as a byte slice
#[inline]
pub fn as_bytes(&self) -> &[u8] {
pub fn as_bytes(&self) -> &Bytes {
&self.0
}

/// Returns the memory as a byte vector
#[inline]
pub(crate) fn resize(&mut self, size: usize) {
self.0.resize(size, 0);
pub fn into_bytes(self) -> Bytes {
self.0
}

#[inline]
pub(crate) fn resize(&mut self, len: usize) {
if len <= self.len() {
return self.0.truncate(len);
}
let mut data = self.0.to_vec();
data.resize(len, 0);
self.0 = Bytes::from(data);
}

/// Returns the size of the memory
Expand Down

0 comments on commit 5c47bc8

Please sign in to comment.