Skip to content

Commit

Permalink
Merge pull request #68 from data-pup/add-dominators-regex
Browse files Browse the repository at this point in the history
Added regex functionality to the dominators sub-command.
  • Loading branch information
data-pup authored May 23, 2018
2 parents 9f2340d + 2066625 commit 51b4112
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 39 deletions.
65 changes: 41 additions & 24 deletions analyze/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ pub fn top(items: &mut ir::Items, opts: &opt::Top) -> Result<Box<traits::Emit>,

struct DominatorTree {
tree: BTreeMap<ir::Id, Vec<ir::Id>>,
root_id: ir::Id,
items: Vec<ir::Id>,
opts: opt::Dominators,
}

Expand All @@ -277,11 +277,6 @@ impl traits::Emit for DominatorTree {

let opts = &self.opts;
let mut row = 0 as u32;
let start_depth = if self.root_id == items.meta_root() {
0
} else {
1
};

fn recursive_add_rows(
table: &mut Table,
Expand Down Expand Up @@ -343,15 +338,19 @@ impl traits::Emit for DominatorTree {
}
}

recursive_add_rows(
&mut table,
items,
&self.tree,
start_depth,
&mut row,
&opts,
self.root_id,
);
for id in &self.items {
let start_depth = if *id == items.meta_root() { 0 } else { 1 };
recursive_add_rows(
&mut table,
items,
&self.tree,
start_depth,
&mut row,
&opts,
*id,
);
}

write!(dest, "{}", &table)?;
Ok(())
}
Expand Down Expand Up @@ -396,7 +395,11 @@ impl traits::Emit for DominatorTree {
}

let mut obj = json::object(dest)?;
recursive_add_children(items, &self.opts, &self.tree, self.root_id, &mut obj)
for curr_id in &self.items {
recursive_add_children(items, &self.opts, &self.tree, *curr_id, &mut obj)?;
}

Ok(())
}

fn emit_csv(&self, items: &ir::Items, dest: &mut io::Write) -> Result<(), traits::Error> {
Expand Down Expand Up @@ -472,18 +475,32 @@ pub fn dominators(
items.compute_retained_sizes();
items.compute_predecessors();

let subtree = opts.subtree();
let root_id = match subtree.is_empty() {
true => items.meta_root(),
false => items
.get_item_by_name(&subtree)
.map(|item| item.id())
.unwrap_or(items.meta_root()),
let arguments = opts.items();
let dominator_items = match arguments.is_empty() {
true => vec![items.meta_root()],
false => {
if opts.using_regexps() {
let regexps = regex::RegexSet::new(arguments)?;
let mut sorted_items: Vec<_> = items
.iter()
.filter(|item| regexps.is_match(&item.name()))
.map(|item| item.id())
.collect();
sorted_items.sort_by_key(|id| items.retained_size(*id) as i32 * -1);
sorted_items
} else {
arguments
.iter()
.filter_map(|name| items.get_item_by_name(name))
.map(|item| item.id())
.collect()
}
}
};

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

Expand Down
38 changes: 23 additions & 15 deletions opt/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub struct Dominators {
output_format: traits::OutputFormat,

/// The name of the function whose dominator subtree should be printed.
subtree: Option<String>,
items: Vec<String>,

/// The maximum depth to print the dominators tree.
#[structopt(short = "d")]
Expand All @@ -143,6 +143,20 @@ pub struct Dominators {
/// The maximum number of rows, regardless of depth in the tree, to display.
#[structopt(short = "r")]
max_rows: Option<u32>,

/// Whether or not `items` should be treated as regular expressions.
#[structopt(long = "regex")]
using_regexps: bool,
}

impl Dominators {
// TODO: wasm-bindgen does not support sending Vec<String> across
// the wasm ABI boundary yet.

/// The items whose dominators subtree should be printed.
pub fn items(&self) -> &[String] {
&self.items
}
}

#[wasm_bindgen]
Expand All @@ -162,6 +176,11 @@ impl Dominators {
self.max_rows.unwrap_or(u32::MAX)
}

/// Whether or not `items` should be treated as regular expressions.
pub fn using_regexps(&self) -> bool {
self.using_regexps
}

/// Set the maximum depth to print the dominators tree.
pub fn set_max_depth(&mut self, max_depth: u32) {
self.max_depth = Some(max_depth);
Expand All @@ -172,20 +191,9 @@ impl Dominators {
self.max_rows = Some(max_rows);
}

// TODO: wasm-bindgen does not support sending Option<String> across
// the wasm ABI boundary, return an empty string if `subtree` is None.

/// The function whose subtree should be printed.
pub fn subtree(&self) -> String {
match &self.subtree {
Some(s) => s.clone(),
None => String::new(),
}
}

/// Set the function whose subtree should be printed.
pub fn set_subtree(&mut self, subtree: &str) {
self.subtree = Some(subtree.to_string());
/// Set whether or not `items` should be treated as regular expressions.
pub fn set_using_regexps(&mut self, using_regexps: bool) {
self.using_regexps = using_regexps;
}
}

Expand Down
14 changes: 14 additions & 0 deletions twiggy/tests/expectations/dominators_regex_any_func
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Retained Bytes β”‚ Retained % β”‚ Dominator Tree
────────────────┼────────────┼─────────────────────────
15 β”Š 10.42% β”Š func[3]
14 β”Š 9.72% β”Š β€· woof
6 β”Š 4.17% β”Š β€· func[0]
5 β”Š 3.47% β”Š β€· calledOnce
6 β”Š 4.17% β”Š func[0]
5 β”Š 3.47% β”Š β€· calledOnce
6 β”Š 4.17% β”Š func[1]
5 β”Š 3.47% β”Š β€· calledTwice
6 β”Š 4.17% β”Š func[2]
5 β”Š 3.47% β”Š β€· bark
6 β”Š 4.17% β”Š func[4]
5 β”Š 3.47% β”Š β€· awoo
8 changes: 8 additions & 0 deletions twiggy/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ test!(
"hello"
);

test!(
dominators_regex_any_func,
"dominators",
"./fixtures/paths_test.wasm",
"--regex",
"func\\[[0-9]+\\]"
);

test!(
paths_test_called_once,
"paths",
Expand Down

0 comments on commit 51b4112

Please sign in to comment.