Skip to content

Commit

Permalink
Refactor Memory::relocate_memory (#784)
Browse files Browse the repository at this point in the history
* Add first draft inefficient implementation

* Add new test cases

* Clean leftover segments + add tests

* Add temporal fix + TODO

* Add test programs

* Simplify code

* Simplify code

* Remove intermediate storange vec

* Reserve memory for relocated values

* Remove impossible-case code

* Add tests

* Clippy

* Remove temp_data trim

* Simplify code + add test case

* Remove comments + add cairo progran

* Use remove instead of swap for relocated temporary memory

* Rely on insertion errors + add tests

* Add newline at eof

* Add comment

* Change method name + add tests

* Update CHANGELOG.md

Co-authored-by: Mario Rugiero <mario.rugiero@lambdaclass.com>

* Update src/vm/vm_memory/memory.rs

Co-authored-by: Juan Rigada <62958725+Jrigada@users.noreply.github.com>

* Update src/vm/vm_memory/memory.rs

Co-authored-by: Juan Rigada <62958725+Jrigada@users.noreply.github.com>

---------

Co-authored-by: Mario Rugiero <mario.rugiero@lambdaclass.com>
Co-authored-by: Juan Rigada <62958725+Jrigada@users.noreply.github.com>
  • Loading branch information
3 people committed Feb 2, 2023
1 parent b91f4ac commit 4a642c7
Show file tree
Hide file tree
Showing 6 changed files with 440 additions and 71 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

#### Upcoming Changes

* Refactor `Memory::relocate memory` [#784](https://github.com/lambdaclass/cairo-rs/pull/784)
* Bugfixes:
* `Memory::relocate_memory` now moves data in the temporary memory relocated by a relocation rule to the real memory
* Aditional Notes:
* When relocating temporary memory produces clashes with pre-existing values in the real memory, an InconsistentMemory error is returned instead of keeping the last inserted value. This differs from the original implementation.

* Restrict addresses to Relocatable + fix some error variants used in signature.rs [#792](https://github.com/lambdaclass/cairo-rs/pull/792)
* Public Api Changes:
* Change `ValidationRule` inner type to `Box<dyn Fn(&Memory, &Relocatable) -> Result<Vec<Relocatable>, MemoryError>>`.
Expand Down
4 changes: 0 additions & 4 deletions cairo_programs/relocate_segments.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ func main() {
ids.temporary_array = segments.add_temp_segment()
%}

// Insert values into temporary_array
assert temporary_array[0] = 1;
assert temporary_array[1] = 2;

// Create array
let (array : felt*) = alloc();

Expand Down
4 changes: 0 additions & 4 deletions cairo_programs/relocate_segments_with_offset.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ func main() {
ids.temporary_array = segments.add_temp_segment()
%}

// Insert values into temporary_array_no_offset
assert temporary_array[0] = 1;
assert temporary_array[1] = 2;

// Create array
let (array : felt*) = alloc();

Expand Down
27 changes: 27 additions & 0 deletions cairo_programs/relocate_temporary_segment_append.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.segments import relocate_segment

func main() {
alloc_locals;
// Create temporary_array in a temporary segment
local temporary_array: felt*;
%{
ids.temporary_array = segments.add_temp_segment()
%}

// Insert values into temporary_array
assert temporary_array[0] = 4;
assert temporary_array[1] = 5;
assert temporary_array[2] = 6;

// Create array
let (array: felt*) = alloc();
assert array[0] = 1;
assert array[1] = 2;
assert array[2] = 3;

// Realocate temporary_array into the array segment
relocate_segment(src_ptr=temporary_array, dest_ptr=array + 3);

return ();
}
24 changes: 24 additions & 0 deletions cairo_programs/relocate_temporary_segment_into_new.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.segments import relocate_segment

func main() {
alloc_locals;
// Create temporary_array in a temporary segment
local temporary_array: felt*;
%{
ids.temporary_array = segments.add_temp_segment()
%}

// Insert values into temporary_array
assert temporary_array[0] = 1;
assert temporary_array[1] = 2;
assert temporary_array[2] = 3;

// Create array
let (array: felt*) = alloc();

// Realocate temporary_array into the array segment
relocate_segment(src_ptr=temporary_array, dest_ptr=array);

return ();
}
Loading

0 comments on commit 4a642c7

Please sign in to comment.