Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: use Vec::with_capacity and reserve_exact #11904

Merged
merged 3 commits into from
Oct 21, 2024

Conversation

nkysg
Copy link
Contributor

@nkysg nkysg commented Oct 20, 2024

No description provided.

@nkysg nkysg changed the title use Vec::with_capacity and reserve_exact perf: use Vec::with_capacity and reserve_exact Oct 20, 2024
Copy link
Member

@emhane emhane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! in all of these cases, the vec is filled till capacity. if that were not the case, say if we would only push to the vec on some condition, you can just call my_vec.shrink_to_fit() after the loop

crates/net/discv4/src/lib.rs Outdated Show resolved Hide resolved
crates/rpc/rpc/src/eth/core.rs Outdated Show resolved Hide resolved
@nkysg nkysg force-pushed the use_with_capacity_and_reserve_exact branch from 2e19a75 to ea14dc6 Compare October 21, 2024 01:23
@nkysg nkysg force-pushed the use_with_capacity_and_reserve_exact branch from ea14dc6 to 1638722 Compare October 21, 2024 01:30

// iterator over the chunks
let chunks = tx_range
.clone()
.step_by(chunk_size)
.map(|start| start..std::cmp::min(start + chunk_size as u64, tx_range.end));
let mut channels = Vec::with_capacity(tx_range_size.div_ceil(chunk_size));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if there is a better method to calculate channels capacity

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mattsse mattsse added this pull request to the merge queue Oct 21, 2024
@mattsse mattsse added the C-perf A change motivated by improving speed, memory usage or disk footprint label Oct 21, 2024
Merged via the queue into paradigmxyz:main with commit f25cceb Oct 21, 2024
38 checks passed
@nkysg nkysg deleted the use_with_capacity_and_reserve_exact branch October 21, 2024 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-perf A change motivated by improving speed, memory usage or disk footprint
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants