From 1eab6173f56c64f44b518f13fe84dd51a2d5cb0d Mon Sep 17 00:00:00 2001 From: austinabell Date: Wed, 19 Feb 2020 15:02:08 -0500 Subject: [PATCH] remove all clones and copies from serializations --- blockchain/blocks/src/block.rs | 6 +-- blockchain/blocks/src/header.rs | 75 ++++++++++++++++++------------ blockchain/blocks/src/ticket.rs | 52 ++++++++++----------- blockchain/blocks/src/tipset.rs | 3 +- vm/message/src/signed_message.rs | 3 +- vm/message/src/unsigned_message.rs | 30 ++++++++---- 6 files changed, 94 insertions(+), 75 deletions(-) diff --git a/blockchain/blocks/src/block.rs b/blockchain/blocks/src/block.rs index a7a28485d4c2..742275c7ad74 100644 --- a/blockchain/blocks/src/block.rs +++ b/blockchain/blocks/src/block.rs @@ -45,11 +45,7 @@ impl Serialize for TxMeta { where S: Serializer, { - let value = ( - self.bls_message_root.clone(), - self.secp_message_root.clone(), - ); - Serialize::serialize(&value, serializer) + (&self.bls_message_root, &self.secp_message_root).serialize(serializer) } } diff --git a/blockchain/blocks/src/header.rs b/blockchain/blocks/src/header.rs index df7a075f1988..7a4dd91ffff1 100644 --- a/blockchain/blocks/src/header.rs +++ b/blockchain/blocks/src/header.rs @@ -115,42 +115,41 @@ impl Cbor for BlockHeader { } } -#[derive(Serialize, Deserialize)] -struct TupleBlockHeader( - Address, // miner_address - Ticket, // ticket - EPostProof, // epost_verify - TipSetKeys, // parents []cid - #[serde(with = "biguint_ser")] BigUint, // weight - ChainEpoch, // epoch - Cid, // state_root - Cid, // message_receipts - Cid, // messages - Signature, // bls_aggregate - u64, // timestamp - Signature, // signature - u64, // fork_signal -); - impl ser::Serialize for BlockHeader { fn serialize(&self, serializer: S) -> Result where S: Serializer, { + #[derive(Serialize)] + struct TupleBlockHeader<'a>( + &'a Address, // miner_address + &'a Ticket, // ticket + &'a EPostProof, // epost_verify + &'a TipSetKeys, // parents []cid + #[serde(with = "biguint_ser")] &'a BigUint, // weight + &'a ChainEpoch, // epoch + &'a Cid, // state_root + &'a Cid, // message_receipts + &'a Cid, // messages + &'a Signature, // bls_aggregate + &'a u64, // timestamp + &'a Signature, // signature + &'a u64, // fork_signal + ); TupleBlockHeader( - self.miner_address.clone(), - self.ticket.clone(), - self.epost_verify.clone(), - self.parents.clone(), - self.weight.clone(), - self.epoch, - self.state_root.clone(), - self.message_receipts.clone(), - self.messages.clone(), - self.bls_aggregate.clone(), - self.timestamp, - self.signature.clone(), - self.fork_signal, + &self.miner_address, + &self.ticket, + &self.epost_verify, + &self.parents, + &self.weight, + &self.epoch, + &self.state_root, + &self.message_receipts, + &self.messages, + &self.bls_aggregate, + &self.timestamp, + &self.signature, + &self.fork_signal, ) .serialize(serializer) } @@ -161,6 +160,22 @@ impl<'de> de::Deserialize<'de> for BlockHeader { where D: Deserializer<'de>, { + #[derive(Deserialize)] + struct TupleBlockHeader( + Address, + Ticket, + EPostProof, + TipSetKeys, + #[serde(with = "biguint_ser")] BigUint, + ChainEpoch, + Cid, + Cid, + Cid, + Signature, + u64, + Signature, + u64, + ); let TupleBlockHeader( miner_address, ticket, diff --git a/blockchain/blocks/src/ticket.rs b/blockchain/blocks/src/ticket.rs index 4e5b1406942c..4e42725120b2 100644 --- a/blockchain/blocks/src/ticket.rs +++ b/blockchain/blocks/src/ticket.rs @@ -30,8 +30,7 @@ impl ser::Serialize for Ticket { where S: Serializer, { - let value = [self.vrfproof.clone()]; - value.serialize(serializer) + [&self.vrfproof].serialize(serializer) } } @@ -61,26 +60,14 @@ pub struct EPostProof { pub candidates: Vec, } -#[derive(Serialize, Deserialize)] -struct CborEPostTicket( - #[serde(with = "serde_bytes")] Vec, // partial - u64, // sector_id - u64, // challenge_index -); - -#[derive(Serialize, Deserialize)] -struct CborEPostProof( - #[serde(with = "serde_bytes")] Vec, // proof - #[serde(with = "serde_bytes")] Vec, // post_rand - Vec, // candidates -); - impl ser::Serialize for EPostTicket { fn serialize(&self, serializer: S) -> Result where S: Serializer, { - CborEPostTicket(self.partial.clone(), self.sector_id, self.challenge_index) + #[derive(Serialize)] + struct TupleEPostTicket<'a>(#[serde(with = "serde_bytes")] &'a [u8], &'a u64, &'a u64); + TupleEPostTicket(&self.partial, &self.sector_id, &self.challenge_index) .serialize(serializer) } } @@ -90,7 +77,9 @@ impl<'de> de::Deserialize<'de> for EPostTicket { where D: Deserializer<'de>, { - let CborEPostTicket(partial, sector_id, challenge_index) = + #[derive(Deserialize)] + struct TupleEPostTicket(#[serde(with = "serde_bytes")] Vec, u64, u64); + let TupleEPostTicket(partial, sector_id, challenge_index) = Deserialize::deserialize(deserializer)?; Ok(Self { partial, @@ -105,22 +94,31 @@ impl ser::Serialize for EPostProof { where S: Serializer, { - CborEPostProof( - self.proof.clone(), - self.post_rand.clone(), - self.candidates.clone(), - ) - .serialize(serializer) + #[derive(Serialize)] + struct TupleEPostProof<'a>( + #[serde(with = "serde_bytes")] &'a [u8], + #[serde(with = "serde_bytes")] &'a [u8], + &'a [EPostTicket], + ); + TupleEPostProof(&self.proof, &self.post_rand, &self.candidates).serialize(serializer) } } -impl<'de> de::Deserialize<'de> for EPostProof { +// Type defined outside of deserialize block because of bug with clippy +// with more than one annotated field +#[derive(Deserialize)] +struct TupleEPostProof( + #[serde(with = "serde_bytes")] Vec, + #[serde(with = "serde_bytes")] Vec, + Vec, +); + +impl<'de> Deserialize<'de> for EPostProof { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { - let CborEPostProof(proof, post_rand, candidates) = - CborEPostProof::deserialize(deserializer)?; + let TupleEPostProof(proof, post_rand, candidates) = Deserialize::deserialize(deserializer)?; Ok(Self { proof, post_rand, diff --git a/blockchain/blocks/src/tipset.rs b/blockchain/blocks/src/tipset.rs index d2a74d134f46..709ed2b2e8eb 100644 --- a/blockchain/blocks/src/tipset.rs +++ b/blockchain/blocks/src/tipset.rs @@ -49,8 +49,7 @@ impl ser::Serialize for TipSetKeys { where S: Serializer, { - let value = self.cids.clone(); - value.serialize(serializer) + self.cids.serialize(serializer) } } diff --git a/vm/message/src/signed_message.rs b/vm/message/src/signed_message.rs index 1c91d320e1cd..4498e57f51cb 100644 --- a/vm/message/src/signed_message.rs +++ b/vm/message/src/signed_message.rs @@ -22,8 +22,7 @@ impl Serialize for SignedMessage { where S: Serializer, { - let value = (self.message.clone(), self.signature.clone()); - Serialize::serialize(&value, serializer) + (&self.message, &self.signature).serialize(serializer) } } diff --git a/vm/message/src/unsigned_message.rs b/vm/message/src/unsigned_message.rs index d558130df21f..536dee3c4989 100644 --- a/vm/message/src/unsigned_message.rs +++ b/vm/message/src/unsigned_message.rs @@ -64,7 +64,8 @@ impl UnsignedMessage { } } -#[derive(Serialize, Deserialize)] +// Type declared outside of deserialize block because of clippy bug +#[derive(Deserialize)] pub struct TupleUnsignedMessage( Address, Address, @@ -81,15 +82,26 @@ impl ser::Serialize for UnsignedMessage { where S: ser::Serializer, { + #[derive(Serialize)] + pub struct TupleUnsignedMessage<'a>( + &'a Address, + &'a Address, + &'a u64, + &'a TokenAmount, + #[serde(with = "biguint_ser")] &'a BigUint, + #[serde(with = "biguint_ser")] &'a BigUint, + &'a MethodNum, + &'a Serialized, + ); TupleUnsignedMessage( - self.to.clone(), - self.from.clone(), - self.sequence, - self.value.clone(), - self.gas_price.clone(), - self.gas_limit.clone(), - self.method_num, - self.params.clone(), + &self.to, + &self.from, + &self.sequence, + &self.value, + &self.gas_price, + &self.gas_limit, + &self.method_num, + &self.params, ) .serialize(s) }