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