From afef3b72bcc35862b860d89803abf3ea0df9157d Mon Sep 17 00:00:00 2001 From: Mario Rugiero Date: Thu, 4 May 2023 18:50:12 -0300 Subject: [PATCH] =?UTF-8?q?perf:=20eager=20resize=20for=20=C2=B4load=5Fdat?= =?UTF-8?q?a=C2=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 2 ++ src/vm/vm_memory/memory_segments.rs | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6bdf3d7166..1c9c6126be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ #### Upcoming Changes +* perf: insert elements from the tail in `load_data` so reallocation happens only once [#1117](https://github.com/lambdaclass/cairo-rs/pull/1117) + * Use to_signed_felt as function for felt252 as BigInt within [-P/2, P/2] range and use to_bigint as function for representation as BigInt. [#1100](https://github.com/lambdaclass/cairo-rs/pull/1100) * Implement hint on field_arithmetic lib [#1090](https://github.com/lambdaclass/cairo-rs/pull/1090) diff --git a/src/vm/vm_memory/memory_segments.rs b/src/vm/vm_memory/memory_segments.rs index 5fdda00126..c6c91221ed 100644 --- a/src/vm/vm_memory/memory_segments.rs +++ b/src/vm/vm_memory/memory_segments.rs @@ -55,7 +55,9 @@ impl MemorySegmentManager { ptr: Relocatable, data: &Vec, ) -> Result { - for (num, value) in data.iter().enumerate() { + // Starting from the end ensures any necessary resize + // is performed once with enough room for everything + for (num, value) in data.iter().enumerate().rev() { self.memory.insert((ptr + num)?, value)?; } (ptr + data.len()).map_err(MemoryError::Math)