Skip to content

Commit d4e2198

Browse files
committed
se Vec<u8> for Encoder to avoid cloning
1 parent cbfd9a7 commit d4e2198

File tree

4 files changed

+18
-29
lines changed

4 files changed

+18
-29
lines changed

crates/services/p2p/src/codecs.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@ pub mod request_response;
55
use crate::gossipsub::messages::GossipTopicTag;
66
use libp2p::request_response as libp2p_request_response;
77

8-
use std::{
9-
borrow::Cow,
10-
io,
11-
};
8+
use std::io;
129

1310
pub trait Encoder: Send {
14-
/// Returns the serialized object as a slice.
15-
fn as_bytes(&self) -> Cow<[u8]>;
11+
/// Returns the serialized object as a Vector.
12+
fn as_vec(self) -> Vec<u8>;
1613
}
1714

1815
/// The trait encodes the type to the bytes and passes it to the `Encoder`,
@@ -23,12 +20,10 @@ pub trait Encoder: Send {
2320
pub trait Encode<T: ?Sized> {
2421
type Error;
2522
/// The encoder type that stores serialized object.
26-
type Encoder<'a>: Encoder
27-
where
28-
T: 'a;
23+
type Encoder: Encoder;
2924

3025
/// Encodes the object to the bytes and passes it to the `Encoder`.
31-
fn encode<'a>(&self, t: &'a T) -> Result<Self::Encoder<'a>, Self::Error>;
26+
fn encode(&self, t: &T) -> Result<Self::Encoder, Self::Error>;
3227
}
3328

3429
/// The trait decodes the type from the bytes.
@@ -38,12 +33,9 @@ pub trait Decode<T> {
3833
fn decode(&self, bytes: &[u8]) -> Result<T, Self::Error>;
3934
}
4035

41-
impl<'a> Encoder for Cow<'a, [u8]> {
42-
fn as_bytes(&self) -> Cow<'_, [u8]> {
43-
match self {
44-
Cow::Borrowed(borrowed) => Cow::Borrowed(borrowed),
45-
Cow::Owned(owned) => Cow::Borrowed(owned.as_ref()),
46-
}
36+
impl Encoder for Vec<u8> {
37+
fn as_vec(self) -> Vec<u8> {
38+
self
4739
}
4840
}
4941

crates/services/p2p/src/codecs/gossipsub.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ where
3232
fn encode(&self, data: Self::RequestMessage) -> Result<Vec<u8>, io::Error> {
3333
match data {
3434
GossipsubBroadcastRequest::NewTx(tx) => {
35-
Ok(self.codec.encode(&tx)?.as_bytes().into_owned())
35+
let bytes = self.codec.encode(&tx)?;
36+
Ok(bytes.as_vec())
3637
}
3738
}
3839
}

crates/services/p2p/src/codecs/postcard.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ use super::{
55
Encode,
66
};
77

8-
use std::{
9-
borrow::Cow,
10-
io,
11-
};
8+
use std::io;
129

1310
#[derive(Clone, Default)]
1411
pub struct PostcardCodec;
@@ -39,13 +36,12 @@ impl<T> Encode<T> for PostcardCodec
3936
where
4037
T: ?Sized + serde::Serialize,
4138
{
42-
type Encoder<'a> = Cow<'a, [u8]> where T: 'a;
39+
type Encoder = Vec<u8>;
4340
type Error = io::Error;
4441

45-
fn encode<'a>(&self, value: &'a T) -> Result<Self::Encoder<'a>, Self::Error> {
46-
Ok(Cow::Owned(postcard::to_allocvec(value).map_err(|e| {
47-
io::Error::new(io::ErrorKind::Other, e.to_string())
48-
})?))
42+
fn encode<'a>(&self, value: &'a T) -> Result<Self::Encoder, Self::Error> {
43+
Ok(postcard::to_allocvec(value)
44+
.map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))?)
4945
}
5046
}
5147

crates/services/p2p/src/codecs/request_response.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ where
106106
T: futures::AsyncWrite + Unpin + Send,
107107
{
108108
let encoded_data = self.codec.encode(&req)?;
109-
socket.write_all(&encoded_data.as_bytes()).await?;
109+
socket.write_all(&encoded_data.as_vec()).await?;
110110
Ok(())
111111
}
112112

@@ -123,12 +123,12 @@ where
123123
RequestResponseProtocol::V1 => {
124124
let v1_response: V1ResponseMessage = res.into();
125125
let res = self.codec.encode(&v1_response)?;
126-
let res = res.as_bytes();
126+
let res = res.as_vec();
127127
socket.write_all(&res).await?;
128128
}
129129
RequestResponseProtocol::V2 => {
130130
let res = self.codec.encode(&res)?;
131-
let res = res.as_bytes();
131+
let res = res.as_vec();
132132
socket.write_all(&res).await?;
133133
}
134134
};

0 commit comments

Comments
 (0)