From a5e3417d7e6914c731afb796f8bcc46f2335bdbb Mon Sep 17 00:00:00 2001 From: nk_ysg Date: Thu, 31 Oct 2024 12:23:41 +0800 Subject: [PATCH] use Vec::with_capacity avoid realloc mem --- abci/src/application/kvstore.rs | 2 +- light-client/tests/model_based.rs | 2 +- testgen/src/light_chain.rs | 3 ++- testgen/src/tester.rs | 1 + tools/rpc-probe/src/plan.rs | 2 +- 5 files changed, 6 insertions(+), 4 deletions(-) diff --git a/abci/src/application/kvstore.rs b/abci/src/application/kvstore.rs index 3eb53bf61..cf9b23f4e 100644 --- a/abci/src/application/kvstore.rs +++ b/abci/src/application/kvstore.rs @@ -191,7 +191,7 @@ impl Application for KeyValueStoreApp { } fn finalize_block(&self, request: RequestFinalizeBlock) -> ResponseFinalizeBlock { - let mut events = Vec::new(); + let mut events = Vec::with_capacity(request.txs.len()); for tx in request.txs { let tx = std::str::from_utf8(&tx).unwrap(); let tx_parts = tx.split('=').collect::>(); diff --git a/light-client/tests/model_based.rs b/light-client/tests/model_based.rs index 9c5e689f0..3354f9a41 100644 --- a/light-client/tests/model_based.rs +++ b/light-client/tests/model_based.rs @@ -710,7 +710,7 @@ mod mbt { } fn model_based_test_batch(batch: ApalacheTestBatch) -> Vec<(String, String)> { - let mut res = Vec::new(); + let mut res = Vec::with_capacity(batch.tests.len()); for test in batch.tests { let tc = ApalacheTestCase { model: batch.model.clone(), diff --git a/testgen/src/light_chain.rs b/testgen/src/light_chain.rs index fe3a2ea42..c712efd89 100644 --- a/testgen/src/light_chain.rs +++ b/testgen/src/light_chain.rs @@ -20,7 +20,8 @@ impl LightChain { // TODO: like how does someone generate a chain with different validators at each height pub fn default_with_length(num: u64) -> Self { let mut last_block = LightBlock::new_default(1); - let mut light_blocks: Vec = vec![last_block.clone()]; + let mut light_blocks: Vec = Vec::with_capacity(num as usize); + light_blocks.push(last_block.clone()); for _i in 2..=num { // add "next" light block to the vector diff --git a/testgen/src/tester.rs b/testgen/src/tester.rs index 6639f8efd..55623b496 100644 --- a/testgen/src/tester.rs +++ b/testgen/src/tester.rs @@ -411,6 +411,7 @@ impl Tester { match batch(path, input) { None => continue, Some(tests) => { + res_tests.reserve_exact(tests.len()); for (name, input) in tests { let test_path = path.to_string() + "/" + &name; res_tests.push((test_path, input)); diff --git a/tools/rpc-probe/src/plan.rs b/tools/rpc-probe/src/plan.rs index 6212cc680..c8bc38d8f 100644 --- a/tools/rpc-probe/src/plan.rs +++ b/tools/rpc-probe/src/plan.rs @@ -298,7 +298,7 @@ async fn execute_parallel_interactions( config: &PlanConfig, interaction_sets: Vec>, ) -> Result<()> { - let mut handles = Vec::new(); + let mut handles = Vec::with_capacity(interaction_sets.len()); for interactions in interaction_sets { let mut inner_client = client.clone(); let inner_config = config.clone();