Skip to content

adding extra dominators #19

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

Merged
merged 2 commits into from
Feb 18, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 50 additions & 26 deletions analyze/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ pub fn top(items: &mut ir::Items, opts: &opt::Top) -> Result<Box<traits::Emit>,

struct DominatorTree {
tree: BTreeMap<ir::Id, Vec<ir::Id>>,
dominators: opt::Dominators,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nitpick: let's rename this field to opts

}

impl traits::Emit for DominatorTree {
Expand All @@ -212,48 +213,70 @@ impl traits::Emit for DominatorTree {
(Align::Left, "Dominator Tree".to_string()),
]);

let dominators = &self.dominators;

fn recursive_add_rows(
table: &mut Table,
items: &ir::Items,
dominator_tree: &BTreeMap<ir::Id, Vec<ir::Id>>,
depth: usize,
dominators: &opt::Dominators,
id: ir::Id,
) {
assert_eq!(id == items.meta_root(), depth == 0);

if depth > 0 {
let item = &items[id];
if dominators.max_row != 0 && depth != dominators.max_depth {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Rather than indenting the whole function inside this if, let's:

  • add a new parameter, row: &mut usize
  • add an early return at the start of this function if *row == opts.max_rows || depth > opts.max_depth { return; }
  • whenever we actually add a row to the table, *row += 1;
  • finally, when we first call recursive_add_rows, pass &mut 0 for the rows parameter

These tweaks should make this functionality a little less invasive

if depth > 0 {
let item = &items[id];

let size = items.retained_size(id);
let size_percent = (size as f64) / (items.size() as f64) * 100.0;
let size = items.retained_size(id);
let size_percent = (size as f64) / (items.size() as f64) * 100.0;

let mut label = String::with_capacity(depth * 4 + item.name().len() + "⤷ ".len());
for _ in 2..depth {
label.push_str(" ");
}
if depth != 1 {
label.push_str(" ⤷ ");
}
label.push_str(item.name());
let mut label =
String::with_capacity(depth * 4 + item.name().len() + "⤷ ".len());
for _ in 2..depth {
label.push_str(" ");
}
if depth != 1 {
label.push_str(" ⤷ ");
}
label.push_str(item.name());

table.add_row(vec![
size.to_string(),
format!("{:.2}%", size_percent),
label,
]);
}
table.add_row(vec![
size.to_string(),
format!("{:.2}%", size_percent),
label,
]);
}

if let Some(children) = dominator_tree.get(&id) {
let mut children: Vec<_> = children.iter().cloned().collect();
children
.sort_unstable_by(|a, b| items.retained_size(*b).cmp(&items.retained_size(*a)));
for child in children {
recursive_add_rows(table, items, dominator_tree, depth + 1, child);
if let Some(children) = dominator_tree.get(&id) {
let mut children: Vec<_> = children.iter().cloned().collect();
children.sort_unstable_by(|a, b| {
items.retained_size(*b).cmp(&items.retained_size(*a))
});
for child in children {
// dominators.max_row = &dominators.max_row - 1;
recursive_add_rows(
table,
items,
dominator_tree,
depth + 1,
&dominators,
child,
);
}
}
}
}

recursive_add_rows(&mut table, items, &self.tree, 0, items.meta_root());
recursive_add_rows(
&mut table,
items,
&self.tree,
0,
&dominators,
items.meta_root(),
);
write!(&mut dest, "{}", &table)?;
Ok(())
}
Expand All @@ -262,13 +285,14 @@ impl traits::Emit for DominatorTree {
/// Compute the dominator tree for the given IR graph.
pub fn dominators(
items: &mut ir::Items,
_opts: &opt::Dominators,
opts: &opt::Dominators,
) -> Result<Box<traits::Emit>, failure::Error> {
items.compute_dominator_tree();
items.compute_retained_sizes();

let tree = DominatorTree {
tree: items.dominator_tree().clone(),
dominators: opts.clone(),
};

Ok(Box::new(tree) as Box<traits::Emit>)
Expand Down
4 changes: 4 additions & 0 deletions opt/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ pub struct Dominators {
/// The format the output should be written in.
#[structopt(short = "f", long = "format", default_value = "text")]
pub output_format: OutputFormat,

#[structopt(short = "d", default_value = "10")] pub max_depth: usize,

#[structopt(short = "r", default_value = "10")] pub max_row: usize,
Copy link
Collaborator

Choose a reason for hiding this comment

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

NItpick: max_rows instead of `max_row.

Let's also add doc comments to each new field, since this will turn into the CLI help text:

The maximum depth to print the dominators tree.

The maximum number of rows, regardless of depth in the tree, to display.

Let's also make these Option<usize> rather than providing a default value.

}

impl CommonOptions for Dominators {
Expand Down
14 changes: 14 additions & 0 deletions svelte/tests/expectations/dominators_wee_alloc_with_depth_and_row
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Retained Bytes │ Retained % │ Dominator Tree
────────────────┼────────────┼────────────────────────────
774 ┊ 27.48% ┊ "function names" subsection
564 ┊ 20.02% ┊ export "hello"
556 ┊ 19.74% ┊ ⤷ func[8]
551 ┊ 19.56% ┊ ⤷ hello
387 ┊ 13.74% ┊ ⤷ func[2]
4 ┊ 0.14% ┊ ⤷ type[5]
59 ┊ 2.09% ┊ export "goodbye"
49 ┊ 1.74% ┊ ⤷ func[9]
44 ┊ 1.56% ┊ ⤷ goodbye
4 ┊ 0.14% ┊ ⤷ type[3]
11 ┊ 0.39% ┊ export "memory"
2 ┊ 0.07% ┊ ⤷ memory[0]
10 changes: 10 additions & 0 deletions svelte/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ test!(
"./fixtures/wee_alloc.wasm"
);

test!(
dominators_wee_alloc_with_depth_and_row,
"dominators",
"./fixtures/wee_alloc.wasm",
"-d",
"5",
"-r",
"3"
);

test!(
paths_wee_alloc,
"paths",
Expand Down