@@ -14,152 +14,113 @@ use std::fs;
1414use std:: io:: { self , StdoutLock , Write } ;
1515use std:: time:: Instant ;
1616
17- #[ derive( Clone , Copy , Debug , PartialEq , Eq , Hash ) ]
18- pub enum ProfileCategory {
19- Parsing ,
20- Expansion ,
21- TypeChecking ,
22- BorrowChecking ,
23- Codegen ,
24- Linking ,
25- Other ,
26- }
27-
28- struct Categories < T > {
29- parsing : T ,
30- expansion : T ,
31- type_checking : T ,
32- borrow_checking : T ,
33- codegen : T ,
34- linking : T ,
35- other : T ,
36- }
37-
38- impl < T : Default > Categories < T > {
39- fn new ( ) -> Categories < T > {
40- Categories {
41- parsing : T :: default ( ) ,
42- expansion : T :: default ( ) ,
43- type_checking : T :: default ( ) ,
44- borrow_checking : T :: default ( ) ,
45- codegen : T :: default ( ) ,
46- linking : T :: default ( ) ,
47- other : T :: default ( ) ,
17+ macro_rules! define_categories {
18+ ( $( $name: ident, ) * ) => {
19+ #[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
20+ pub enum ProfileCategory {
21+ $( $name) ,*
4822 }
49- }
50- }
5123
52- impl < T > Categories < T > {
53- fn get ( & self , category : ProfileCategory ) -> & T {
54- match category {
55- ProfileCategory :: Parsing => & self . parsing ,
56- ProfileCategory :: Expansion => & self . expansion ,
57- ProfileCategory :: TypeChecking => & self . type_checking ,
58- ProfileCategory :: BorrowChecking => & self . borrow_checking ,
59- ProfileCategory :: Codegen => & self . codegen ,
60- ProfileCategory :: Linking => & self . linking ,
61- ProfileCategory :: Other => & self . other ,
24+ #[ allow( bad_style) ]
25+ struct Categories <T > {
26+ $( $name: T ) ,*
6227 }
63- }
6428
65- fn set ( & mut self , category : ProfileCategory , value : T ) {
66- match category {
67- ProfileCategory :: Parsing => self . parsing = value,
68- ProfileCategory :: Expansion => self . expansion = value,
69- ProfileCategory :: TypeChecking => self . type_checking = value,
70- ProfileCategory :: BorrowChecking => self . borrow_checking = value,
71- ProfileCategory :: Codegen => self . codegen = value,
72- ProfileCategory :: Linking => self . linking = value,
73- ProfileCategory :: Other => self . other = value,
29+ impl <T : Default > Categories <T > {
30+ fn new( ) -> Categories <T > {
31+ Categories {
32+ $( $name: T :: default ( ) ) ,*
33+ }
34+ }
7435 }
75- }
76- }
7736
78- struct CategoryData {
79- times : Categories < u64 > ,
80- query_counts : Categories < ( u64 , u64 ) > ,
81- }
37+ impl <T > Categories <T > {
38+ fn get( & self , category: ProfileCategory ) -> & T {
39+ match category {
40+ $( ProfileCategory :: $name => & self . $name) ,*
41+ }
42+ }
8243
83- impl CategoryData {
84- fn new ( ) -> CategoryData {
85- CategoryData {
86- times : Categories :: new ( ) ,
87- query_counts : Categories :: new ( ) ,
44+ fn set ( & mut self , category : ProfileCategory , value : T ) {
45+ match category {
46+ $ ( ProfileCategory :: $name => self . $name = value ) , *
47+ }
48+ }
8849 }
89- }
9050
91- fn print ( & self , lock : & mut StdoutLock ) {
92- macro_rules! p {
93- ( $name: tt, $rustic_name: ident) => {
94- let ( hits, total) = self . query_counts. $rustic_name;
95- let ( hits, total) = if total > 0 {
96- ( format!( "{:.2}" ,
97- ( ( ( hits as f32 ) / ( total as f32 ) ) * 100.0 ) ) , total. to_string( ) )
98- } else {
99- ( "" . into( ) , "" . into( ) )
100- } ;
101-
102- writeln!(
103- lock,
104- "| {0: <16} | {1: <14} | {2: <14} | {3: <8} |" ,
105- $name,
106- self . times. $rustic_name / 1_000_000 ,
107- total,
108- hits
109- ) . unwrap( ) ;
110- } ;
51+ struct CategoryData {
52+ times: Categories <u64 >,
53+ query_counts: Categories <( u64 , u64 ) >,
11154 }
11255
113- writeln ! ( lock, "| Phase | Time (ms) | Queries | Hits (%) |" )
114- . unwrap ( ) ;
115- writeln ! ( lock, "| ---------------- | -------------- | -------------- | -------- |" )
116- . unwrap ( ) ;
117-
118- p ! ( "Parsing" , parsing) ;
119- p ! ( "Expansion" , expansion) ;
120- p ! ( "TypeChecking" , type_checking) ;
121- p ! ( "BorrowChecking" , borrow_checking) ;
122- p ! ( "Codegen" , codegen) ;
123- p ! ( "Linking" , linking) ;
124- p ! ( "Other" , other) ;
125- }
56+ impl CategoryData {
57+ fn new( ) -> CategoryData {
58+ CategoryData {
59+ times: Categories :: new( ) ,
60+ query_counts: Categories :: new( ) ,
61+ }
62+ }
12663
127- fn json ( & self ) -> String {
128- macro_rules! j {
129- ( $category: tt, $rustic_name: ident) => { {
130- let ( hits, total) = self . query_counts. $rustic_name;
131-
132- format!(
133- "{{ \" category\" : {}, \" time_ms\" : {},
134- \" query_count\" : {}, \" query_hits\" : {} }}" ,
135- stringify!( $category) ,
136- self . times. $rustic_name / 1_000_000 ,
137- total,
138- format!( "{:.2}" , ( ( ( hits as f32 ) / ( total as f32 ) ) * 100.0 ) )
139- )
140- } }
141- }
64+ fn print( & self , lock: & mut StdoutLock ) {
65+ writeln!( lock, "| Phase | Time (ms) | Queries | Hits (%) |" )
66+ . unwrap( ) ;
67+ writeln!( lock, "| ---------------- | -------------- | -------------- | -------- |" )
68+ . unwrap( ) ;
69+
70+ $(
71+ let ( hits, total) = self . query_counts. $name;
72+ let ( hits, total) = if total > 0 {
73+ ( format!( "{:.2}" ,
74+ ( ( ( hits as f32 ) / ( total as f32 ) ) * 100.0 ) ) , total. to_string( ) )
75+ } else {
76+ ( "" . into( ) , "" . into( ) )
77+ } ;
78+
79+ writeln!(
80+ lock,
81+ "| {0: <16} | {1: <14} | {2: <14} | {3: <8} |" ,
82+ stringify!( $name) ,
83+ self . times. $name / 1_000_000 ,
84+ total,
85+ hits
86+ ) . unwrap( ) ;
87+ ) *
88+ }
14289
143- format ! ( "[
144- {},
145- {},
146- {},
147- {},
148- {},
149- {},
150- {}
151- ]" ,
152- j!( "Parsing" , parsing) ,
153- j!( "Expansion" , expansion) ,
154- j!( "TypeChecking" , type_checking) ,
155- j!( "BorrowChecking" , borrow_checking) ,
156- j!( "Codegen" , codegen) ,
157- j!( "Linking" , linking) ,
158- j!( "Other" , other)
159- )
90+ fn json( & self ) -> String {
91+ let mut json = String :: from( "[" ) ;
92+
93+ $(
94+ let ( hits, total) = self . query_counts. $name;
95+
96+ json. push_str( & format!(
97+ "{{ \" category\" : {}, \" time_ms\" : {},
98+ \" query_count\" : {}, \" query_hits\" : {} }}" ,
99+ stringify!( $name) ,
100+ self . times. $name / 1_000_000 ,
101+ total,
102+ format!( "{:.2}" , ( ( ( hits as f32 ) / ( total as f32 ) ) * 100.0 ) )
103+ ) ) ;
104+ ) *
105+
106+ json. push( ']' ) ;
107+
108+ json
109+ }
110+ }
160111 }
161112}
162113
114+ define_categories ! {
115+ Parsing ,
116+ Expansion ,
117+ TypeChecking ,
118+ BorrowChecking ,
119+ Codegen ,
120+ Linking ,
121+ Other ,
122+ }
123+
163124pub struct SelfProfiler {
164125 timer_stack : Vec < ProfileCategory > ,
165126 data : CategoryData ,
0 commit comments