1- using BenchmarkDotNet . Extensions ;
1+ using BenchmarkDotNet . Extensions ;
22using BenchmarkDotNet . Running ;
33using System . Reflection ;
44using System . Runtime . CompilerServices ;
55
66namespace BenchmarkDotNet . Toolchains . InProcess . NoEmit ;
77
8- internal static partial class BenchmarkActionFactory
8+ internal static class BenchmarkActionFactory
99{
10- /// <summary>
11- /// Dispatch method that creates <see cref="BenchmarkAction"/> using
12- /// <paramref name="targetMethod"/> or <paramref name="fallbackIdleSignature"/> to find correct implementation.
13- /// Either <paramref name="targetMethod"/> or <paramref name="fallbackIdleSignature"/> should be not <c>null</c>.
14- /// </summary>
15- private static BenchmarkAction CreateCore ( object instance , MethodInfo ? targetMethod , MethodInfo ? fallbackIdleSignature , int unrollFactor )
10+ private static IBenchmarkAction CreateCore ( IBenchmarkActionFactory ? factory , object instance , MethodInfo targetMethod , int unrollFactor )
1611 {
17- PrepareInstanceAndResultType ( instance , targetMethod , fallbackIdleSignature , out var resultInstance , out var resultType ) ;
12+ if ( factory ? . TryCreate ( instance , targetMethod , unrollFactor , out var benchmarkAction ) == true )
13+ {
14+ return benchmarkAction ;
15+ }
16+
17+ PrepareInstanceAndResultType ( instance , targetMethod , out var resultInstance , out var resultType ) ;
1818
1919 if ( resultType == typeof ( void ) )
2020 return new BenchmarkActionVoid ( resultInstance , targetMethod , unrollFactor ) ;
2121
22- // targetMethod must not be null here. Because it's checked by PrepareInstanceAndResultType.
23- // Following null check is added to suppress nullable annotation errors.
24- ArgumentNullException . ThrowIfNull ( targetMethod ) ;
25-
2622 if ( resultType == typeof ( void * ) )
2723 return new BenchmarkActionVoidPointer ( resultInstance , targetMethod , unrollFactor ) ;
2824
@@ -71,7 +67,7 @@ private static BenchmarkAction CreateCore(object instance, MethodInfo? targetMet
7167
7268 if ( resultType . IsAwaitable ( ) )
7369 {
74- throw new NotSupportedException ( $ "{ nameof ( InProcessNoEmitToolchain ) } does not support returning awaitable types except (Value)Task(<T>).") ;
70+ throw new NotSupportedException ( $ "Default { nameof ( BenchmarkActionFactory ) } does not support returning awaitable types except (Value)Task(<T>).") ;
7571 }
7672
7773 return Create (
@@ -81,21 +77,10 @@ private static BenchmarkAction CreateCore(object instance, MethodInfo? targetMet
8177 unrollFactor ) ;
8278 }
8379
84- private static void PrepareInstanceAndResultType ( object instance , MethodInfo ? targetMethod , MethodInfo ? fallbackIdleSignature , out object ? resultInstance , out Type resultType )
80+ private static void PrepareInstanceAndResultType ( object instance , MethodInfo targetMethod , out object ? resultInstance , out Type resultType )
8581 {
86- var signature = targetMethod ?? fallbackIdleSignature ;
87- if ( signature == null )
88- throw new ArgumentNullException (
89- nameof ( fallbackIdleSignature ) ,
90- $ "Either { nameof ( targetMethod ) } or { nameof ( fallbackIdleSignature ) } should be not null.") ;
91-
92- if ( ! signature . IsStatic && instance == null )
93- throw new ArgumentNullException (
94- nameof ( instance ) ,
95- $ "The { nameof ( instance ) } parameter should be not null as invocation method is instance method.") ;
96-
97- resultInstance = signature . IsStatic ? null : instance ;
98- resultType = signature . ReturnType ;
82+ resultInstance = targetMethod . IsStatic ? null : instance ;
83+ resultType = targetMethod . ReturnType ;
9984
10085 if ( resultType == typeof ( void ) )
10186 {
@@ -107,32 +92,31 @@ private static void PrepareInstanceAndResultType(object instance, MethodInfo? ta
10792 }
10893 else if ( resultType . IsPointer && resultType != typeof ( void * ) )
10994 {
110- throw new NotSupportedException ( "InProcessNoEmitToolchain only supports void* return, not T*") ;
95+ throw new NotSupportedException ( $ "Default { nameof ( BenchmarkActionFactory ) } only supports void* return, not T*") ;
11196 }
11297 }
11398
11499 /// <summary>Helper to enforce .ctor signature.</summary>
115100 private static BenchmarkActionBase Create ( Type actionType , object ? instance , MethodInfo method , int unrollFactor ) =>
116101 ( BenchmarkActionBase ) Activator . CreateInstance ( actionType , instance , method , unrollFactor ) ! ;
117102
118- private static void FallbackMethod ( ) { }
119- private static readonly MethodInfo FallbackSignature = new Action ( FallbackMethod ) . GetMethodInfo ( ) ;
103+ private static readonly MethodInfo FallbackSignature = new Action ( BenchmarkActionBase . OverheadStatic ) . GetMethodInfo ( ) ;
120104
121- public static BenchmarkAction CreateWorkload ( Descriptor descriptor , object instance , int unrollFactor ) =>
122- CreateCore ( instance , descriptor . WorkloadMethod , null , unrollFactor ) ;
105+ public static IBenchmarkAction CreateWorkload ( IBenchmarkActionFactory ? factory , Descriptor descriptor , object instance , int unrollFactor ) =>
106+ CreateCore ( factory , instance , descriptor . WorkloadMethod , unrollFactor ) ;
123107
124- public static BenchmarkAction CreateOverhead ( Descriptor descriptor , object instance , int unrollFactor ) =>
125- CreateCore ( instance , null , FallbackSignature , unrollFactor ) ;
108+ public static IBenchmarkAction CreateOverhead ( IBenchmarkActionFactory ? factory , Descriptor descriptor , object instance , int unrollFactor ) =>
109+ CreateCore ( factory , instance , FallbackSignature , unrollFactor ) ;
126110
127- public static BenchmarkAction CreateGlobalSetup ( Descriptor descriptor , object instance ) =>
128- CreateCore ( instance , descriptor . GlobalSetupMethod , FallbackSignature , 1 ) ;
111+ public static IBenchmarkAction CreateGlobalSetup ( IBenchmarkActionFactory ? factory , Descriptor descriptor , object instance ) =>
112+ CreateCore ( factory , instance , descriptor . GlobalSetupMethod ?? FallbackSignature , 1 ) ;
129113
130- public static BenchmarkAction CreateGlobalCleanup ( Descriptor descriptor , object instance ) =>
131- CreateCore ( instance , descriptor . GlobalCleanupMethod , FallbackSignature , 1 ) ;
114+ public static IBenchmarkAction CreateGlobalCleanup ( IBenchmarkActionFactory ? factory , Descriptor descriptor , object instance ) =>
115+ CreateCore ( factory , instance , descriptor . GlobalCleanupMethod ?? FallbackSignature , 1 ) ;
132116
133- public static BenchmarkAction CreateIterationSetup ( Descriptor descriptor , object instance ) =>
134- CreateCore ( instance , descriptor . IterationSetupMethod , FallbackSignature , 1 ) ;
117+ public static IBenchmarkAction CreateIterationSetup ( IBenchmarkActionFactory ? factory , Descriptor descriptor , object instance ) =>
118+ CreateCore ( factory , instance , descriptor . IterationSetupMethod ?? FallbackSignature , 1 ) ;
135119
136- public static BenchmarkAction CreateIterationCleanup ( Descriptor descriptor , object instance ) =>
137- CreateCore ( instance , descriptor . IterationCleanupMethod , FallbackSignature , 1 ) ;
120+ public static IBenchmarkAction CreateIterationCleanup ( IBenchmarkActionFactory ? factory , Descriptor descriptor , object instance ) =>
121+ CreateCore ( factory , instance , descriptor . IterationCleanupMethod ?? FallbackSignature , 1 ) ;
138122}
0 commit comments