-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Use Vec::with_capacity(1) in a couple of places. #50762
Conversation
r? @eddyb (rust_highfive has picked a reviewer for you, use r? to override) |
src/librustc/middle/dataflow.rs
Outdated
@@ -181,7 +181,7 @@ fn build_local_id_to_index(body: Option<&hir::Body>, | |||
|
|||
cfg.graph.each_node(|node_idx, node| { | |||
if let cfg::CFGNodeData::AST(id) = node.data { | |||
index.entry(id).or_insert(vec![]).push(node_idx); | |||
index.entry(id).or_insert(Vec::with_capacity(1)).push(node_idx); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably make the allocation lazy with .or_insert_with(|| Vec::with_capacity(1))
src/librustc/middle/dataflow.rs
Outdated
@@ -209,7 +209,8 @@ fn build_local_id_to_index(body: Option<&hir::Body>, | |||
} | |||
|
|||
fn visit_pat(&mut self, p: &hir::Pat) { | |||
self.index.entry(p.hir_id.local_id).or_insert(vec![]).push(self.entry); | |||
self.index.entry(p.hir_id.local_id) | |||
.or_insert(Vec::with_capacity(1)).push(self.entry); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@arthurprs' comment about making or_insert
lazy also applies here.
Is there a reason these aren't being changed to |
For the So the improvement for (BTW, it's interesting that these vectors get copied so much. I considered changing them to boxed slices to make them smaller, but that didn't work out because many of them need to be modifiable. I'd be interested to hear other ideas on this front.) For the |
Because Vec::new() + push() results in a capacity of 4, and these particular vectors almost never grow past a length of 1. This change reduces the number of bytes of heap allocation significantly. (For serde it's a 15% reduction for a debug build.) This didn't give noticeable speed-ups on my machine but it's a trivial change and certainly can't hurt.
I tried |
@bors r+ |
📌 Commit 4123f80 has been approved by |
Actually, I've worked out a better way of doing things. @eddyb, can you please unapprove this PR? |
@bors r- |
#50818 is the much-improved version. |
Because Vec::new() + push() results in a capacity of 4, and these
particular vectors almost never grow past a length of 1.
This change reduces the number of bytes of heap allocation
significantly. (For serde it's a 15% reduction for a debug build.) This
didn't give noticeable speed-ups on my machine but it's a trivial change
and certainly can't hurt.