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

Added regular expressions and specific arguments to diff. #129

Merged
merged 1 commit into from
Aug 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
Added regular expressions and specific arguments to diff.
  • Loading branch information
data-pup committed Aug 20, 2018
commit 4839dc7477c07b962a26c4614bf18cb5d7a75aee
40 changes: 35 additions & 5 deletions analyze/analyses/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::collections::{HashMap, HashSet};
use std::io;

use csv;
use regex;
use serde::{self, ser::SerializeStruct};

use formats::json;
Expand Down Expand Up @@ -146,8 +147,21 @@ pub fn diff(
let names = old_sizes
.keys()
.chain(new_sizes.keys())
.map(|k| k.to_string())
.collect::<HashSet<_>>();
.map(|k| k.to_string());

// If arguments were given to the command, we should filter out items that
// do not match any of the given names or expressions.
let names: HashSet<String> = if !opts.items().is_empty() {
if opts.using_regexps() {
let regexps = regex::RegexSet::new(opts.items())?;
names.filter(|name| regexps.is_match(name)).collect()
} else {
let item_names = opts.items().iter().collect::<HashSet<_>>();
names.filter(|name| item_names.contains(&name)).collect()
}
} else {
names.collect()
};

// Iterate through the set of item names, and use the closure above to map
// each item into a `DiffEntry` object. Then, sort the collection.
Expand All @@ -171,16 +185,32 @@ pub fn diff(
};

// Create a `DiffEntry` representing the net change, and total row count.
// If specifying arguments were not given, calculate the total net changes,
// otherwise find the total values only for items in the the deltas collection.
let (total_cnt, total_delta) = if opts.items().is_empty() {
(
deltas.len(),
i64::from(new_items.size()) - i64::from(old_items.size()),
)
} else {
deltas
.iter()
.fold((0, 0), |(cnt, rem_delta), DiffEntry { delta, .. }| {
(cnt + 1, rem_delta + delta)
})
};
let total = DiffEntry {
name: format!("Σ [{} Total Rows]", deltas.len()),
delta: i64::from(new_items.size()) - i64::from(old_items.size()),
name: format!("Σ [{} Total Rows]", total_cnt),
delta: total_delta,
};

// Now that the 'remaining' and 'total' summary entries have been created,
// truncate the vector of deltas before we box up the result, and push
// the remaining and total rows to the deltas vector.
deltas.truncate(max_items);
deltas.push(remaining);
if rem_cnt > 0 {
deltas.push(remaining);
}
deltas.push(total);

// Return the results so that they can be emitted.
Expand Down
29 changes: 29 additions & 0 deletions opt/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,16 @@ pub struct Diff {
#[structopt(short = "f", long = "format", default_value = "text")]
output_format: traits::OutputFormat,

/// The name of the item(s) whose diff should be printed.
items: Vec<String>,

/// The maximum number of items to display.
#[structopt(short = "n", default_value = "20")]
max_items: u32,

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

impl Default for Diff {
Expand All @@ -472,22 +479,44 @@ impl Default for Diff {
#[cfg(feature = "cli")]
output_format: Default::default(),

items: Default::default(),
max_items: 20,
using_regexps: false,
}
}
}

impl Diff {
// 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]
impl Diff {
/// The maximum number of items to display.
pub fn max_items(&self) -> u32 {
self.max_items
}

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

/// Set the maximum number of items to display.
pub fn set_max_items(&mut self, n: u32) {
self.max_items = n;
}

/// 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;
}
}

/// Find and display code and data that is not transitively referenced by any
Expand Down
5 changes: 5 additions & 0 deletions twiggy/tests/expectations/diff_test_exact_wee_alloc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Delta Bytes │ Item
─────────────┼──────────────────
+243 ┊ goodbye
+15 ┊ hello
+258 ┊ Σ [2 Total Rows]
11 changes: 11 additions & 0 deletions twiggy/tests/expectations/diff_test_regex_wee_alloc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Delta Bytes │ Item
─────────────┼──────────────────
-1034 ┊ data[3]
-25 ┊ data[1]
-25 ┊ data[2]
-8 ┊ type[4]
-4 ┊ type[5]
+2 ┊ data[0]
-2 ┊ type[0]
-1 ┊ type[1]
-1097 ┊ Σ [8 Total Rows]
18 changes: 18 additions & 0 deletions twiggy/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,24 @@ test!(
"5"
);

test!(
diff_test_regex_wee_alloc,
"diff",
"./fixtures/wee_alloc.wasm",
"./fixtures/wee_alloc.2.wasm",
"--regex",
"(data|type)\\[\\d*\\]"
);

test!(
diff_test_exact_wee_alloc,
"diff",
"./fixtures/wee_alloc.wasm",
"./fixtures/wee_alloc.2.wasm",
"hello",
"goodbye"
);

test!(garbage, "garbage", "./fixtures/garbage.wasm");

test!(
Expand Down