Skip to content

Commit

Permalink
Factor out the common count * size pattern.
Browse files Browse the repository at this point in the history
  • Loading branch information
nnethercote committed Dec 17, 2024
1 parent d727f86 commit 7e44cbf
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions compiler/rustc_passes/src/input_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ impl NodeStats {
fn new() -> NodeStats {
NodeStats { count: 0, size: 0 }
}

fn nodes_size(&self) -> usize {
self.count * self.size
}
}

struct Node {
Expand Down Expand Up @@ -121,11 +125,9 @@ impl<'k> StatCollector<'k> {
// We will soon sort, so the initial order does not matter.
#[allow(rustc::potential_query_instability)]
let mut nodes: Vec<_> = self.nodes.iter().collect();
nodes.sort_by_cached_key(|(label, node)| {
(node.stats.count * node.stats.size, label.to_owned())
});
nodes.sort_by_cached_key(|(label, node)| (node.stats.nodes_size(), label.to_owned()));

let total_size = nodes.iter().map(|(_, node)| node.stats.count * node.stats.size).sum();
let total_size = nodes.iter().map(|(_, node)| node.stats.nodes_size()).sum();
let total_count = nodes.iter().map(|(_, node)| node.stats.count).sum();

eprintln!("{prefix} {title}");
Expand All @@ -138,7 +140,7 @@ impl<'k> StatCollector<'k> {
let percent = |m, n| (m * 100) as f64 / n as f64;

for (label, node) in nodes {
let size = node.stats.count * node.stats.size;
let size = node.stats.nodes_size();
eprintln!(
"{} {:<18}{:>10} ({:4.1}%){:>14}{:>14}",
prefix,
Expand All @@ -153,12 +155,11 @@ impl<'k> StatCollector<'k> {
#[allow(rustc::potential_query_instability)]
let mut subnodes: Vec<_> = node.subnodes.iter().collect();
subnodes.sort_by(|(label1, subnode1), (label2, subnode2)| {
(subnode1.count * subnode1.size, label1)
.cmp(&(subnode2.count * subnode2.size, label2))
(subnode1.nodes_size(), label1).cmp(&(subnode2.nodes_size(), label2))
});

for (label, subnode) in subnodes {
let size = subnode.count * subnode.size;
let size = subnode.nodes_size();
eprintln!(
"{} - {:<18}{:>10} ({:4.1}%){:>14}",
prefix,
Expand Down

0 comments on commit 7e44cbf

Please sign in to comment.