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