Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove all clones and copies from serializations #234

Merged
merged 3 commits into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions blockchain/blocks/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
75 changes: 45 additions & 30 deletions blockchain/blocks/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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)
}
Expand All @@ -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,
Expand Down
52 changes: 25 additions & 27 deletions blockchain/blocks/src/ticket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ impl ser::Serialize for Ticket {
where
S: Serializer,
{
let value = [self.vrfproof.clone()];
value.serialize(serializer)
[&self.vrfproof].serialize(serializer)
}
}

Expand Down Expand Up @@ -61,26 +60,14 @@ pub struct EPostProof {
pub candidates: Vec<EPostTicket>,
}

#[derive(Serialize, Deserialize)]
struct CborEPostTicket(
#[serde(with = "serde_bytes")] Vec<u8>, // partial
u64, // sector_id
u64, // challenge_index
);

#[derive(Serialize, Deserialize)]
struct CborEPostProof(
#[serde(with = "serde_bytes")] Vec<u8>, // proof
#[serde(with = "serde_bytes")] Vec<u8>, // post_rand
Vec<EPostTicket>, // candidates
);

impl ser::Serialize for EPostTicket {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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)
}
}
Expand All @@ -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<u8>, u64, u64);
let TupleEPostTicket(partial, sector_id, challenge_index) =
Deserialize::deserialize(deserializer)?;
Ok(Self {
partial,
Expand All @@ -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<u8>,
#[serde(with = "serde_bytes")] Vec<u8>,
Vec<EPostTicket>,
);

impl<'de> Deserialize<'de> for EPostProof {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
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,
Expand Down
3 changes: 1 addition & 2 deletions blockchain/blocks/src/tipset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ impl ser::Serialize for TipSetKeys {
where
S: Serializer,
{
let value = self.cids.clone();
value.serialize(serializer)
self.cids.serialize(serializer)
}
}

Expand Down
3 changes: 1 addition & 2 deletions vm/message/src/signed_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
30 changes: 21 additions & 9 deletions vm/message/src/unsigned_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
Expand Down