From 1e9342a949c284fb1eaed396b68ba1cc1f9c622d Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Tue, 17 May 2022 10:26:06 -0400 Subject: [PATCH] improve `hashed_storage_read` --- src/misc.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/misc.rs b/src/misc.rs index 3a81881..f9bc965 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -335,15 +335,12 @@ pub fn hashed_storage_read(data: &str) -> Option<(HashBytes, Vec)> { } // can't use data.as_bytes() here for some reason, seems to break on wasm? - // Other memory trickery seems to not worm on wasm. so I was unable to implement a more effecient manner of doing this - let decoded_1 = data.chars().map(|c| c as u8).collect::>(); + let decoded_1: Vec = data.chars().map(|c| c as u8).collect::>(); - let (hash, cached_data) = { - let (a, b) = unsafe { decoded_1.split_at_unchecked(HASH_LENGTH) }; - unsafe { (&*(a.as_ptr() as *const HashBytes), b) } - }; + let hash: HashBytes = unsafe { *(decoded_1[..HASH_LENGTH].as_ptr() as *const HashBytes) }; + let cached_data = decoded_1[HASH_LENGTH..].to_vec(); debug_assert!(!cached_data.is_empty()); - Some((*hash, cached_data.to_vec())) + Some((hash, cached_data)) }