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

perf: implement mem_eq and use it for set_add #1198

Merged
merged 1 commit into from
Jun 1, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

#### Upcoming Changes

* feat: implement `mem_eq` function to test for equality of two ranges in memory [#1198](https://github.com/lambdaclass/cairo-rs/pull/1198)
* perf: use `mem_eq` in `set_add` [#1198](https://github.com/lambdaclass/cairo-rs/pull/1198)

* feat: wrap big variants of `HintError`, `VirtualMachineError`, `RunnerError`, `MemoryError`, `MathError`, `InsufficientAllocatedCellsError` in `Box` [#1193](https://github.com/lambdaclass/cairo-rs/pull/1193)
* BREAKING: all tuple variants of `HintError` with a single `Felt252` or multiple elements now receive a single `Box`

Expand Down
6 changes: 1 addition & 5 deletions src/hint_processor/builtin_hint_processor/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::{
types::errors::math_errors::MathError,
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
};
use core::cmp::Ordering;
use felt::Felt252;
use num_traits::{One, ToPrimitive, Zero};

Expand Down Expand Up @@ -41,10 +40,7 @@ pub fn set_add(
let range_limit = (set_end_ptr - set_ptr)?;

for i in 0..range_limit {
if matches!(
vm.memcmp(elm_ptr, (set_ptr + elm_size * i)?, elm_size),
(Ordering::Equal, _)
) {
if vm.mem_eq(elm_ptr, (set_ptr + elm_size * i)?, elm_size) {
insert_value_from_var_name("index", Felt252::new(i), vm, ids_data, ap_tracking)?;
return insert_value_from_var_name(
"is_elm_in_set",
Expand Down
4 changes: 4 additions & 0 deletions src/vm/vm_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,10 @@ impl VirtualMachine {
self.segments.memory.memcmp(lhs, rhs, len)
}

pub fn mem_eq(&self, lhs: Relocatable, rhs: Relocatable, len: usize) -> bool {
self.segments.memory.mem_eq(lhs, rhs, len)
}

///Gets `n_ret` return values from memory
pub fn get_return_values(&self, n_ret: usize) -> Result<Vec<MaybeRelocatable>, MemoryError> {
let addr = (self.run_context.get_ap() - n_ret)
Expand Down
48 changes: 48 additions & 0 deletions src/vm/vm_memory/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,54 @@
(Ordering::Equal, len)
}

/// Compares two ranges of values in memory of length `len`
/// Returns the ordering and the first relative position at which they differ
/// Special cases:
/// - `lhs` exists in memory but `rhs` doesn't -> (Ordering::Greater, 0)
/// - `rhs` exists in memory but `lhs` doesn't -> (Ordering::Less, 0)
/// - None of `lhs` or `rhs` exist in memory -> (Ordering::Equal, 0)
/// Everything else behaves much like `memcmp` in C.
/// This is meant as an optimization for hints to avoid allocations.
pub(crate) fn mem_eq(&self, lhs: Relocatable, rhs: Relocatable, len: usize) -> bool {
if lhs == rhs {
return true;

Check warning on line 411 in src/vm/vm_memory/memory.rs

View check run for this annotation

Codecov / codecov/patch

src/vm/vm_memory/memory.rs#L411

Added line #L411 was not covered by tests
}
let get_segment = |idx: isize| {
if idx.is_negative() {
self.temp_data.get(-(idx + 1) as usize)

Check warning on line 415 in src/vm/vm_memory/memory.rs

View check run for this annotation

Codecov / codecov/patch

src/vm/vm_memory/memory.rs#L415

Added line #L415 was not covered by tests
} else {
self.data.get(idx as usize)
}
};
match (
get_segment(lhs.segment_index),
get_segment(rhs.segment_index),
) {
(None, None) => {
return true;

Check warning on line 425 in src/vm/vm_memory/memory.rs

View check run for this annotation

Codecov / codecov/patch

src/vm/vm_memory/memory.rs#L425

Added line #L425 was not covered by tests
}
(Some(_), None) => {
return false;

Check warning on line 428 in src/vm/vm_memory/memory.rs

View check run for this annotation

Codecov / codecov/patch

src/vm/vm_memory/memory.rs#L428

Added line #L428 was not covered by tests
}
(None, Some(_)) => {
return false;

Check warning on line 431 in src/vm/vm_memory/memory.rs

View check run for this annotation

Codecov / codecov/patch

src/vm/vm_memory/memory.rs#L431

Added line #L431 was not covered by tests
}
(Some(lhs_segment), Some(rhs_segment)) => {
let (lhs_start, rhs_start) = (lhs.offset, rhs.offset);
for i in 0..len {
let (lhs, rhs) = (
lhs_segment.get(lhs_start + i),
rhs_segment.get(rhs_start + i),
);
if lhs != rhs {
return false;
}
}
}
};
true
}

/// Gets a range of memory values from addr to addr + size
/// The outputed range may contain gaps if the original memory has them
pub fn get_range(&self, addr: Relocatable, size: usize) -> Vec<Option<Cow<MaybeRelocatable>>> {
Expand Down