From 43fc8b3e3ba7a6bb4941ba4f1b8b45891c60bb48 Mon Sep 17 00:00:00 2001 From: nkysggsy Date: Sun, 22 Dec 2024 14:10:20 +0800 Subject: [PATCH] use Vec::with_capacity avoid realloc --- fuel-merkle/src/binary/merkle_tree.rs | 2 +- fuel-tx/src/transaction/metadata.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/fuel-merkle/src/binary/merkle_tree.rs b/fuel-merkle/src/binary/merkle_tree.rs index 276a36367d..fcfd39b455 100644 --- a/fuel-merkle/src/binary/merkle_tree.rs +++ b/fuel-merkle/src/binary/merkle_tree.rs @@ -221,8 +221,8 @@ where storage: StorageType, leaves_count: u64, ) -> Result> { - let mut nodes = Vec::new(); let peaks = peak_positions(leaves_count).ok_or(MerkleTreeError::TooLarge)?; + let mut nodes = Vec::with_capacity(peaks.len()); for peak in peaks.iter() { let key = peak.in_order_index(); let node = storage diff --git a/fuel-tx/src/transaction/metadata.rs b/fuel-tx/src/transaction/metadata.rs index b66dde9b6b..92616f09fc 100644 --- a/fuel-tx/src/transaction/metadata.rs +++ b/fuel-tx/src/transaction/metadata.rs @@ -81,7 +81,7 @@ impl CommonMetadata { .collect_vec(); let mut offset = tx.inputs_offset(); - let mut inputs_offset_at = Vec::new(); + let mut inputs_offset_at = Vec::with_capacity(tx.inputs().len()); for (index, input) in tx.inputs().iter().enumerate() { let i = offset; offset = offset @@ -91,7 +91,7 @@ impl CommonMetadata { } let mut offset = tx.outputs_offset(); - let mut outputs_offset_at = Vec::new(); + let mut outputs_offset_at = Vec::with_capacity(tx.outputs.len()); for (index, output) in tx.outputs().iter().enumerate() { let i = offset; offset = offset @@ -101,7 +101,7 @@ impl CommonMetadata { } let mut offset = tx.witnesses_offset(); - let mut witnesses_offset_at = Vec::new(); + let mut witnesses_offset_at = Vec::with_capacity(tx.witness().len()); for (index, witnesses) in tx.witnesses().iter().enumerate() { let i = offset; offset = offset