]> git.lizzy.rs Git - rust.git/blob - src/librustc_passes/hir_stats.rs
Rollup merge of #53523 - phungleson:fix-impl-from-for-std-error, r=GuillaumeGomez
[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 && !self.seen.insert(id) {
65             return
66         }
67
68         let entry = self.data.entry(label).or_insert(NodeData {
69             count: 0,
70             size: 0,
71         });
72
73         entry.count += 1;
74         entry.size = ::std::mem::size_of_val(node);
75     }
76
77     fn print(&self, title: &str) {
78         let mut stats: Vec<_> = self.data.iter().collect();
79
80         stats.sort_by_key(|&(_, ref d)| d.count * d.size);
81
82         let mut total_size = 0;
83
84         println!("\n{}\n", title);
85
86         println!("{:<18}{:>18}{:>14}{:>14}",
87             "Name", "Accumulated Size", "Count", "Item Size");
88         println!("----------------------------------------------------------------");
89
90         for (label, data) in stats {
91             println!("{:<18}{:>18}{:>14}{:>14}",
92                 label,
93                 to_readable_str(data.count * data.size),
94                 to_readable_str(data.count),
95                 to_readable_str(data.size));
96
97             total_size += data.count * data.size;
98         }
99         println!("----------------------------------------------------------------");
100         println!("{:<18}{:>18}\n",
101                 "Total",
102                 to_readable_str(total_size));
103     }
104 }
105
106 impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
107     fn nested_visit_map<'this>(&'this mut self) -> hir_visit::NestedVisitorMap<'this, 'v> {
108         panic!("visit_nested_xxx must be manually implemented in this visitor")
109     }
110
111     fn visit_nested_item(&mut self, id: hir::ItemId) {
112         let nested_item = self.krate.unwrap().item(id.id);
113         self.visit_item(nested_item)
114     }
115
116     fn visit_nested_trait_item(&mut self, trait_item_id: hir::TraitItemId) {
117         let nested_trait_item = self.krate.unwrap().trait_item(trait_item_id);
118         self.visit_trait_item(nested_trait_item)
119     }
120
121     fn visit_nested_impl_item(&mut self, impl_item_id: hir::ImplItemId) {
122         let nested_impl_item = self.krate.unwrap().impl_item(impl_item_id);
123         self.visit_impl_item(nested_impl_item)
124     }
125
126     fn visit_nested_body(&mut self, body_id: hir::BodyId) {
127         let nested_body = self.krate.unwrap().body(body_id);
128         self.visit_body(nested_body)
129     }
130
131     fn visit_item(&mut self, i: &'v hir::Item) {
132         self.record("Item", Id::Node(i.id), i);
133         hir_visit::walk_item(self, i)
134     }
135
136     fn visit_mod(&mut self, m: &'v hir::Mod, _s: Span, n: NodeId) {
137         self.record("Mod", Id::None, m);
138         hir_visit::walk_mod(self, m, n)
139     }
140
141     fn visit_foreign_item(&mut self, i: &'v hir::ForeignItem) {
142         self.record("ForeignItem", Id::Node(i.id), i);
143         hir_visit::walk_foreign_item(self, i)
144     }
145
146     fn visit_local(&mut self, l: &'v hir::Local) {
147         self.record("Local", Id::Node(l.id), l);
148         hir_visit::walk_local(self, l)
149     }
150
151     fn visit_block(&mut self, b: &'v hir::Block) {
152         self.record("Block", Id::Node(b.id), b);
153         hir_visit::walk_block(self, b)
154     }
155
156     fn visit_stmt(&mut self, s: &'v hir::Stmt) {
157         self.record("Stmt", Id::Node(s.node.id()), s);
158         hir_visit::walk_stmt(self, s)
159     }
160
161     fn visit_arm(&mut self, a: &'v hir::Arm) {
162         self.record("Arm", Id::None, a);
163         hir_visit::walk_arm(self, a)
164     }
165
166     fn visit_pat(&mut self, p: &'v hir::Pat) {
167         self.record("Pat", Id::Node(p.id), p);
168         hir_visit::walk_pat(self, p)
169     }
170
171     fn visit_decl(&mut self, d: &'v hir::Decl) {
172         self.record("Decl", Id::None, d);
173         hir_visit::walk_decl(self, d)
174     }
175
176     fn visit_expr(&mut self, ex: &'v hir::Expr) {
177         self.record("Expr", Id::Node(ex.id), ex);
178         hir_visit::walk_expr(self, ex)
179     }
180
181     fn visit_ty(&mut self, t: &'v hir::Ty) {
182         self.record("Ty", Id::Node(t.id), t);
183         hir_visit::walk_ty(self, t)
184     }
185
186     fn visit_fn(&mut self,
187                 fk: hir_visit::FnKind<'v>,
188                 fd: &'v hir::FnDecl,
189                 b: hir::BodyId,
190                 s: Span,
191                 id: NodeId) {
192         self.record("FnDecl", Id::None, fd);
193         hir_visit::walk_fn(self, fk, fd, b, s, id)
194     }
195
196     fn visit_where_predicate(&mut self, predicate: &'v hir::WherePredicate) {
197         self.record("WherePredicate", Id::None, predicate);
198         hir_visit::walk_where_predicate(self, predicate)
199     }
200
201     fn visit_trait_item(&mut self, ti: &'v hir::TraitItem) {
202         self.record("TraitItem", Id::Node(ti.id), ti);
203         hir_visit::walk_trait_item(self, ti)
204     }
205
206     fn visit_impl_item(&mut self, ii: &'v hir::ImplItem) {
207         self.record("ImplItem", Id::Node(ii.id), ii);
208         hir_visit::walk_impl_item(self, ii)
209     }
210
211     fn visit_param_bound(&mut self, bounds: &'v hir::GenericBound) {
212         self.record("GenericBound", Id::None, bounds);
213         hir_visit::walk_param_bound(self, bounds)
214     }
215
216     fn visit_struct_field(&mut self, s: &'v hir::StructField) {
217         self.record("StructField", Id::Node(s.id), s);
218         hir_visit::walk_struct_field(self, s)
219     }
220
221     fn visit_variant(&mut self,
222                      v: &'v hir::Variant,
223                      g: &'v hir::Generics,
224                      item_id: NodeId) {
225         self.record("Variant", Id::None, v);
226         hir_visit::walk_variant(self, v, g, item_id)
227     }
228
229     fn visit_lifetime(&mut self, lifetime: &'v hir::Lifetime) {
230         self.record("Lifetime", Id::Node(lifetime.id), lifetime);
231         hir_visit::walk_lifetime(self, lifetime)
232     }
233
234     fn visit_qpath(&mut self, qpath: &'v hir::QPath, id: hir::HirId, span: Span) {
235         self.record("QPath", Id::None, qpath);
236         hir_visit::walk_qpath(self, qpath, id, span)
237     }
238
239     fn visit_path(&mut self, path: &'v hir::Path, _id: hir::HirId) {
240         self.record("Path", Id::None, path);
241         hir_visit::walk_path(self, path)
242     }
243
244     fn visit_path_segment(&mut self,
245                           path_span: Span,
246                           path_segment: &'v hir::PathSegment) {
247         self.record("PathSegment", Id::None, path_segment);
248         hir_visit::walk_path_segment(self, path_span, path_segment)
249     }
250
251     fn visit_assoc_type_binding(&mut self, type_binding: &'v hir::TypeBinding) {
252         self.record("TypeBinding", Id::Node(type_binding.id), type_binding);
253         hir_visit::walk_assoc_type_binding(self, type_binding)
254     }
255
256     fn visit_attribute(&mut self, attr: &'v ast::Attribute) {
257         self.record("Attribute", Id::Attr(attr.id), attr);
258     }
259
260     fn visit_macro_def(&mut self, macro_def: &'v hir::MacroDef) {
261         self.record("MacroDef", Id::Node(macro_def.id), macro_def);
262         hir_visit::walk_macro_def(self, macro_def)
263     }
264 }
265
266 impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
267
268     fn visit_mod(&mut self, m: &'v ast::Mod, _s: Span, _a: &[ast::Attribute], _n: NodeId) {
269         self.record("Mod", Id::None, m);
270         ast_visit::walk_mod(self, m)
271     }
272
273     fn visit_foreign_item(&mut self, i: &'v ast::ForeignItem) {
274         self.record("ForeignItem", Id::None, i);
275         ast_visit::walk_foreign_item(self, i)
276     }
277
278     fn visit_item(&mut self, i: &'v ast::Item) {
279         self.record("Item", Id::None, i);
280         ast_visit::walk_item(self, i)
281     }
282
283     fn visit_local(&mut self, l: &'v ast::Local) {
284         self.record("Local", Id::None, l);
285         ast_visit::walk_local(self, l)
286     }
287
288     fn visit_block(&mut self, b: &'v ast::Block) {
289         self.record("Block", Id::None, b);
290         ast_visit::walk_block(self, b)
291     }
292
293     fn visit_stmt(&mut self, s: &'v ast::Stmt) {
294         self.record("Stmt", Id::None, s);
295         ast_visit::walk_stmt(self, s)
296     }
297
298     fn visit_arm(&mut self, a: &'v ast::Arm) {
299         self.record("Arm", Id::None, a);
300         ast_visit::walk_arm(self, a)
301     }
302
303     fn visit_pat(&mut self, p: &'v ast::Pat) {
304         self.record("Pat", Id::None, p);
305         ast_visit::walk_pat(self, p)
306     }
307
308     fn visit_expr(&mut self, ex: &'v ast::Expr) {
309         self.record("Expr", Id::None, ex);
310         ast_visit::walk_expr(self, ex)
311     }
312
313     fn visit_ty(&mut self, t: &'v ast::Ty) {
314         self.record("Ty", Id::None, t);
315         ast_visit::walk_ty(self, t)
316     }
317
318     fn visit_fn(&mut self,
319                 fk: ast_visit::FnKind<'v>,
320                 fd: &'v ast::FnDecl,
321                 s: Span,
322                 _: NodeId) {
323         self.record("FnDecl", Id::None, fd);
324         ast_visit::walk_fn(self, fk, fd, s)
325     }
326
327     fn visit_trait_item(&mut self, ti: &'v ast::TraitItem) {
328         self.record("TraitItem", Id::None, ti);
329         ast_visit::walk_trait_item(self, ti)
330     }
331
332     fn visit_impl_item(&mut self, ii: &'v ast::ImplItem) {
333         self.record("ImplItem", Id::None, ii);
334         ast_visit::walk_impl_item(self, ii)
335     }
336
337     fn visit_param_bound(&mut self, bounds: &'v ast::GenericBound) {
338         self.record("GenericBound", Id::None, bounds);
339         ast_visit::walk_param_bound(self, bounds)
340     }
341
342     fn visit_struct_field(&mut self, s: &'v ast::StructField) {
343         self.record("StructField", Id::None, s);
344         ast_visit::walk_struct_field(self, s)
345     }
346
347     fn visit_variant(&mut self,
348                      v: &'v ast::Variant,
349                      g: &'v ast::Generics,
350                      item_id: NodeId) {
351         self.record("Variant", Id::None, v);
352         ast_visit::walk_variant(self, v, g, item_id)
353     }
354
355     fn visit_lifetime(&mut self, lifetime: &'v ast::Lifetime) {
356         self.record("Lifetime", Id::None, lifetime);
357         ast_visit::walk_lifetime(self, lifetime)
358     }
359
360     fn visit_mac(&mut self, mac: &'v ast::Mac) {
361         self.record("Mac", Id::None, mac);
362     }
363
364     fn visit_path_segment(&mut self,
365                           path_span: Span,
366                           path_segment: &'v 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: &'v 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: &'v ast::Attribute) {
377         self.record("Attribute", Id::None, attr);
378     }
379 }