-
Notifications
You must be signed in to change notification settings - Fork 197
Optimize Cairo 0 execution #2206
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
base: 2.x.y
Are you sure you want to change the base?
Changes from all commits
27d314d
a8daa0a
b4c8768
fa58734
dbea70a
4612ef6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -744,6 +744,58 @@ impl Memory { | |
self.mark_as_accessed(key); | ||
Ok(()) | ||
} | ||
|
||
/// Inserts multiple values into contiguous memory addresses. | ||
/// | ||
/// If the address isn't contiguous with previously inserted data, memory gaps will be represented by NONE values | ||
/// | ||
/// Returns an error if: | ||
/// - The segment index given by the address corresponds to a non-allocated segment. | ||
/// - An inserted value is inconsistent with the current value at the memory cell | ||
pub fn insert_all( | ||
&mut self, | ||
key: Relocatable, | ||
vals: &[MaybeRelocatable], | ||
) -> Result<(), MemoryError> { | ||
let segment = self.get_segment(key)?; | ||
let (_, value_offset) = from_relocatable_to_indexes(key); | ||
|
||
// Allocate space for all new elements. | ||
if segment.len() < value_offset + vals.len() { | ||
segment.reserve(value_offset + vals.len() - segment.len()); | ||
} | ||
Comment on lines
+764
to
+766
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I think we can remove the if since the documentation of
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe they refer to different things:
The if is required, as we only need to reserve new elements if the segment's length is not enough already. Without the condition, we would be sometimes be calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I thought that Maybe I have the wrong understanding about segments, but if the lenght of a segment is 5. Doesn't it mean it has 5 allocated elements? If that is the case, having an offset lower than the lenght means it would want to write on already used memory which is something that it cannot be done, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think that in
We may want to call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perfect, I forgot you could have those. Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently, If we make sure that |
||
// Fill middle with NONE. | ||
if segment.len() < value_offset { | ||
segment.resize(value_offset, MemoryCell::NONE); | ||
} | ||
// Insert new elements. | ||
let last_element_to_replace = segment.len().min(value_offset + vals.len()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn´t the last index always There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The behavior of It receives two arguments:
The length of the range and the length of the replacement does not need to coincide. For example, consider the following array:
If we want to insert
The result would look like this:
The following, instead, fails with index out of bounds, because we are replacing an element that does not exist.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ohh I see. Awesome, thanks! |
||
let replaced = segment.splice( | ||
value_offset..last_element_to_replace, | ||
vals.iter().map(|v| MemoryCell::new(v.clone())), | ||
); | ||
|
||
// Check for inconsistencies. | ||
let inconsistent_cell = replaced.enumerate().find(|(idx, replaced)| { | ||
replaced | ||
.get_value() | ||
.is_some_and(|replaced| replaced != vals[*idx]) | ||
}); | ||
if let Some((insertions_idx, current_value)) = inconsistent_cell { | ||
return Err(MemoryError::InconsistentMemory(Box::new(( | ||
(key + insertions_idx)?, | ||
current_value.into(), | ||
vals[insertions_idx].clone(), | ||
)))); | ||
} | ||
|
||
// Validate inserted memory cells | ||
for i in 0..vals.len() { | ||
self.validate_memory_cell((key + i)?)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
impl From<&Memory> for CairoPieMemory { | ||
|
Uh oh!
There was an error while loading. Please reload this page.