Skip to content

Commit

Permalink
use Vec::with_capacity avoid realloc mem
Browse files Browse the repository at this point in the history
  • Loading branch information
nkysg committed Oct 31, 2024
1 parent 2f94e7f commit a5e3417
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion abci/src/application/kvstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<&str>>();
Expand Down
2 changes: 1 addition & 1 deletion light-client/tests/model_based.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
3 changes: 2 additions & 1 deletion testgen/src/light_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LightBlock> = vec![last_block.clone()];
let mut light_blocks: Vec<LightBlock> = Vec::with_capacity(num as usize);
light_blocks.push(last_block.clone());

for _i in 2..=num {
// add "next" light block to the vector
Expand Down
1 change: 1 addition & 0 deletions testgen/src/tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion tools/rpc-probe/src/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ async fn execute_parallel_interactions(
config: &PlanConfig,
interaction_sets: Vec<Vec<PlannedInteraction>>,
) -> 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();
Expand Down

0 comments on commit a5e3417

Please sign in to comment.