@@ -262,7 +262,7 @@ pub struct InspectorData {
262
262
pub traces : Option < SparsedTraceArena > ,
263
263
pub line_coverage : Option < HitMaps > ,
264
264
pub edge_coverage : Option < Vec < u8 > > ,
265
- pub cheatcodes : Option < Cheatcodes > ,
265
+ pub cheatcodes : Option < Box < Cheatcodes > > ,
266
266
pub chisel_state : Option < ( Vec < U256 > , Vec < u8 > , Option < InstructionResult > ) > ,
267
267
pub reverter : Option < Address > ,
268
268
}
@@ -290,7 +290,7 @@ pub struct InnerContextData {
290
290
/// collection, etc.
291
291
#[ derive( Clone , Debug , Default ) ]
292
292
pub struct InspectorStack {
293
- pub cheatcodes : Option < Cheatcodes > ,
293
+ pub cheatcodes : Option < Box < Cheatcodes > > ,
294
294
pub inner : InspectorStackInner ,
295
295
}
296
296
@@ -300,15 +300,17 @@ pub struct InspectorStack {
300
300
#[ derive( Default , Clone , Debug ) ]
301
301
pub struct InspectorStackInner {
302
302
// Inspectors.
303
- pub chisel_state : Option < ChiselState > ,
304
- pub edge_coverage : Option < EdgeCovInspector > ,
305
- pub fuzzer : Option < Fuzzer > ,
306
- pub line_coverage : Option < LineCoverageCollector > ,
307
- pub log_collector : Option < LogCollector > ,
308
- pub printer : Option < CustomPrintTracer > ,
309
- pub revert_diag : Option < RevertDiagnostic > ,
310
- pub script_execution_inspector : Option < ScriptExecutionInspector > ,
311
- pub tracer : Option < TracingInspector > ,
303
+ // These are boxed to reduce the size of the struct and slightly improve performance of the
304
+ // `if let Some` checks.
305
+ pub chisel_state : Option < Box < ChiselState > > ,
306
+ pub edge_coverage : Option < Box < EdgeCovInspector > > ,
307
+ pub fuzzer : Option < Box < Fuzzer > > ,
308
+ pub line_coverage : Option < Box < LineCoverageCollector > > ,
309
+ pub log_collector : Option < Box < LogCollector > > ,
310
+ pub printer : Option < Box < CustomPrintTracer > > ,
311
+ pub revert_diag : Option < Box < RevertDiagnostic > > ,
312
+ pub script_execution_inspector : Option < Box < ScriptExecutionInspector > > ,
313
+ pub tracer : Option < Box < TracingInspector > > ,
312
314
313
315
// InspectorExt and other internal data.
314
316
pub enable_isolation : bool ,
@@ -335,7 +337,7 @@ impl CheatcodesExecutor for InspectorStackInner {
335
337
Box :: new ( InspectorStackRefMut { cheatcodes : Some ( cheats) , inner : self } )
336
338
}
337
339
338
- fn tracing_inspector ( & mut self ) -> Option < & mut Option < TracingInspector > > {
340
+ fn tracing_inspector ( & mut self ) -> Option < & mut Option < Box < TracingInspector > > > {
339
341
Some ( & mut self . tracer )
340
342
}
341
343
}
@@ -398,19 +400,19 @@ impl InspectorStack {
398
400
/// Set the cheatcodes inspector.
399
401
#[ inline]
400
402
pub fn set_cheatcodes ( & mut self , cheatcodes : Cheatcodes ) {
401
- self . cheatcodes = Some ( cheatcodes) ;
403
+ self . cheatcodes = Some ( cheatcodes. into ( ) ) ;
402
404
}
403
405
404
406
/// Set the fuzzer inspector.
405
407
#[ inline]
406
408
pub fn set_fuzzer ( & mut self , fuzzer : Fuzzer ) {
407
- self . fuzzer = Some ( fuzzer) ;
409
+ self . fuzzer = Some ( fuzzer. into ( ) ) ;
408
410
}
409
411
410
412
/// Set the Chisel inspector.
411
413
#[ inline]
412
414
pub fn set_chisel ( & mut self , final_pc : usize ) {
413
- self . chisel_state = Some ( ChiselState :: new ( final_pc) ) ;
415
+ self . chisel_state = Some ( ChiselState :: new ( final_pc) . into ( ) ) ;
414
416
}
415
417
416
418
/// Set whether to enable the line coverage collector.
@@ -422,7 +424,8 @@ impl InspectorStack {
422
424
/// Set whether to enable the edge coverage collector.
423
425
#[ inline]
424
426
pub fn collect_edge_coverage ( & mut self , yes : bool ) {
425
- self . edge_coverage = yes. then ( EdgeCovInspector :: new) ; // TODO configurable edge size?
427
+ // TODO: configurable edge size?
428
+ self . edge_coverage = yes. then ( EdgeCovInspector :: new) . map ( Into :: into) ;
426
429
}
427
430
428
431
/// Set whether to enable call isolation.
@@ -459,11 +462,7 @@ impl InspectorStack {
459
462
/// Revert diagnostic inspector is activated when `mode != TraceMode::None`
460
463
#[ inline]
461
464
pub fn tracing ( & mut self , mode : TraceMode ) {
462
- if mode. is_none ( ) {
463
- self . revert_diag = None ;
464
- } else {
465
- self . revert_diag = Some ( RevertDiagnostic :: default ( ) ) ;
466
- }
465
+ self . revert_diag = ( !mode. is_none ( ) ) . then ( RevertDiagnostic :: default) . map ( Into :: into) ;
467
466
468
467
if let Some ( config) = mode. into_config ( ) {
469
468
* self . tracer . get_or_insert_with ( Default :: default) . config_mut ( ) = config;
@@ -480,7 +479,6 @@ impl InspectorStack {
480
479
}
481
480
482
481
/// Collects all the data gathered during inspection into a single struct.
483
- #[ inline]
484
482
pub fn collect ( self ) -> InspectorData {
485
483
let Self {
486
484
mut cheatcodes,
@@ -531,7 +529,7 @@ impl InspectorStack {
531
529
532
530
#[ inline( always) ]
533
531
fn as_mut ( & mut self ) -> InspectorStackRefMut < ' _ > {
534
- InspectorStackRefMut { cheatcodes : self . cheatcodes . as_mut ( ) , inner : & mut self . inner }
532
+ InspectorStackRefMut { cheatcodes : self . cheatcodes . as_deref_mut ( ) , inner : & mut self . inner }
535
533
}
536
534
}
537
535
@@ -740,17 +738,16 @@ impl InspectorStackRefMut<'_> {
740
738
/// it.
741
739
fn with_stack < O > ( & mut self , f : impl FnOnce ( & mut InspectorStack ) -> O ) -> O {
742
740
let mut stack = InspectorStack {
743
- cheatcodes : self
744
- . cheatcodes
745
- . as_deref_mut ( )
746
- . map ( |cheats| core:: mem:: replace ( cheats, Cheatcodes :: new ( cheats. config . clone ( ) ) ) ) ,
741
+ cheatcodes : self . cheatcodes . as_deref_mut ( ) . map ( |cheats| {
742
+ core:: mem:: replace ( cheats, Cheatcodes :: new ( cheats. config . clone ( ) ) ) . into ( )
743
+ } ) ,
747
744
inner : std:: mem:: take ( self . inner ) ,
748
745
} ;
749
746
750
747
let out = f ( & mut stack) ;
751
748
752
749
if let Some ( cheats) = self . cheatcodes . as_deref_mut ( ) {
753
- * cheats = stack. cheatcodes . take ( ) . unwrap ( ) ;
750
+ * cheats = * stack. cheatcodes . take ( ) . unwrap ( ) ;
754
751
}
755
752
756
753
* self . inner = stack. inner ;
@@ -791,6 +788,11 @@ impl InspectorStackRefMut<'_> {
791
788
}
792
789
}
793
790
791
+ // We take extra care in optimizing `step` and `step_end`, as they're are likely the most
792
+ // hot functions in all of Foundry.
793
+ // We want to `#[inline(always)]` these functions so that `InspectorStack` does not
794
+ // delegate to `InspectorStackRefMut` in this case.
795
+
794
796
#[ inline( always) ]
795
797
fn step_inlined (
796
798
& mut self ,
@@ -799,17 +801,18 @@ impl InspectorStackRefMut<'_> {
799
801
) {
800
802
call_inspectors ! (
801
803
[
804
+ // These are sorted in definition order.
805
+ & mut self . edge_coverage,
802
806
& mut self . fuzzer,
803
- & mut self . tracer,
804
807
& mut self . line_coverage,
805
- & mut self . edge_coverage,
806
- & mut self . script_execution_inspector,
807
808
& mut self . printer,
808
809
& mut self . revert_diag,
810
+ & mut self . script_execution_inspector,
811
+ & mut self . tracer,
809
812
// Keep `cheatcodes` last to make use of the tail call.
810
813
& mut self . cheatcodes,
811
814
] ,
812
- |inspector| ( * inspector) . step( interpreter, ecx) ,
815
+ |inspector| ( * * inspector) . step( interpreter, ecx) ,
813
816
) ;
814
817
}
815
818
@@ -821,14 +824,15 @@ impl InspectorStackRefMut<'_> {
821
824
) {
822
825
call_inspectors ! (
823
826
[
824
- & mut self . tracer ,
827
+ // These are sorted in definition order.
825
828
& mut self . chisel_state,
826
829
& mut self . printer,
827
830
& mut self . revert_diag,
831
+ & mut self . tracer,
828
832
// Keep `cheatcodes` last to make use of the tail call.
829
833
& mut self . cheatcodes,
830
834
] ,
831
- |inspector| ( * inspector) . step_end( interpreter, ecx) ,
835
+ |inspector| ( * * inspector) . step_end( interpreter, ecx) ,
832
836
) ;
833
837
}
834
838
}
0 commit comments