1919
2020use std:: collections:: HashMap ;
2121use std:: iter:: Sum ;
22+ use std:: mem;
2223
2324use gazebo:: prelude:: * ;
2425
2526use crate :: eval:: bc:: opcode:: BcOpcode ;
2627use crate :: eval:: runtime:: evaluator:: EvaluatorError ;
2728use crate :: eval:: runtime:: profile:: csv:: CsvWriter ;
29+ use crate :: eval:: runtime:: profile:: data:: ProfileDataImpl ;
30+ use crate :: eval:: ProfileData ;
31+ use crate :: eval:: ProfileMode ;
2832
29- #[ derive( Default , Clone , Dupe , Copy ) ]
33+ #[ derive( Default , Clone , Dupe , Copy , Debug ) ]
3034struct BcInstrStat {
3135 count : u64 ,
3236}
@@ -41,19 +45,20 @@ impl<'a> Sum<&'a BcInstrStat> for BcInstrStat {
4145 }
4246}
4347
44- #[ derive( Default , Clone , Copy , Dupe ) ]
48+ #[ derive( Default , Clone , Copy , Dupe , Debug ) ]
4549struct BcInstrPairsStat {
4650 count : u64 ,
4751 // We are not measuring time here, because even time for single opcode
4852 // is not very accurate or helpful, and time for pairs is even less helpful.
4953}
5054
51- struct BcProfileData {
55+ #[ derive( Clone , Debug ) ]
56+ pub ( crate ) struct BcProfileData {
5257 by_instr : [ BcInstrStat ; BcOpcode :: COUNT ] ,
5358}
5459
55- #[ derive( Default ) ]
56- struct BcPairsProfileData {
60+ #[ derive( Default , Clone , Debug ) ]
61+ pub ( crate ) struct BcPairsProfileData {
5762 last : Option < BcOpcode > ,
5863 by_instr : HashMap < [ BcOpcode ; 2 ] , BcInstrPairsStat > ,
5964}
@@ -72,7 +77,7 @@ impl BcProfileData {
7277 self . by_instr [ opcode as usize ] . count += 1 ;
7378 }
7479
75- fn gen_csv ( & self ) -> String {
80+ pub ( crate ) fn gen_csv ( & self ) -> String {
7681 let mut by_instr: Vec < _ > = self
7782 . by_instr
7883 . iter ( )
@@ -112,7 +117,7 @@ impl BcPairsProfileData {
112117 self . last = Some ( opcode) ;
113118 }
114119
115- fn gen_csv ( & self ) -> String {
120+ pub ( crate ) fn gen_csv ( & self ) -> String {
116121 let mut by_instr: Vec < _ > = self
117122 . by_instr
118123 . iter ( )
@@ -168,11 +173,23 @@ impl BcProfile {
168173 }
169174 }
170175
171- pub ( crate ) fn gen_csv ( & self ) -> anyhow:: Result < String > {
172- match & self . data {
173- BcProfileDataMode :: Bc ( data) => Ok ( data. gen_csv ( ) ) ,
174- BcProfileDataMode :: BcPairs ( data) => Ok ( data. gen_csv ( ) ) ,
175- BcProfileDataMode :: Disabled => Err ( EvaluatorError :: BcProfilingNotEnabled . into ( ) ) ,
176+ pub ( crate ) fn gen_bc_profile ( & mut self ) -> anyhow:: Result < ProfileData > {
177+ match mem:: replace ( & mut self . data , BcProfileDataMode :: Disabled ) {
178+ BcProfileDataMode :: Bc ( bc) => Ok ( ProfileData {
179+ profile_mode : ProfileMode :: Bytecode ,
180+ profile : ProfileDataImpl :: Bc ( bc) ,
181+ } ) ,
182+ _ => Err ( EvaluatorError :: BcProfilingNotEnabled . into ( ) ) ,
183+ }
184+ }
185+
186+ pub ( crate ) fn gen_bc_pairs_profile ( & mut self ) -> anyhow:: Result < ProfileData > {
187+ match mem:: replace ( & mut self . data , BcProfileDataMode :: Disabled ) {
188+ BcProfileDataMode :: BcPairs ( bc_pairs) => Ok ( ProfileData {
189+ profile_mode : ProfileMode :: BytecodePairs ,
190+ profile : ProfileDataImpl :: BcPairs ( * bc_pairs) ,
191+ } ) ,
192+ _ => Err ( EvaluatorError :: BcProfilingNotEnabled . into ( ) ) ,
176193 }
177194 }
178195
@@ -209,7 +226,7 @@ mod tests {
209226 & globals,
210227 )
211228 . unwrap ( ) ;
212- let csv = eval. bc_profile . gen_csv ( ) . unwrap ( ) ;
229+ let csv = eval. bc_profile . gen_bc_profile ( ) . unwrap ( ) . gen ( ) . unwrap ( ) ;
213230 assert ! (
214231 csv. contains( & format!( "\n \" {:?}\" ,1," , BcOpcode :: CallFrozenNativePos ) ) ,
215232 "{:?}" ,
@@ -228,7 +245,12 @@ mod tests {
228245 & globals,
229246 )
230247 . unwrap ( ) ;
231- let csv = eval. bc_profile . gen_csv ( ) . unwrap ( ) ;
248+ let csv = eval
249+ . bc_profile
250+ . gen_bc_pairs_profile ( )
251+ . unwrap ( )
252+ . gen ( )
253+ . unwrap ( ) ;
232254 assert ! (
233255 csv. contains( & format!(
234256 "\n \" {:?}\" ,\" {:?}\" ,1" ,
0 commit comments