Skip to content

Commit f137ca8

Browse files
authored
chore: remove some more usages of BytesMut (#9025)
1 parent a34e41c commit f137ca8

File tree

10 files changed

+38
-49
lines changed

10 files changed

+38
-49
lines changed

crates/net/downloaders/src/file_client.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use reth_network_p2p::{
1010
};
1111
use reth_network_peers::PeerId;
1212
use reth_primitives::{
13-
BlockBody, BlockHash, BlockHashOrNumber, BlockNumber, BytesMut, Header, HeadersDirection,
14-
SealedHeader, B256,
13+
BlockBody, BlockHash, BlockHashOrNumber, BlockNumber, Header, HeadersDirection, SealedHeader,
14+
B256,
1515
};
1616
use std::{collections::HashMap, io, path::Path};
1717
use thiserror::Error;
@@ -419,26 +419,22 @@ impl ChunkedFileReader {
419419
let new_read_bytes_target_len = chunk_target_len - old_bytes_len;
420420

421421
// read new bytes from file
422-
let mut reader = BytesMut::zeroed(new_read_bytes_target_len as usize);
422+
let prev_read_bytes_len = self.chunk.len();
423+
self.chunk.extend(std::iter::repeat(0).take(new_read_bytes_target_len as usize));
424+
let reader = &mut self.chunk[prev_read_bytes_len..];
423425

424426
// actual bytes that have been read
425-
let new_read_bytes_len = self.file.read_exact(&mut reader).await? as u64;
427+
let new_read_bytes_len = self.file.read_exact(reader).await? as u64;
428+
let next_chunk_byte_len = self.chunk.len();
426429

427430
// update remaining file length
428431
self.file_byte_len -= new_read_bytes_len;
429432

430-
let prev_read_bytes_len = self.chunk.len();
431-
432-
// read new bytes from file into chunk
433-
self.chunk.extend_from_slice(&reader[..]);
434-
let next_chunk_byte_len = self.chunk.len();
435-
436433
debug!(target: "downloaders::file",
437434
max_chunk_byte_len=self.chunk_byte_len,
438435
prev_read_bytes_len,
439436
new_read_bytes_target_len,
440437
new_read_bytes_len,
441-
reader_capacity=reader.capacity(),
442438
next_chunk_byte_len,
443439
remaining_file_byte_len=self.file_byte_len,
444440
"new bytes were read from file"

crates/net/ecies/src/codec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl Decoder for ECIESCodec {
4343
type Item = IngressECIESValue;
4444
type Error = ECIESError;
4545

46-
#[instrument(level = "trace", skip_all, fields(peer=&*format!("{:?}", self.ecies.remote_id.map(|s| s.to_string())), state=&*format!("{:?}", self.state)))]
46+
#[instrument(level = "trace", skip_all, fields(peer=?self.ecies.remote_id, state=?self.state))]
4747
fn decode(&mut self, buf: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
4848
loop {
4949
match self.state {
@@ -115,7 +115,7 @@ impl Decoder for ECIESCodec {
115115
impl Encoder<EgressECIESValue> for ECIESCodec {
116116
type Error = io::Error;
117117

118-
#[instrument(level = "trace", skip(self, buf), fields(peer=&*format!("{:?}", self.ecies.remote_id.map(|s| s.to_string())), state=&*format!("{:?}", self.state)))]
118+
#[instrument(level = "trace", skip(self, buf), fields(peer=?self.ecies.remote_id, state=?self.state))]
119119
fn encode(&mut self, item: EgressECIESValue, buf: &mut BytesMut) -> Result<(), Self::Error> {
120120
match item {
121121
EgressECIESValue::Auth => {

crates/net/eth-wire/src/multiplex.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,14 @@ impl ProtocolProxy {
312312
return Err(io::ErrorKind::InvalidInput.into())
313313
}
314314

315-
let mut masked_bytes = BytesMut::zeroed(msg.len());
316-
masked_bytes[0] = msg[0]
317-
.checked_add(self.shared_cap.relative_message_id_offset())
318-
.ok_or(io::ErrorKind::InvalidInput)?;
315+
let offset = self.shared_cap.relative_message_id_offset();
316+
if offset == 0 {
317+
return Ok(msg);
318+
}
319319

320-
masked_bytes[1..].copy_from_slice(&msg[1..]);
321-
Ok(masked_bytes.freeze())
320+
let mut masked = Vec::from(msg);
321+
masked[0] = masked[0].checked_add(offset).ok_or(io::ErrorKind::InvalidInput)?;
322+
Ok(masked.into())
322323
}
323324

324325
/// Unmasks the message ID of a message received from the wire.

crates/primitives/src/transaction/eip1559.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use super::access_list::AccessList;
22
use crate::{keccak256, Bytes, ChainId, Signature, TxKind, TxType, B256, U256};
33
use alloy_rlp::{length_of_length, Decodable, Encodable, Header};
4-
use bytes::BytesMut;
54
use core::mem;
65
use reth_codecs::{main_codec, Compact};
76

@@ -216,7 +215,7 @@ impl TxEip1559 {
216215
/// Outputs the signature hash of the transaction by first encoding without a signature, then
217216
/// hashing.
218217
pub(crate) fn signature_hash(&self) -> B256 {
219-
let mut buf = BytesMut::with_capacity(self.payload_len_for_signature());
218+
let mut buf = Vec::with_capacity(self.payload_len_for_signature());
220219
self.encode_for_signing(&mut buf);
221220
keccak256(&buf)
222221
}

crates/primitives/src/transaction/eip2930.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use super::access_list::AccessList;
22
use crate::{keccak256, Bytes, ChainId, Signature, TxKind, TxType, B256, U256};
33
use alloy_rlp::{length_of_length, Decodable, Encodable, Header};
4-
use bytes::BytesMut;
54
use core::mem;
65
use reth_codecs::{main_codec, Compact};
76

@@ -179,7 +178,7 @@ impl TxEip2930 {
179178
/// Outputs the signature hash of the transaction by first encoding without a signature, then
180179
/// hashing.
181180
pub(crate) fn signature_hash(&self) -> B256 {
182-
let mut buf = BytesMut::with_capacity(self.payload_len_for_signature());
181+
let mut buf = Vec::with_capacity(self.payload_len_for_signature());
183182
self.encode_for_signing(&mut buf);
184183
keccak256(&buf)
185184
}

crates/primitives/src/transaction/legacy.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::{keccak256, Bytes, ChainId, Signature, TxKind, TxType, B256, U256};
22
use alloy_rlp::{length_of_length, Encodable, Header};
3-
use bytes::BytesMut;
43
use core::mem;
54
use reth_codecs::{main_codec, Compact};
65

@@ -163,7 +162,7 @@ impl TxLegacy {
163162
///
164163
/// See [`Self::encode_for_signing`] for more information on the encoding format.
165164
pub(crate) fn signature_hash(&self) -> B256 {
166-
let mut buf = BytesMut::with_capacity(self.payload_len_for_signature());
165+
let mut buf = Vec::with_capacity(self.payload_len_for_signature());
167166
self.encode_for_signing(&mut buf);
168167
keccak256(&buf)
169168
}

crates/primitives/src/transaction/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -860,10 +860,11 @@ impl Compact for TransactionSignedNoHash {
860860
let tx_bits = if zstd_bit {
861861
TRANSACTION_COMPRESSOR.with(|compressor| {
862862
let mut compressor = compressor.borrow_mut();
863-
let mut tmp = bytes::BytesMut::with_capacity(200);
863+
let mut tmp = Vec::with_capacity(256);
864864
let tx_bits = self.transaction.to_compact(&mut tmp);
865865

866866
buf.put_slice(&compressor.compress(&tmp).expect("Failed to compress"));
867+
867868
tx_bits as u8
868869
})
869870
} else {

crates/rpc/rpc-engine-api/tests/it/payload.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
use alloy_rlp::{Decodable, Error as RlpError};
44
use assert_matches::assert_matches;
55
use reth_primitives::{
6-
bytes::{Bytes, BytesMut},
7-
proofs, Block, SealedBlock, TransactionSigned, Withdrawals, B256, U256,
6+
proofs, Block, Bytes, SealedBlock, TransactionSigned, Withdrawals, B256, U256,
87
};
98
use reth_rpc_types::engine::{
109
ExecutionPayload, ExecutionPayloadBodyV1, ExecutionPayloadV1, PayloadError,
@@ -59,20 +58,19 @@ fn payload_validation() {
5958

6059
// Valid extra data
6160
let block_with_valid_extra_data = transform_block(block.clone(), |mut b| {
62-
b.header.extra_data = BytesMut::zeroed(32).freeze().into();
61+
b.header.extra_data = Bytes::from_static(&[0; 32]);
6362
b
6463
});
6564

6665
assert_matches!(try_into_sealed_block(block_with_valid_extra_data, None), Ok(_));
6766

6867
// Invalid extra data
69-
let block_with_invalid_extra_data: Bytes = BytesMut::zeroed(33).freeze();
68+
let block_with_invalid_extra_data = Bytes::from_static(&[0; 33]);
7069
let invalid_extra_data_block = transform_block(block.clone(), |mut b| {
71-
b.header.extra_data = block_with_invalid_extra_data.clone().into();
70+
b.header.extra_data = block_with_invalid_extra_data.clone();
7271
b
7372
});
7473
assert_matches!(
75-
7674
try_into_sealed_block(invalid_extra_data_block,None),
7775
Err(PayloadError::ExtraData(data)) if data == block_with_invalid_extra_data
7876
);
@@ -92,7 +90,7 @@ fn payload_validation() {
9290
let mut payload_with_invalid_txs: ExecutionPayloadV1 = block_to_payload_v1(block.clone());
9391

9492
payload_with_invalid_txs.transactions.iter_mut().for_each(|tx| {
95-
*tx = Bytes::new().into();
93+
*tx = Bytes::new();
9694
});
9795
let payload_with_invalid_txs = try_payload_v1_to_block(payload_with_invalid_txs);
9896
assert_matches!(payload_with_invalid_txs, Err(PayloadError::Decode(RlpError::InputTooShort)));

crates/storage/codecs/src/alloy/access_list.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ impl Compact for AccessListItem {
88
where
99
B: bytes::BufMut + AsMut<[u8]>,
1010
{
11-
let mut buffer = bytes::BytesMut::new();
11+
let mut buffer = Vec::new();
1212
self.address.to_compact(&mut buffer);
1313
self.storage_keys.specialized_to_compact(&mut buffer);
14-
let total_length = buffer.len();
15-
buf.put(buffer);
16-
total_length
14+
buf.put(&buffer[..]);
15+
buffer.len()
1716
}
1817

1918
fn from_compact(mut buf: &[u8], _: usize) -> (Self, &[u8]) {
@@ -31,11 +30,10 @@ impl Compact for AccessList {
3130
where
3231
B: bytes::BufMut + AsMut<[u8]>,
3332
{
34-
let mut buffer = bytes::BytesMut::new();
33+
let mut buffer = Vec::new();
3534
self.0.to_compact(&mut buffer);
36-
let total_length = buffer.len();
37-
buf.put(buffer);
38-
total_length
35+
buf.put(&buffer[..]);
36+
buffer.len()
3937
}
4038

4139
fn from_compact(mut buf: &[u8], _: usize) -> (Self, &[u8]) {

crates/storage/codecs/src/alloy/log.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ impl Compact for LogData {
1010
where
1111
B: BufMut + AsMut<[u8]>,
1212
{
13-
let mut buffer = bytes::BytesMut::new();
13+
let mut buffer = Vec::new();
1414
let (topics, data) = self.split();
1515
topics.specialized_to_compact(&mut buffer);
1616
data.to_compact(&mut buffer);
17-
let total_length = buffer.len();
18-
buf.put(buffer);
19-
total_length
17+
buf.put(&buffer[..]);
18+
buffer.len()
2019
}
2120

2221
fn from_compact(mut buf: &[u8], _: usize) -> (Self, &[u8]) {
@@ -33,12 +32,11 @@ impl Compact for Log {
3332
where
3433
B: BufMut + AsMut<[u8]>,
3534
{
36-
let mut buffer = bytes::BytesMut::new();
35+
let mut buffer = Vec::new();
3736
self.address.to_compact(&mut buffer);
3837
self.data.to_compact(&mut buffer);
39-
let total_length = buffer.len();
40-
buf.put(buffer);
41-
total_length
38+
buf.put(&buffer[..]);
39+
buffer.len()
4240
}
4341

4442
fn from_compact(mut buf: &[u8], _: usize) -> (Self, &[u8]) {

0 commit comments

Comments
 (0)