Skip to content

Commit

Permalink
perf: replace BytesMut with alloy_rlp::encode
Browse files Browse the repository at this point in the history
  • Loading branch information
estensen committed Mar 8, 2024
1 parent 91c7dd0 commit 2eaec96
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 64 deletions.
13 changes: 3 additions & 10 deletions crates/net/discv4/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
use alloy_rlp::Encodable;
use reth_net_common::ban_list::BanList;
use reth_net_nat::{NatResolver, ResolveNatInterval};
use reth_primitives::{
bytes::{Bytes, BytesMut},
NodeRecord,
};
use reth_primitives::{bytes::Bytes, NodeRecord};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::{
Expand Down Expand Up @@ -80,9 +77,7 @@ impl Discv4Config {

/// Add another key value pair to include in the ENR
pub fn add_eip868_pair(&mut self, key: impl Into<Vec<u8>>, value: impl Encodable) -> &mut Self {
let mut buf = BytesMut::new();
value.encode(&mut buf);
self.add_eip868_rlp_pair(key, buf.freeze())
self.add_eip868_rlp_pair(key, Bytes::from(alloy_rlp::encode(&value)))
}

/// Add another key value pair to include in the ENR
Expand Down Expand Up @@ -237,9 +232,7 @@ impl Discv4ConfigBuilder {

/// Add another key value pair to include in the ENR
pub fn add_eip868_pair(&mut self, key: impl Into<Vec<u8>>, value: impl Encodable) -> &mut Self {
let mut buf = BytesMut::new();
value.encode(&mut buf);
self.add_eip868_rlp_pair(key, buf.freeze())
self.add_eip868_rlp_pair(key, Bytes::from(alloy_rlp::encode(&value)))
}

/// Add another key value pair to include in the ENR
Expand Down
9 changes: 2 additions & 7 deletions crates/net/discv4/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ use discv5::{
use enr::Enr;
use parking_lot::Mutex;
use proto::{EnrRequest, EnrResponse, EnrWrapper};
use reth_primitives::{
bytes::{Bytes, BytesMut},
hex, ForkId, PeerId, B256,
};
use reth_primitives::{bytes::Bytes, hex, ForkId, PeerId, B256};
use secp256k1::SecretKey;
use std::{
cell::RefCell,
Expand Down Expand Up @@ -377,9 +374,7 @@ impl Discv4 {
///
/// If the key already exists, this will update it.
pub fn set_eip868_rlp(&self, key: Vec<u8>, value: impl alloy_rlp::Encodable) {
let mut buf = BytesMut::new();
value.encode(&mut buf);
self.set_eip868_rlp_pair(key, buf.freeze())
self.set_eip868_rlp_pair(key, Bytes::from(alloy_rlp::encode(&value)))
}

#[inline]
Expand Down
30 changes: 6 additions & 24 deletions crates/net/discv4/src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,10 +565,7 @@ mod tests {
udp_port: rng.gen(),
};

let mut buf = BytesMut::new();
msg.encode(&mut buf);

let decoded = NodeEndpoint::decode(&mut buf.as_ref()).unwrap();
let decoded = NodeEndpoint::decode(&mut alloy_rlp::encode(&msg).as_slice()).unwrap();
assert_eq!(msg, decoded);
}
}
Expand All @@ -585,10 +582,7 @@ mod tests {
udp_port: rng.gen(),
};

let mut buf = BytesMut::new();
msg.encode(&mut buf);

let decoded = NodeEndpoint::decode(&mut buf.as_ref()).unwrap();
let decoded = NodeEndpoint::decode(&mut alloy_rlp::encode(&msg).as_slice()).unwrap();
assert_eq!(msg, decoded);
}
}
Expand All @@ -606,10 +600,7 @@ mod tests {
enr_sq: None,
};

let mut buf = BytesMut::new();
msg.encode(&mut buf);

let decoded = Ping::decode(&mut buf.as_ref()).unwrap();
let decoded = Ping::decode(&mut alloy_rlp::encode(&msg).as_slice()).unwrap();
assert_eq!(msg, decoded);
}
}
Expand All @@ -627,10 +618,7 @@ mod tests {
enr_sq: Some(rng.gen()),
};

let mut buf = BytesMut::new();
msg.encode(&mut buf);

let decoded = Ping::decode(&mut buf.as_ref()).unwrap();
let decoded = Ping::decode(&mut alloy_rlp::encode(&msg).as_slice()).unwrap();
assert_eq!(msg, decoded);
}
}
Expand All @@ -648,10 +636,7 @@ mod tests {
enr_sq: None,
};

let mut buf = BytesMut::new();
msg.encode(&mut buf);

let decoded = Pong::decode(&mut buf.as_ref()).unwrap();
let decoded = Pong::decode(&mut alloy_rlp::encode(&msg).as_slice()).unwrap();
assert_eq!(msg, decoded);
}
}
Expand All @@ -669,10 +654,7 @@ mod tests {
enr_sq: Some(rng.gen()),
};

let mut buf = BytesMut::new();
msg.encode(&mut buf);

let decoded = Pong::decode(&mut buf.as_ref()).unwrap();
let decoded = Pong::decode(&mut alloy_rlp::encode(&msg).as_slice()).unwrap();
assert_eq!(msg, decoded);
}
}
Expand Down
7 changes: 4 additions & 3 deletions crates/net/eth-wire/src/p2pstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,14 @@ where
&mut self,
reason: DisconnectReason,
) -> Result<(), P2PStreamError> {
let mut buf = BytesMut::new();
P2PMessage::Disconnect(reason).encode(&mut buf);
trace!(
%reason,
"Sending disconnect message during the handshake",
);
self.inner.send(buf.freeze()).await.map_err(P2PStreamError::Io)
self.inner
.send(Bytes::from(alloy_rlp::encode(P2PMessage::Disconnect(reason))))
.await
.map_err(P2PStreamError::Io)
}
}

Expand Down
13 changes: 3 additions & 10 deletions crates/primitives/src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ mod tests {
};

use super::*;
use alloy_rlp::{Decodable, Encodable};
use bytes::BytesMut;
use alloy_rlp::{Decodable};
use rand::{thread_rng, Rng, RngCore};
use reth_rpc_types::PeerId;

Expand Down Expand Up @@ -126,10 +125,7 @@ mod tests {
id: rng.gen(),
};

let mut buf = BytesMut::new();
record.encode(&mut buf);

let decoded = NodeRecord::decode(&mut buf.as_ref()).unwrap();
let decoded = NodeRecord::decode(&mut alloy_rlp::encode(&record).as_slice()).unwrap();
assert_eq!(record, decoded);
}
}
Expand All @@ -147,10 +143,7 @@ mod tests {
id: rng.gen(),
};

let mut buf = BytesMut::new();
record.encode(&mut buf);

let decoded = NodeRecord::decode(&mut buf.as_ref()).unwrap();
let decoded = NodeRecord::decode(&mut alloy_rlp::encode(&record).as_slice()).unwrap();
assert_eq!(record, decoded);
}
}
Expand Down
13 changes: 3 additions & 10 deletions crates/rpc/rpc-types/src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@ impl FromStr for NodeRecord {
#[cfg(test)]
mod tests {
use super::*;
use alloy_rlp::{Decodable, Encodable};
use bytes::BytesMut;
use alloy_rlp::{Decodable};
use rand::{thread_rng, Rng, RngCore};

#[test]
Expand Down Expand Up @@ -219,10 +218,7 @@ mod tests {
id: rng.gen(),
};

let mut buf = BytesMut::new();
record.encode(&mut buf);

let decoded = NodeRecord::decode(&mut buf.as_ref()).unwrap();
let decoded = NodeRecord::decode(&mut alloy_rlp::encode(&record).as_slice()).unwrap();
assert_eq!(record, decoded);
}
}
Expand All @@ -240,10 +236,7 @@ mod tests {
id: rng.gen(),
};

let mut buf = BytesMut::new();
record.encode(&mut buf);

let decoded = NodeRecord::decode(&mut buf.as_ref()).unwrap();
let decoded = NodeRecord::decode(&mut alloy_rlp::encode(&record).as_slice()).unwrap();
assert_eq!(record, decoded);
}
}
Expand Down

0 comments on commit 2eaec96

Please sign in to comment.