Skip to content

Commit 2c9a72a

Browse files
committed
filtering monomorphizations
1 parent c291b8a commit 2c9a72a

File tree

5 files changed

+102
-6
lines changed

5 files changed

+102
-6
lines changed

β€Žanalyze/analyses/monos/mod.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::cmp;
22
use std::collections::{BTreeMap, BTreeSet};
33

4+
use regex;
45
use twiggy_ir as ir;
56
use twiggy_opt as opt;
67
use twiggy_traits as traits;
@@ -20,7 +21,14 @@ type MonosMap<'a> = BTreeMap<&'a str, Vec<(String, u32)>>;
2021

2122
/// Collect the monomorphizations of generic functions into a map, then
2223
/// process the entries and sort the resulting vector.
23-
fn collect_monomorphizations<'a>(items: &'a ir::Items) -> MonosMap {
24+
fn collect_monomorphizations<'a>(
25+
items: &'a ir::Items,
26+
opts: &opt::Monos,
27+
) -> Result<MonosMap<'a>, traits::Error> {
28+
let args_given = !opts.functions().is_empty();
29+
let using_regexps = opts.using_regexps();
30+
let regexps = regex::RegexSet::new(opts.functions())?;
31+
2432
let unsorted_monos: BTreeMap<&'a str, BTreeSet<(String, u32)>> = items
2533
.iter()
2634
.filter_map(|item| {
@@ -30,6 +38,11 @@ fn collect_monomorphizations<'a>(items: &'a ir::Items) -> MonosMap {
3038
None
3139
}
3240
})
41+
.filter(|(generic, inst)| match (args_given, using_regexps) {
42+
(true, true) => regexps.is_match(generic),
43+
(true, false) => opts.functions().iter().any(|name| name == generic),
44+
(false, _) => true,
45+
})
3346
.fold(BTreeMap::new(), |mut monos, (generic, inst)| {
3447
monos
3548
.entry(generic)
@@ -38,7 +51,7 @@ fn collect_monomorphizations<'a>(items: &'a ir::Items) -> MonosMap {
3851
monos
3952
});
4053

41-
unsorted_monos
54+
Ok(unsorted_monos
4255
.into_iter()
4356
.map(|(generic, inst_set)| {
4457
let mut insts = inst_set.into_iter().collect::<Vec<_>>();
@@ -47,7 +60,7 @@ fn collect_monomorphizations<'a>(items: &'a ir::Items) -> MonosMap {
4760
});
4861
(generic, insts)
4962
})
50-
.collect()
63+
.collect())
5164
}
5265

5366
/// Helper function usedd to summarize a sequence of `MonosEntry` objects.
@@ -175,7 +188,7 @@ pub fn monos(
175188
items: &mut ir::Items,
176189
opts: &opt::Monos,
177190
) -> Result<Box<dyn traits::Emit>, traits::Error> {
178-
let monos_map = collect_monomorphizations(&items);
191+
let monos_map = collect_monomorphizations(&items, &opts)?;
179192
let mut monos = process_monomorphizations(monos_map, &opts);
180193
monos = add_stats(monos, &opts);
181194
Ok(Box::new(Monos { monos }) as Box<_>)

β€Žopt/definitions.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,14 +361,18 @@ impl Paths {
361361
/// List the generic function monomorphizations that are contributing to
362362
/// code bloat.
363363
#[wasm_bindgen]
364-
#[derive(Clone, Debug)]
365-
#[derive(StructOpt)]
364+
#[derive(Clone, Debug, StructOpt)]
366365
pub struct Monos {
367366
/// The path to the input binary to size profile.
368367
#[cfg(feature = "cli")]
369368
#[structopt(parse(from_os_str))]
370369
input: path::PathBuf,
371370

371+
/// The names of the generic functions whose monomorphizations
372+
/// should be printed.
373+
#[cfg(feature = "cli")]
374+
functions: Vec<String>,
375+
372376
/// The parse mode for the input binary data.
373377
#[cfg(feature = "cli")]
374378
#[structopt(short = "d", long = "mode", default_value = "auto")]
@@ -411,6 +415,10 @@ pub struct Monos {
411415
/// function. Overrides -n <max_monos>
412416
#[structopt(long = "all-monos")]
413417
all_monos: bool,
418+
419+
/// Whether or not `names` should be treated as regular expressions.
420+
#[structopt(long = "regex")]
421+
using_regexps: bool,
414422
}
415423

416424
impl Default for Monos {
@@ -425,17 +433,31 @@ impl Default for Monos {
425433
#[cfg(feature = "cli")]
426434
output_format: Default::default(),
427435

436+
functions: Default::default(),
437+
428438
only_generics: false,
429439
max_generics: 10,
430440
max_monos: 10,
431441

432442
all_generics_and_monos: false,
433443
all_generics: false,
434444
all_monos: false,
445+
446+
using_regexps: false,
435447
}
436448
}
437449
}
438450

451+
impl Monos {
452+
// TODO: wasm-bindgen doesn't support sending Vec<String> across the wasm
453+
// ABI boundary yet.
454+
455+
/// The functions to find call paths to.
456+
pub fn functions(&self) -> &[String] {
457+
&self.functions
458+
}
459+
}
460+
439461
#[wasm_bindgen]
440462
impl Monos {
441463
/// Construct a new, default `Monos`.
@@ -467,6 +489,11 @@ impl Monos {
467489
}
468490
}
469491

492+
/// Whether or not `functions` should be treated as regular expressions.
493+
pub fn using_regexps(&self) -> bool {
494+
self.using_regexps
495+
}
496+
470497
/// Set whether to hide individual monomorphizations and only show the
471498
/// generic functions.
472499
pub fn set_only_generics(&mut self, do_it: bool) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Apprx. Bloat Bytes β”‚ Apprx. Bloat % β”‚ Bytes β”‚ % β”‚ Monomorphizations
2+
────────────────────┼────────────────┼───────┼───────┼─────────────────────────────────────────────────
3+
2141 β”Š 3.68% β”Š 3249 β”Š 5.58% β”Š alloc::slice::merge_sort
4+
β”Š β”Š 1108 β”Š 1.90% β”Š alloc::slice::merge_sort::hb3d195f9800bdad6
5+
β”Š β”Š 1108 β”Š 1.90% β”Š alloc::slice::merge_sort::hfcf2318d7dc71d03
6+
β”Š β”Š 1033 β”Š 1.77% β”Š alloc::slice::merge_sort::hcfca67f5c75a52ef
7+
236 β”Š 0.41% β”Š 357 β”Š 0.61% β”Š alloc::slice::insert_head
8+
β”Š β”Š 121 β”Š 0.21% β”Š alloc::slice::insert_head::h2cdb84a455761146
9+
β”Š β”Š 121 β”Š 0.21% β”Š alloc::slice::insert_head::haf6e08236bab8bde
10+
β”Š β”Š 115 β”Š 0.20% β”Š alloc::slice::insert_head::hed0e79da03eeec8b
11+
2377 β”Š 4.08% β”Š 3606 β”Š 6.20% β”Š Ξ£ [8 Total Rows]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Apprx. Bloat Bytes β”‚ Apprx. Bloat % β”‚ Bytes β”‚ % β”‚ Monomorphizations
2+
────────────────────┼────────────────┼───────┼───────┼─────────────────────────────────────────────────────────────────────────────────────
3+
2141 β”Š 3.68% β”Š 3249 β”Š 5.58% β”Š alloc::slice::merge_sort
4+
β”Š β”Š 1108 β”Š 1.90% β”Š alloc::slice::merge_sort::hb3d195f9800bdad6
5+
β”Š β”Š 1108 β”Š 1.90% β”Š alloc::slice::merge_sort::hfcf2318d7dc71d03
6+
β”Š β”Š 1033 β”Š 1.77% β”Š alloc::slice::merge_sort::hcfca67f5c75a52ef
7+
658 β”Š 1.13% β”Š 843 β”Š 1.45% β”Š <alloc::raw_vec::RawVec<T, A>>::double
8+
β”Š β”Š 185 β”Š 0.32% β”Š <alloc::raw_vec::RawVec<T, A>>::double::h28f86621ee2a10aa
9+
β”Š β”Š 185 β”Š 0.32% β”Š <alloc::raw_vec::RawVec<T, A>>::double::h956450b93bdc9e1e
10+
β”Š β”Š 185 β”Š 0.32% β”Š <alloc::raw_vec::RawVec<T, A>>::double::hcb2fb5861b96a3b0
11+
β”Š β”Š 176 β”Š 0.30% β”Š <alloc::raw_vec::RawVec<T, A>>::double::ha715b4e5cc3c60ae
12+
β”Š β”Š 112 β”Š 0.19% β”Š <alloc::raw_vec::RawVec<T, A>>::double::h77ff8547127c5db2
13+
236 β”Š 0.41% β”Š 357 β”Š 0.61% β”Š alloc::slice::insert_head
14+
β”Š β”Š 121 β”Š 0.21% β”Š alloc::slice::insert_head::h2cdb84a455761146
15+
β”Š β”Š 121 β”Š 0.21% β”Š alloc::slice::insert_head::haf6e08236bab8bde
16+
β”Š β”Š 115 β”Š 0.20% β”Š alloc::slice::insert_head::hed0e79da03eeec8b
17+
210 β”Š 0.36% β”Š 290 β”Š 0.50% β”Š <alloc::vec::Vec<T>>::push
18+
β”Š β”Š 80 β”Š 0.14% β”Š <alloc::vec::Vec<T>>::push::h98b02eda22d1ca25
19+
β”Š β”Š 71 β”Š 0.12% β”Š <alloc::vec::Vec<T>>::push::h5729b9e7651ef67b
20+
β”Š β”Š 71 β”Š 0.12% β”Š <alloc::vec::Vec<T>>::push::hc927b4bedb35b00d
21+
β”Š β”Š 68 β”Š 0.12% β”Š <alloc::vec::Vec<T>>::push::h9415ef699ccc65d8
22+
40 β”Š 0.07% β”Š 60 β”Š 0.10% β”Š <alloc::raw_vec::RawVec<T, A> as core::ops::drop::Drop>::drop
23+
β”Š β”Š 20 β”Š 0.03% β”Š <alloc::raw_vec::RawVec<T, A> as core::ops::drop::Drop>::drop::h420ff33e8bc0de30
24+
β”Š β”Š 20 β”Š 0.03% β”Š <alloc::raw_vec::RawVec<T, A> as core::ops::drop::Drop>::drop::hab66cea5bda1ed02
25+
β”Š β”Š 20 β”Š 0.03% β”Š <alloc::raw_vec::RawVec<T, A> as core::ops::drop::Drop>::drop::hbe243f4c44295f3d
26+
0 β”Š 0.00% β”Š 871 β”Š 1.50% β”Š ... and 16 more.
27+
3285 β”Š 5.64% β”Š 5670 β”Š 9.74% β”Š Ξ£ [39 Total Rows]

β€Žtwiggy/tests/all/monos_tests.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,21 @@ test!(
5757
"-f",
5858
"json"
5959
);
60+
61+
test!(
62+
monos_regex,
63+
"monos",
64+
"./fixtures/monos.wasm",
65+
"--regex",
66+
"-m",
67+
"5",
68+
"^<?alloc::.*"
69+
);
70+
71+
test!(
72+
monos_functions,
73+
"monos",
74+
"./fixtures/monos.wasm",
75+
"alloc::slice::insert_head",
76+
"alloc::slice::merge_sort"
77+
);

0 commit comments

Comments
Β (0)