]> git.lizzy.rs Git - rust.git/blob - src/librustc_passes/hir_stats.rs
Rollup merge of #68504 - tmiasko:check-pass, r=alexcrichton
[rust.git] / src / librustc_passes / hir_stats.rs
1 // The visitors in this module collect sizes and counts of the most important
2 // pieces of AST and HIR. The resulting numbers are good approximations but not
3 // completely accurate (some things might be counted twice, others missed).
4
5 use rustc::hir::map::Map;
6 use rustc::util::common::to_readable_str;
7 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
8 use rustc_hir as hir;
9 use rustc_hir::intravisit as hir_visit;
10 use rustc_hir::HirId;
11 use rustc_span::Span;
12 use syntax::ast::{self, AttrId, NodeId};
13 use syntax::visit as ast_visit;
14
15 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
16 enum Id {
17     Node(HirId),
18     Attr(AttrId),
19     None,
20 }
21
22 struct NodeData {
23     count: usize,
24     size: usize,
25 }
26
27 struct StatCollector<'k> {
28     krate: Option<&'k hir::Crate<'k>>,
29     data: FxHashMap<&'static str, NodeData>,
30     seen: FxHashSet<Id>,
31 }
32
33 pub fn print_hir_stats(krate: &hir::Crate<'_>) {
34     let mut collector = StatCollector {
35         krate: Some(krate),
36         data: FxHashMap::default(),
37         seen: FxHashSet::default(),
38     };
39     hir_visit::walk_crate(&mut collector, krate);
40     collector.print("HIR STATS");
41 }
42
43 pub fn print_ast_stats(krate: &ast::Crate, title: &str) {
44     let mut collector =
45         StatCollector { krate: None, data: FxHashMap::default(), seen: FxHashSet::default() };
46     ast_visit::walk_crate(&mut collector, krate);
47     collector.print(title);
48 }
49
50 impl<'k> StatCollector<'k> {
51     fn record<T>(&mut self, label: &'static str, id: Id, node: &T) {
52         if id != Id::None && !self.seen.insert(id) {
53             return;
54         }
55
56         let entry = self.data.entry(label).or_insert(NodeData { count: 0, size: 0 });
57
58         entry.count += 1;
59         entry.size = std::mem::size_of_val(node);
60     }
61
62     fn print(&self, title: &str) {
63         let mut stats: Vec<_> = self.data.iter().collect();
64
65         stats.sort_by_key(|&(_, ref d)| d.count * d.size);
66
67         let mut total_size = 0;
68
69         println!("\n{}\n", title);
70
71         println!("{:<18}{:>18}{:>14}{:>14}", "Name", "Accumulated Size", "Count", "Item Size");
72         println!("----------------------------------------------------------------");
73
74         for (label, data) in stats {
75             println!(
76                 "{:<18}{:>18}{:>14}{:>14}",
77                 label,
78                 to_readable_str(data.count * data.size),
79                 to_readable_str(data.count),
80                 to_readable_str(data.size)
81             );
82
83             total_size += data.count * data.size;
84         }
85         println!("----------------------------------------------------------------");
86         println!("{:<18}{:>18}\n", "Total", to_readable_str(total_size));
87     }
88 }
89
90 impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
91     fn visit_param(&mut self, param: &'v hir::Param<'v>) {
92         self.record("Param", Id::Node(param.hir_id), param);
93         hir_visit::walk_param(self, param)
94     }
95
96     type Map = Map<'v>;
97
98     fn nested_visit_map(&mut self) -> hir_visit::NestedVisitorMap<'_, Self::Map> {
99         panic!("visit_nested_xxx must be manually implemented in this visitor")
100     }
101
102     fn visit_nested_item(&mut self, id: hir::ItemId) {
103         let nested_item = self.krate.unwrap().item(id.id);
104         self.visit_item(nested_item)
105     }
106
107     fn visit_nested_trait_item(&mut self, trait_item_id: hir::TraitItemId) {
108         let nested_trait_item = self.krate.unwrap().trait_item(trait_item_id);
109         self.visit_trait_item(nested_trait_item)
110     }
111
112     fn visit_nested_impl_item(&mut self, impl_item_id: hir::ImplItemId) {
113         let nested_impl_item = self.krate.unwrap().impl_item(impl_item_id);
114         self.visit_impl_item(nested_impl_item)
115     }
116
117     fn visit_nested_body(&mut self, body_id: hir::BodyId) {
118         let nested_body = self.krate.unwrap().body(body_id);
119         self.visit_body(nested_body)
120     }
121
122     fn visit_item(&mut self, i: &'v hir::Item<'v>) {
123         self.record("Item", Id::Node(i.hir_id), i);
124         hir_visit::walk_item(self, i)
125     }
126
127     fn visit_mod(&mut self, m: &'v hir::Mod<'v>, _s: Span, n: hir::HirId) {
128         self.record("Mod", Id::None, m);
129         hir_visit::walk_mod(self, m, n)
130     }
131
132     fn visit_foreign_item(&mut self, i: &'v hir::ForeignItem<'v>) {
133         self.record("ForeignItem", Id::Node(i.hir_id), i);
134         hir_visit::walk_foreign_item(self, i)
135     }
136
137     fn visit_local(&mut self, l: &'v hir::Local<'v>) {
138         self.record("Local", Id::Node(l.hir_id), l);
139         hir_visit::walk_local(self, l)
140     }
141
142     fn visit_block(&mut self, b: &'v hir::Block<'v>) {
143         self.record("Block", Id::Node(b.hir_id), b);
144         hir_visit::walk_block(self, b)
145     }
146
147     fn visit_stmt(&mut self, s: &'v hir::Stmt<'v>) {
148         self.record("Stmt", Id::Node(s.hir_id), s);
149         hir_visit::walk_stmt(self, s)
150     }
151
152     fn visit_arm(&mut self, a: &'v hir::Arm<'v>) {
153         self.record("Arm", Id::Node(a.hir_id), a);
154         hir_visit::walk_arm(self, a)
155     }
156
157     fn visit_pat(&mut self, p: &'v hir::Pat<'v>) {
158         self.record("Pat", Id::Node(p.hir_id), p);
159         hir_visit::walk_pat(self, p)
160     }
161
162     fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
163         self.record("Expr", Id::Node(ex.hir_id), ex);
164         hir_visit::walk_expr(self, ex)
165     }
166
167     fn visit_ty(&mut self, t: &'v hir::Ty<'v>) {
168         self.record("Ty", Id::Node(t.hir_id), t);
169         hir_visit::walk_ty(self, t)
170     }
171
172     fn visit_fn(
173         &mut self,
174         fk: hir_visit::FnKind<'v>,
175         fd: &'v hir::FnDecl<'v>,
176         b: hir::BodyId,
177         s: Span,
178         id: hir::HirId,
179     ) {
180         self.record("FnDecl", Id::None, fd);
181         hir_visit::walk_fn(self, fk, fd, b, s, id)
182     }
183
184     fn visit_where_predicate(&mut self, predicate: &'v hir::WherePredicate<'v>) {
185         self.record("WherePredicate", Id::None, predicate);
186         hir_visit::walk_where_predicate(self, predicate)
187     }
188
189     fn visit_trait_item(&mut self, ti: &'v hir::TraitItem<'v>) {
190         self.record("TraitItem", Id::Node(ti.hir_id), ti);
191         hir_visit::walk_trait_item(self, ti)
192     }
193
194     fn visit_impl_item(&mut self, ii: &'v hir::ImplItem<'v>) {
195         self.record("ImplItem", Id::Node(ii.hir_id), ii);
196         hir_visit::walk_impl_item(self, ii)
197     }
198
199     fn visit_param_bound(&mut self, bounds: &'v hir::GenericBound<'v>) {
200         self.record("GenericBound", Id::None, bounds);
201         hir_visit::walk_param_bound(self, bounds)
202     }
203
204     fn visit_struct_field(&mut self, s: &'v hir::StructField<'v>) {
205         self.record("StructField", Id::Node(s.hir_id), s);
206         hir_visit::walk_struct_field(self, s)
207     }
208
209     fn visit_variant(
210         &mut self,
211         v: &'v hir::Variant<'v>,
212         g: &'v hir::Generics<'v>,
213         item_id: hir::HirId,
214     ) {
215         self.record("Variant", Id::None, v);
216         hir_visit::walk_variant(self, v, g, item_id)
217     }
218
219     fn visit_lifetime(&mut self, lifetime: &'v hir::Lifetime) {
220         self.record("Lifetime", Id::Node(lifetime.hir_id), lifetime);
221         hir_visit::walk_lifetime(self, lifetime)
222     }
223
224     fn visit_qpath(&mut self, qpath: &'v hir::QPath<'v>, id: hir::HirId, span: Span) {
225         self.record("QPath", Id::None, qpath);
226         hir_visit::walk_qpath(self, qpath, id, span)
227     }
228
229     fn visit_path(&mut self, path: &'v hir::Path<'v>, _id: hir::HirId) {
230         self.record("Path", Id::None, path);
231         hir_visit::walk_path(self, path)
232     }
233
234     fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v hir::PathSegment<'v>) {
235         self.record("PathSegment", Id::None, path_segment);
236         hir_visit::walk_path_segment(self, path_span, path_segment)
237     }
238
239     fn visit_assoc_type_binding(&mut self, type_binding: &'v hir::TypeBinding<'v>) {
240         self.record("TypeBinding", Id::Node(type_binding.hir_id), type_binding);
241         hir_visit::walk_assoc_type_binding(self, type_binding)
242     }
243
244     fn visit_attribute(&mut self, attr: &'v ast::Attribute) {
245         self.record("Attribute", Id::Attr(attr.id), attr);
246     }
247
248     fn visit_macro_def(&mut self, macro_def: &'v hir::MacroDef<'v>) {
249         self.record("MacroDef", Id::Node(macro_def.hir_id), macro_def);
250         hir_visit::walk_macro_def(self, macro_def)
251     }
252 }
253
254 impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
255     fn visit_mod(&mut self, m: &'v ast::Mod, _s: Span, _a: &[ast::Attribute], _n: NodeId) {
256         self.record("Mod", Id::None, m);
257         ast_visit::walk_mod(self, m)
258     }
259
260     fn visit_foreign_item(&mut self, i: &'v ast::ForeignItem) {
261         self.record("ForeignItem", Id::None, i);
262         ast_visit::walk_foreign_item(self, i)
263     }
264
265     fn visit_item(&mut self, i: &'v ast::Item) {
266         self.record("Item", Id::None, i);
267         ast_visit::walk_item(self, i)
268     }
269
270     fn visit_local(&mut self, l: &'v ast::Local) {
271         self.record("Local", Id::None, l);
272         ast_visit::walk_local(self, l)
273     }
274
275     fn visit_block(&mut self, b: &'v ast::Block) {
276         self.record("Block", Id::None, b);
277         ast_visit::walk_block(self, b)
278     }
279
280     fn visit_stmt(&mut self, s: &'v ast::Stmt) {
281         self.record("Stmt", Id::None, s);
282         ast_visit::walk_stmt(self, s)
283     }
284
285     fn visit_arm(&mut self, a: &'v ast::Arm) {
286         self.record("Arm", Id::None, a);
287         ast_visit::walk_arm(self, a)
288     }
289
290     fn visit_pat(&mut self, p: &'v ast::Pat) {
291         self.record("Pat", Id::None, p);
292         ast_visit::walk_pat(self, p)
293     }
294
295     fn visit_expr(&mut self, ex: &'v ast::Expr) {
296         self.record("Expr", Id::None, ex);
297         ast_visit::walk_expr(self, ex)
298     }
299
300     fn visit_ty(&mut self, t: &'v ast::Ty) {
301         self.record("Ty", Id::None, t);
302         ast_visit::walk_ty(self, t)
303     }
304
305     fn visit_fn(&mut self, fk: ast_visit::FnKind<'v>, fd: &'v ast::FnDecl, s: Span, _: NodeId) {
306         self.record("FnDecl", Id::None, fd);
307         ast_visit::walk_fn(self, fk, fd, s)
308     }
309
310     fn visit_trait_item(&mut self, ti: &'v ast::AssocItem) {
311         self.record("TraitItem", Id::None, ti);
312         ast_visit::walk_trait_item(self, ti)
313     }
314
315     fn visit_impl_item(&mut self, ii: &'v ast::AssocItem) {
316         self.record("ImplItem", Id::None, ii);
317         ast_visit::walk_impl_item(self, ii)
318     }
319
320     fn visit_param_bound(&mut self, bounds: &'v ast::GenericBound) {
321         self.record("GenericBound", Id::None, bounds);
322         ast_visit::walk_param_bound(self, bounds)
323     }
324
325     fn visit_struct_field(&mut self, s: &'v ast::StructField) {
326         self.record("StructField", Id::None, s);
327         ast_visit::walk_struct_field(self, s)
328     }
329
330     fn visit_variant(&mut self, v: &'v ast::Variant) {
331         self.record("Variant", Id::None, v);
332         ast_visit::walk_variant(self, v)
333     }
334
335     fn visit_lifetime(&mut self, lifetime: &'v ast::Lifetime) {
336         self.record("Lifetime", Id::None, lifetime);
337         ast_visit::walk_lifetime(self, lifetime)
338     }
339
340     fn visit_mac(&mut self, mac: &'v ast::Mac) {
341         self.record("Mac", Id::None, mac);
342     }
343
344     fn visit_path_segment(&mut self, path_span: Span, path_segment: &'v ast::PathSegment) {
345         self.record("PathSegment", Id::None, path_segment);
346         ast_visit::walk_path_segment(self, path_span, path_segment)
347     }
348
349     fn visit_assoc_ty_constraint(&mut self, constraint: &'v ast::AssocTyConstraint) {
350         self.record("AssocTyConstraint", Id::None, constraint);
351         ast_visit::walk_assoc_ty_constraint(self, constraint)
352     }
353
354     fn visit_attribute(&mut self, attr: &'v ast::Attribute) {
355         self.record("Attribute", Id::None, attr);
356     }
357 }