Skip to content

Commit fcbbbb3

Browse files
edg-lMegaRedHand
andauthored
perf(l1): change encode_to_vec().len() -> length(), avoiding encoding to calc rlp length (#5374)
**Motivation** Followup on #5350 since its already a big pr. Changed: - Replaced encode_to_vec().len() calls with length() - Added more tests --------- Co-authored-by: Tomás Grüner <47506558+MegaRedHand@users.noreply.github.com>
1 parent f2b2006 commit fcbbbb3

File tree

9 files changed

+16
-10
lines changed

9 files changed

+16
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Perf
44

5+
### 2025-11-28
6+
7+
- Change some calls from `encode_to_vec().len()` to `.length()` when wanting to get the rlp encoded length [#5374](https://github.com/lambdaclass/ethrex/pull/5374)
8+
59
### 2025-11-20
610

711
- Improve rlp encoding by avoiding extra loops and remove unneeded array vec, also adding a alloc-less length method the default trait impl [#5350](https://github.com/lambdaclass/ethrex/pull/5350)

crates/blockchain/blockchain.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1754,7 +1754,7 @@ pub fn validate_block(
17541754
.map_err(InvalidBlockError::from)?;
17551755

17561756
if chain_config.is_fork_activated(Osaka, block.header.timestamp) {
1757-
let block_rlp_size = block.encode_to_vec().len();
1757+
let block_rlp_size = block.length();
17581758
if block_rlp_size > MAX_RLP_BLOCK_SIZE as usize {
17591759
return Err(error::ChainError::InvalidBlock(
17601760
InvalidBlockError::MaximumRlpSizeExceeded(

crates/blockchain/payload.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ impl PayloadBuildContext {
243243
let vm_db = StoreVmDatabase::new(storage.clone(), parent_header);
244244
let vm = new_evm(blockchain_type, vm_db)?;
245245

246-
let payload_size = payload.encode_to_vec().len() as u64;
246+
let payload_size = payload.length() as u64;
247247
Ok(PayloadBuildContext {
248248
remaining_gas: payload.header.gas_limit,
249249
receipts: vec![],

crates/common/types/block.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,9 @@ mod test {
900900
requests_hash: Some(*EMPTY_KECCACK_HASH),
901901
..Default::default()
902902
};
903-
assert!(validate_block_header(&block, &parent_block, ELASTICITY_MULTIPLIER).is_ok())
903+
assert!(validate_block_header(&block, &parent_block, ELASTICITY_MULTIPLIER).is_ok());
904+
assert_eq!(parent_block.encode_to_vec().len(), parent_block.length());
905+
assert_eq!(block.encode_to_vec().len(), block.length());
904906
}
905907

906908
#[test]

crates/common/types/transaction.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3445,10 +3445,12 @@ mod tests {
34453445

34463446
let encoded = PrivilegedL2Transaction::encode_to_vec(&privileged_l2);
34473447
println!("encoded length: {}", encoded.len());
3448+
assert_eq!(encoded.len(), privileged_l2.length());
34483449

34493450
let deserialized_tx = PrivilegedL2Transaction::decode(&encoded)?;
34503451

34513452
assert_eq!(deserialized_tx, privileged_l2);
3453+
34523454
Ok(())
34533455
}
34543456

crates/l2/monitor/widget/blocks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl BlocksTable {
113113
block.header.coinbase,
114114
block.header.gas_used,
115115
block.header.blob_gas_used,
116-
block.encode_to_vec().len(),
116+
block.length(),
117117
)
118118
})
119119
.collect::<Vec<_>>();

crates/l2/sequencer/block_producer/payload_builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ pub async fn fill_transactions(
9999
let VMType::L2(fee_config) = context.vm.vm_type else {
100100
return Err(BlockProducerError::Custom("invalid VM type".to_string()));
101101
};
102-
let mut acc_encoded_size = context.payload.encode_to_vec().len();
102+
let mut acc_encoded_size = context.payload.length();
103103
let fee_config_len = fee_config.to_vec().len();
104104

105105
debug!("Fetching transactions from mempool");
@@ -152,7 +152,7 @@ pub async fn fill_transactions(
152152

153153
// Check if we have enough blob space to add this transaction
154154
let tx: Transaction = head_tx.clone().into();
155-
let tx_size = tx.encode_to_vec().len();
155+
let tx_size = tx.length();
156156
if acc_encoded_size + fee_config_len + tx_size > SAFE_BYTES_PER_BLOB {
157157
debug!("No more blob space to run transactions");
158158
break;

crates/networking/rpc/types/block.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ impl RpcBlock {
7272
hash: H256,
7373
full_transactions: bool,
7474
) -> Result<RpcBlock, RpcErr> {
75-
let size = Block::new(header.clone(), body.clone())
76-
.encode_to_vec()
77-
.len();
75+
let size = Block::new(header.clone(), body.clone()).length();
7876
let body_wrapper = if full_transactions {
7977
BlockBodyWrapper::Full(FullBlockBody::from_body(body, header.number, hash)?)
8078
} else {

crates/vm/levm/src/hooks/l2_hook.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ fn calculate_l1_fee_gas(
692692
return Ok(0);
693693
};
694694

695-
let tx_size = vm.tx.encode_to_vec().len();
695+
let tx_size = vm.tx.length();
696696

697697
let l1_fee = calculate_l1_fee(fee_config, tx_size)?;
698698
let mut l1_fee_gas = l1_fee

0 commit comments

Comments
 (0)