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