From 2e40d9b6e5f13e58eb966b75de95ead56753049b Mon Sep 17 00:00:00 2001 From: xgreenx Date: Mon, 15 Nov 2021 21:52:06 +0200 Subject: [PATCH 1/2] Optimized decode of AccountId --- crates/env/src/types.rs | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/crates/env/src/types.rs b/crates/env/src/types.rs index fff2d3a8868..cb2b0350f46 100644 --- a/crates/env/src/types.rs +++ b/crates/env/src/types.rs @@ -211,22 +211,19 @@ pub type RentFraction = Perbill; /// This is a mirror of the `AccountId` type used in the default configuration /// of PALLET contracts. #[derive( - Debug, - Copy, - Clone, - PartialEq, - Eq, - Ord, - PartialOrd, - Hash, - Encode, - Decode, - From, - Default, + Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Encode, From, Default, )] #[cfg_attr(feature = "std", derive(TypeInfo))] pub struct AccountId([u8; 32]); +impl Decode for AccountId { + fn decode(input: &mut I) -> Result { + let mut account_id = AccountId { 0: [0; 32] }; + input.read(account_id.0.as_mut_slice())?; + Ok(account_id) + } +} + impl AsRef<[u8; 32]> for AccountId { #[inline] fn as_ref(&self) -> &[u8; 32] { From 7cf279bd95d318bd3538b7e5db9a5288b54237e7 Mon Sep 17 00:00:00 2001 From: xgreenx Date: Mon, 15 Nov 2021 22:24:44 +0200 Subject: [PATCH 2/2] Added custom `Decode` for `Key` and `Hash` --- crates/env/src/types.rs | 21 +++++++++------------ crates/primitives/src/key.rs | 5 +++-- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/crates/env/src/types.rs b/crates/env/src/types.rs index cb2b0350f46..a813d6016ca 100644 --- a/crates/env/src/types.rs +++ b/crates/env/src/types.rs @@ -268,22 +268,19 @@ impl<'a> TryFrom<&'a [u8]> for AccountId { /// This is a mirror of the `Hash` type used in the default configuration /// of PALLET contracts. #[derive( - Debug, - Copy, - Clone, - PartialEq, - Eq, - Ord, - PartialOrd, - Hash, - Encode, - Decode, - From, - Default, + Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Encode, From, Default, )] #[cfg_attr(feature = "std", derive(TypeInfo))] pub struct Hash([u8; 32]); +impl Decode for Hash { + fn decode(input: &mut I) -> Result { + let mut hash = Hash { 0: [0; 32] }; + input.read(hash.0.as_mut_slice())?; + Ok(hash) + } +} + impl<'a> TryFrom<&'a [u8]> for Hash { type Error = TryFromSliceError; diff --git a/crates/primitives/src/key.rs b/crates/primitives/src/key.rs index fafb20e4e3a..86ff7d66690 100644 --- a/crates/primitives/src/key.rs +++ b/crates/primitives/src/key.rs @@ -179,8 +179,9 @@ impl scale::Decode for Key { where I: scale::Input, { - let bytes = <[u8; 32] as scale::Decode>::decode(input)?; - Ok(Self::from(bytes)) + let mut key = Key { 0: [0; 32] }; + input.read(key.0.as_mut_slice())?; + Ok(key) } #[inline]