@@ -65,16 +65,16 @@ pub fn print_hir_stats(tcx: TyCtxt<'_>) {
6565 StatCollector { tcx : Some ( tcx) , nodes : FxHashMap :: default ( ) , seen : FxHashSet :: default ( ) } ;
6666 tcx. hir_walk_toplevel_module ( & mut collector) ;
6767 tcx. hir_walk_attributes ( & mut collector) ;
68- collector. print ( "HIR STATS" , "hir-stats" ) ;
68+ collector. print ( tcx , "HIR STATS" , "hir-stats" ) ;
6969}
7070
71- pub fn print_ast_stats ( krate : & ast :: Crate , title : & str , prefix : & str ) {
71+ pub fn print_ast_stats ( tcx : TyCtxt < ' _ > , krate : & ast :: Crate ) {
7272 use rustc_ast:: visit:: Visitor ;
7373
7474 let mut collector =
7575 StatCollector { tcx : None , nodes : FxHashMap :: default ( ) , seen : FxHashSet :: default ( ) } ;
7676 collector. visit_crate ( krate) ;
77- collector. print ( title , prefix ) ;
77+ collector. print ( tcx , "POST EXPANSION AST STATS" , "ast-stats" ) ;
7878}
7979
8080impl < ' k > StatCollector < ' k > {
@@ -116,29 +116,48 @@ impl<'k> StatCollector<'k> {
116116 }
117117 }
118118
119- fn print ( & self , title : & str , prefix : & str ) {
119+ fn print ( & self , tcx : TyCtxt < ' _ > , title : & str , prefix : & str ) {
120+ use std:: fmt:: Write ;
121+
120122 // We will soon sort, so the initial order does not matter.
121123 #[ allow( rustc:: potential_query_instability) ]
122124 let mut nodes: Vec < _ > = self . nodes . iter ( ) . collect ( ) ;
123125 nodes. sort_by_cached_key ( |( label, node) | ( node. stats . accum_size ( ) , label. to_owned ( ) ) ) ;
126+ nodes. reverse ( ) ; // bigger items first
127+
128+ let name_w = 18 ;
129+ let acc_size1_w = 10 ;
130+ let acc_size2_w = 8 ; // " (NN.N%)"
131+ let acc_size_w = acc_size1_w + acc_size2_w;
132+ let count_w = 14 ;
133+ let item_size_w = 14 ;
134+ let banner_w = name_w + acc_size_w + count_w + item_size_w;
124135
125136 let total_size = nodes. iter ( ) . map ( |( _, node) | node. stats . accum_size ( ) ) . sum ( ) ;
126137 let total_count = nodes. iter ( ) . map ( |( _, node) | node. stats . count ) . sum ( ) ;
127138
128- eprintln ! ( "{prefix} {title}" ) ;
129- eprintln ! (
130- "{} {:<18}{:>18}{:>14}{:>14}" ,
131- prefix, "Name" , "Accumulated Size" , "Count" , "Item Size"
139+ // We write all the text into a string and print it with a single
140+ // `eprint!`. This is an attempt to minimize interleaved text if multiple
141+ // rustc processes are printing macro-stats at the same time (e.g. with
142+ // `RUSTFLAGS='-Zinput-stats' cargo build`). It still doesn't guarantee
143+ // non-interleaving, though.
144+ let mut s = String :: new ( ) ;
145+ _ = writeln ! ( s, "{prefix} {}" , "=" . repeat( banner_w) ) ;
146+ _ = writeln ! ( s, "{prefix} {title}: {}" , tcx. crate_name( hir:: def_id:: LOCAL_CRATE ) ) ;
147+ _ = writeln ! (
148+ s,
149+ "{prefix} {:<name_w$}{:>acc_size_w$}{:>count_w$}{:>item_size_w$}" ,
150+ "Name" , "Accumulated Size" , "Count" , "Item Size"
132151 ) ;
133- eprintln ! ( "{prefix} ----------------------------------------------------------------" ) ;
152+ _ = writeln ! ( s , "{prefix} {}" , "-" . repeat ( banner_w ) ) ;
134153
135154 let percent = |m, n| ( m * 100 ) as f64 / n as f64 ;
136155
137156 for ( label, node) in nodes {
138157 let size = node. stats . accum_size ( ) ;
139- eprintln ! (
140- "{} {:<18}{:>10} ({:4.1}%){:>14}{:>14}" ,
141- prefix,
158+ _ = writeln ! (
159+ s ,
160+ "{ prefix} {:<name_w$}{:>acc_size1_w$} ({:4.1}%){:>count_w$}{:>item_size_w$}" ,
142161 label,
143162 usize_with_underscores( size) ,
144163 percent( size, total_size) ,
@@ -155,9 +174,9 @@ impl<'k> StatCollector<'k> {
155174
156175 for ( label, subnode) in subnodes {
157176 let size = subnode. accum_size ( ) ;
158- eprintln ! (
159- "{} - {:<18}{:>10} ({:4.1}%){:>14}" ,
160- prefix,
177+ _ = writeln ! (
178+ s ,
179+ "{ prefix} - {:<name_w$}{:>acc_size1_w$} ({:4.1}%){:>count_w$}" ,
161180 label,
162181 usize_with_underscores( size) ,
163182 percent( size, total_size) ,
@@ -166,15 +185,17 @@ impl<'k> StatCollector<'k> {
166185 }
167186 }
168187 }
169- eprintln ! ( "{prefix} ----------------------------------------------------------------" ) ;
170- eprintln ! (
171- "{} {:<18}{:>10} {:>14}" ,
172- prefix,
188+ _ = writeln ! ( s , "{prefix} {}" , "-" . repeat ( banner_w ) ) ;
189+ _ = writeln ! (
190+ s ,
191+ "{ prefix} {:<name_w$}{:>acc_size1_w$}{:>acc_size2_w$}{:>count_w$}" ,
173192 "Total" ,
174193 usize_with_underscores( total_size) ,
194+ "" ,
175195 usize_with_underscores( total_count) ,
176196 ) ;
177- eprintln ! ( "{prefix}" ) ;
197+ _ = writeln ! ( s, "{prefix} {}" , "=" . repeat( banner_w) ) ;
198+ eprint ! ( "{s}" ) ;
178199 }
179200}
180201
0 commit comments