]> git.lizzy.rs Git - rust.git/blob - src/librustc_passes/hir_stats.rs
(hopefully) fix pprust error
[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<'v>(krate: &'v 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<'this>(&'this mut self) -> hir_visit::NestedVisitorMap<'this, '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_trait_item(&mut self, trait_item_id: hir::TraitItemId) {
119         let nested_trait_item = self.krate.unwrap().trait_item(trait_item_id);
120         self.visit_trait_item(nested_trait_item)
121     }
122
123     fn visit_nested_impl_item(&mut self, impl_item_id: hir::ImplItemId) {
124         let nested_impl_item = self.krate.unwrap().impl_item(impl_item_id);
125         self.visit_impl_item(nested_impl_item)
126     }
127
128     fn visit_item(&mut self, i: &'v hir::Item) {
129         self.record("Item", Id::Node(i.id), i);
130         hir_visit::walk_item(self, i)
131     }
132
133     ///////////////////////////////////////////////////////////////////////////
134
135     fn visit_mod(&mut self, m: &'v hir::Mod, _s: Span, n: NodeId) {
136         self.record("Mod", Id::None, m);
137         hir_visit::walk_mod(self, m, n)
138     }
139     fn visit_foreign_item(&mut self, i: &'v hir::ForeignItem) {
140         self.record("ForeignItem", Id::Node(i.id), i);
141         hir_visit::walk_foreign_item(self, i)
142     }
143     fn visit_local(&mut self, l: &'v hir::Local) {
144         self.record("Local", Id::Node(l.id), l);
145         hir_visit::walk_local(self, l)
146     }
147     fn visit_block(&mut self, b: &'v hir::Block) {
148         self.record("Block", Id::Node(b.id), b);
149         hir_visit::walk_block(self, b)
150     }
151     fn visit_stmt(&mut self, s: &'v hir::Stmt) {
152         self.record("Stmt", Id::Node(s.node.id()), s);
153         hir_visit::walk_stmt(self, s)
154     }
155     fn visit_arm(&mut self, a: &'v hir::Arm) {
156         self.record("Arm", Id::None, a);
157         hir_visit::walk_arm(self, a)
158     }
159     fn visit_pat(&mut self, p: &'v hir::Pat) {
160         self.record("Pat", Id::Node(p.id), p);
161         hir_visit::walk_pat(self, p)
162     }
163     fn visit_decl(&mut self, d: &'v hir::Decl) {
164         self.record("Decl", Id::None, d);
165         hir_visit::walk_decl(self, d)
166     }
167     fn visit_expr(&mut self, ex: &'v hir::Expr) {
168         self.record("Expr", Id::Node(ex.id), ex);
169         hir_visit::walk_expr(self, ex)
170     }
171
172     fn visit_ty(&mut self, t: &'v hir::Ty) {
173         self.record("Ty", Id::Node(t.id), t);
174         hir_visit::walk_ty(self, t)
175     }
176
177     fn visit_fn(&mut self,
178                 fk: hir_visit::FnKind<'v>,
179                 fd: &'v hir::FnDecl,
180                 b: hir::BodyId,
181                 s: Span,
182                 id: NodeId) {
183         self.record("FnDecl", Id::None, fd);
184         hir_visit::walk_fn(self, fk, fd, b, s, id)
185     }
186
187     fn visit_where_predicate(&mut self, predicate: &'v hir::WherePredicate) {
188         self.record("WherePredicate", Id::None, predicate);
189         hir_visit::walk_where_predicate(self, predicate)
190     }
191
192     fn visit_trait_item(&mut self, ti: &'v hir::TraitItem) {
193         self.record("TraitItem", Id::Node(ti.id), ti);
194         hir_visit::walk_trait_item(self, ti)
195     }
196     fn visit_impl_item(&mut self, ii: &'v hir::ImplItem) {
197         self.record("ImplItem", Id::Node(ii.id), ii);
198         hir_visit::walk_impl_item(self, ii)
199     }
200
201     fn visit_ty_param_bound(&mut self, bounds: &'v hir::TyParamBound) {
202         self.record("TyParamBound", Id::None, bounds);
203         hir_visit::walk_ty_param_bound(self, bounds)
204     }
205
206     fn visit_struct_field(&mut self, s: &'v hir::StructField) {
207         self.record("StructField", Id::Node(s.id), s);
208         hir_visit::walk_struct_field(self, s)
209     }
210
211     fn visit_variant(&mut self,
212                      v: &'v hir::Variant,
213                      g: &'v hir::Generics,
214                      item_id: NodeId) {
215         self.record("Variant", Id::None, v);
216         hir_visit::walk_variant(self, v, g, item_id)
217     }
218     fn visit_lifetime(&mut self, lifetime: &'v hir::Lifetime) {
219         self.record("Lifetime", Id::Node(lifetime.id), lifetime);
220         hir_visit::walk_lifetime(self, lifetime)
221     }
222     fn visit_lifetime_def(&mut self, lifetime: &'v hir::LifetimeDef) {
223         self.record("LifetimeDef", Id::None, lifetime);
224         hir_visit::walk_lifetime_def(self, lifetime)
225     }
226     fn visit_qpath(&mut self, qpath: &'v hir::QPath, id: NodeId, span: Span) {
227         self.record("QPath", Id::None, qpath);
228         hir_visit::walk_qpath(self, qpath, id, span)
229     }
230     fn visit_path(&mut self, path: &'v hir::Path, _id: NodeId) {
231         self.record("Path", Id::None, path);
232         hir_visit::walk_path(self, path)
233     }
234     fn visit_path_segment(&mut self,
235                           path_span: Span,
236                           path_segment: &'v hir::PathSegment) {
237         self.record("PathSegment", Id::None, path_segment);
238         hir_visit::walk_path_segment(self, path_span, path_segment)
239     }
240     fn visit_assoc_type_binding(&mut self, type_binding: &'v hir::TypeBinding) {
241         self.record("TypeBinding", Id::Node(type_binding.id), type_binding);
242         hir_visit::walk_assoc_type_binding(self, type_binding)
243     }
244     fn visit_attribute(&mut self, attr: &'v ast::Attribute) {
245         self.record("Attribute", Id::Attr(attr.id), attr);
246     }
247     fn visit_macro_def(&mut self, macro_def: &'v hir::MacroDef) {
248         self.record("MacroDef", Id::Node(macro_def.id), macro_def);
249         hir_visit::walk_macro_def(self, macro_def)
250     }
251 }
252
253 impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
254
255     fn visit_mod(&mut self, m: &'v ast::Mod, _s: Span, _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,
306                 fk: ast_visit::FnKind<'v>,
307                 fd: &'v ast::FnDecl,
308                 s: Span,
309                 _: NodeId) {
310         self.record("FnDecl", Id::None, fd);
311         ast_visit::walk_fn(self, fk, fd, s)
312     }
313
314     fn visit_trait_item(&mut self, ti: &'v ast::TraitItem) {
315         self.record("TraitItem", Id::None, ti);
316         ast_visit::walk_trait_item(self, ti)
317     }
318
319     fn visit_impl_item(&mut self, ii: &'v ast::ImplItem) {
320         self.record("ImplItem", Id::None, ii);
321         ast_visit::walk_impl_item(self, ii)
322     }
323
324     fn visit_ty_param_bound(&mut self, bounds: &'v ast::TyParamBound) {
325         self.record("TyParamBound", Id::None, bounds);
326         ast_visit::walk_ty_param_bound(self, bounds)
327     }
328
329     fn visit_struct_field(&mut self, s: &'v ast::StructField) {
330         self.record("StructField", Id::None, s);
331         ast_visit::walk_struct_field(self, s)
332     }
333
334     fn visit_variant(&mut self,
335                      v: &'v ast::Variant,
336                      g: &'v ast::Generics,
337                      item_id: NodeId) {
338         self.record("Variant", Id::None, v);
339         ast_visit::walk_variant(self, v, g, item_id)
340     }
341
342     fn visit_lifetime(&mut self, lifetime: &'v ast::Lifetime) {
343         self.record("Lifetime", Id::None, lifetime);
344         ast_visit::walk_lifetime(self, lifetime)
345     }
346
347     fn visit_lifetime_def(&mut self, lifetime: &'v ast::LifetimeDef) {
348         self.record("LifetimeDef", Id::None, lifetime);
349         ast_visit::walk_lifetime_def(self, lifetime)
350     }
351
352     fn visit_mac(&mut self, mac: &'v ast::Mac) {
353         self.record("Mac", Id::None, mac);
354     }
355
356     fn visit_path_list_item(&mut self,
357                             prefix: &'v ast::Path,
358                             item: &'v ast::PathListItem) {
359         self.record("PathListItem", Id::None, item);
360         ast_visit::walk_path_list_item(self, prefix, item)
361     }
362
363     fn visit_path_segment(&mut self,
364                           path_span: Span,
365                           path_segment: &'v ast::PathSegment) {
366         self.record("PathSegment", Id::None, path_segment);
367         ast_visit::walk_path_segment(self, path_span, path_segment)
368     }
369
370     fn visit_assoc_type_binding(&mut self, type_binding: &'v ast::TypeBinding) {
371         self.record("TypeBinding", Id::None, type_binding);
372         ast_visit::walk_assoc_type_binding(self, type_binding)
373     }
374
375     fn visit_attribute(&mut self, attr: &'v ast::Attribute) {
376         self.record("Attribute", Id::None, attr);
377     }
378 }