Skip to content

Commit

Permalink
dev: optimize FromBytes
Browse files Browse the repository at this point in the history
  • Loading branch information
enitrat committed Sep 5, 2024
1 parent e452de2 commit 18e8a77
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
scarb 2.7.1
starknet-foundry 0.28.0
starknet-foundry 0.30.0
6 changes: 5 additions & 1 deletion crates/utils/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ edition = "2023_10"
evm = { path = "../evm" }
alexandria_data_structures = { path = "../alexandria_data_structures" }

# For profiling
[cairo]
unstable-add-statements-functions-debug-info = true

[tool]
fmt.workspace = true

[dev-dependencies]
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.28.0" }
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry.git", tag = "v0.30.0" }

[scripts]
test = "snforge test --max-n-steps 4294967295"
Expand Down
40 changes: 12 additions & 28 deletions crates/utils/src/helpers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ pub impl FromBytesImpl<
+ByteSize<T>,
+BytesUsedTrait<T>,
+Into<u8, T>,
+Into<u32, T>,
+Into<u16, T>,
+TryInto<T, u8>,
+Copy<T>,
+Drop<T>,
Expand All @@ -585,47 +585,31 @@ pub impl FromBytesImpl<
fn from_be_bytes(self: Span<u8>) -> Option<T> {
let byte_size = ByteSize::<T>::byte_size();

let len = self.len();
if len == 0 {
return Option::Some(Zero::zero());
}
if len > byte_size {
if self.len() != byte_size {
return Option::None;
}
let offset: u32 = len - 1;
let mut result: T = Zero::zero();
let mut i: u32 = 0;
while i != len {
let byte: T = (*self.at(i)).into();
// Safe unwrap, since offset - i is inbound in case of u8 { offset - i = 0 }, and
// TryInto<u32, u32>, TryInto<u32, u64>, TryInto<u32, u128>, TryInto<u32, 256> are safe
result += byte.shl((8 * (offset - i)).into());

i += 1;
let mut result: T = Zero::zero();
for byte in self {
let tmp = result * 256_u16.into();
result = tmp + (*byte).into();
};
Option::Some(result)
}

fn from_le_bytes(self: Span<u8>) -> Option<T> {
let byte_size = ByteSize::<T>::byte_size();
let len = self.len();

if len == 0 {
return Option::Some(Zero::zero());
}
if len > byte_size {
if self.len() != byte_size {
return Option::None;
}

let mut result: T = Zero::zero();
let mut i: u32 = 0;
while i != len {
let byte: T = (*self.at(i)).into();
// safe unwrap, as i is inbound in case of u8 { max value can be 8 * 1 = 8 }, and
// TryInto<u32, u32>, TryInto<u32, u64>, TryInto<u32, u128>, TryInto<u32, 256> are safe
result += byte.shl((8 * i).into());

i += 1;
let mut i = self.len();
while i != 0 {
i -= 1;
let tmp = result * 256_u16.into();
result = tmp + (*self[i]).into();
};
Option::Some(result)
}
Expand Down

0 comments on commit 18e8a77

Please sign in to comment.