Skip to content

Commit

Permalink
perf: More efficient touch_range
Browse files Browse the repository at this point in the history
  • Loading branch information
zlangley committed Jan 29, 2025
1 parent 442a9df commit 2779a29
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
6 changes: 2 additions & 4 deletions crates/vm/src/system/memory/controller/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ impl<F: PrimeField32> MemoryInterface<F> {
merkle_chip,
..
} => {
for offset in 0..len {
boundary_chip.touch_address(addr_space, pointer + offset);
merkle_chip.touch_address(addr_space, pointer + offset);
}
boundary_chip.touch_range(addr_space, pointer, len);
merkle_chip.touch_range(addr_space, pointer, len);
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions crates/vm/src/system/memory/merkle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,13 @@ impl<const CHUNK: usize, F: PrimeField32> MemoryMerkleChip<CHUNK, F> {
address / CHUNK as u32,
);
}

pub fn touch_range(&mut self, address_space: u32, address: u32, len: u32) {
let as_label = address_space - self.air.memory_dimensions.as_offset;
let first_address_label = address / CHUNK as u32;
let last_address_label = (address + len - 1) / CHUNK as u32;
for address_label in first_address_label..=last_address_label {
self.touch_node(0, as_label, address_label);
}
}
}
8 changes: 8 additions & 0 deletions crates/vm/src/system/memory/persistent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ impl<const CHUNK: usize, F: PrimeField32> PersistentBoundaryChip<F, CHUNK> {
self.touched_labels.touch(address_space, label);
}

pub fn touch_range(&mut self, address_space: u32, pointer: u32, len: u32) {
let start_label = pointer / CHUNK as u32;
let end_label = (pointer + len - 1) / CHUNK as u32;
for label in start_label..=end_label {
self.touched_labels.touch(address_space, label);
}
}

pub fn finalize(
&mut self,
initial_memory: &MemoryImage<F>,
Expand Down
4 changes: 0 additions & 4 deletions crates/vm/src/system/memory/volatile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,6 @@ impl<F: Field> VolatileBoundaryChip<F> {
pub fn touch_address(&mut self, addr_space: u32, pointer: u32) {
self.touched_addresses.insert((addr_space, pointer));
}

pub fn all_addresses(&self) -> Vec<(u32, u32)> {
self.touched_addresses.iter().cloned().collect()
}
}

impl<F: PrimeField32> VolatileBoundaryChip<F> {
Expand Down

0 comments on commit 2779a29

Please sign in to comment.