我在尝试理解.NET Core 2.1中的性能问题。此处可以找到相关代码:
https://github.com/mike-eee/StructureActivation
这是相关的基准测试代码,使用BenchmarkDotNet进行测试:public class Program
{
static void Main()
{
BenchmarkRunner.Run<Program>();
}
[Benchmark(Baseline = true)]
public uint? Activated() => new Structure(100).SomeValue;
[Benchmark]
public uint? ActivatedAssignment()
{
var selection = new Structure(100);
return selection.SomeValue;
}
}
public readonly struct Structure
{
public Structure(uint? someValue) => SomeValue = someValue;
public uint? SomeValue { get; }
}
从一开始,我预计Activated
会更快,因为它不存储本地变量,而我一直认为在当前堆栈上下文中定位和保留空间会导致性能损失。
然而,在运行测试时,我得到了以下结果:
// * Summary *
BenchmarkDotNet=v0.11.1, OS=Windows 10.0.17134.285 (1803/April2018Update/Redstone4)
Intel Core i7-4820K CPU 3.70GHz (Haswell), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=2.1.402
[Host] : .NET Core 2.1.4 (CoreCLR 4.6.26814.03, CoreFX 4.6.26814.02), 64bit RyuJIT
DefaultJob : .NET Core 2.1.4 (CoreCLR 4.6.26814.03, CoreFX 4.6.26814.02), 64bit RyuJIT
Method | Mean | Error | StdDev | Scaled |
-------------------- |---------:|----------:|----------:|-------:|
Activated | 4.700 ns | 0.0128 ns | 0.0107 ns | 1.00 |
ActivatedAssignment | 3.331 ns | 0.0278 ns | 0.0260 ns | 0.71 |
激活结构(不存储本地变量)大约比原来慢30%。
参考ReSharper的IL Viewer中的IL:
.method /*06000002*/ public hidebysig instance valuetype [System.Runtime/*23000001*/]System.Nullable`1/*0100000E*/<unsigned int32>
Activated() cil managed
{
.custom /*0C00000C*/ instance void [BenchmarkDotNet/*23000002*/]BenchmarkDotNet.Attributes.BenchmarkAttribute/*0100000D*/::.ctor()
= (01 00 01 00 54 02 08 42 61 73 65 6c 69 6e 65 01 ) // ....T..Baseline.
// property bool 'Baseline' = bool(true)
.maxstack 1
.locals /*11000001*/ init (
[0] valuetype StructureActivation.Structure/*02000003*/ V_0
)
// [14 31 - 14 59]
IL_0000: ldc.i4.s 100 // 0x64
IL_0002: newobj instance void valuetype [System.Runtime/*23000001*/]System.Nullable`1/*0100000E*/<unsigned int32>/*1B000001*/::.ctor(!0/*unsigned int32*/)/*0A00000F*/
IL_0007: newobj instance void StructureActivation.Structure/*02000003*/::.ctor(valuetype [System.Runtime/*23000001*/]System.Nullable`1/*0100000E*/<unsigned int32>)/*06000005*/
IL_000c: stloc.0 // V_0
IL_000d: ldloca.s V_0
IL_000f: call instance valuetype [System.Runtime/*23000001*/]System.Nullable`1/*0100000E*/<unsigned int32> StructureActivation.Structure/*02000003*/::get_SomeValue()/*06000006*/
IL_0014: ret
} // end of method Program::Activated
.method /*06000003*/ public hidebysig instance valuetype [System.Runtime/*23000001*/]System.Nullable`1/*0100000E*/<unsigned int32>
ActivatedAssignment() cil managed
{
.custom /*0C00000D*/ instance void [BenchmarkDotNet/*23000002*/]BenchmarkDotNet.Attributes.BenchmarkAttribute/*0100000D*/::.ctor()
= (01 00 00 00 )
.maxstack 2
.locals /*11000001*/ init (
[0] valuetype StructureActivation.Structure/*02000003*/ selection
)
// [19 4 - 19 39]
IL_0000: ldloca.s selection
IL_0002: ldc.i4.s 100 // 0x64
IL_0004: newobj instance void valuetype [System.Runtime/*23000001*/]System.Nullable`1/*0100000E*/<unsigned int32>/*1B000001*/::.ctor(!0/*unsigned int32*/)/*0A00000F*/
IL_0009: call instance void StructureActivation.Structure/*02000003*/::.ctor(valuetype [System.Runtime/*23000001*/]System.Nullable`1/*0100000E*/<unsigned int32>)/*06000005*/
// [20 4 - 20 31]
IL_000e: ldloca.s selection
IL_0010: call instance valuetype [System.Runtime/*23000001*/]System.Nullable`1/*0100000E*/<unsigned int32> StructureActivation.Structure/*02000003*/::get_SomeValue()/*06000006*/
IL_0015: ret
} // end of method Program::ActivatedAssignment
经过检查,Activated
有两个newobj
,而ActivatedAssignment
只有一个,这可能是导致两个基准测试之间差异的原因。
我的问题是:这是否符合预期?我试图理解为什么代码较少的基准测试实际上比代码更多的基准测试慢。如有任何指导/建议以确保我遵循最佳实践,将不胜感激。
uint?
(又称Nullable<uint>
)很难看,因为它的HasValue
字段需要被赋值,优化器因为无法推理出可能的副作用而束手无策。非常重要的是,您还应该使用普通的uint
进行比较,这会让您对在性能关键代码中使用可空类型三思而后行。允许将该方法内联,将其置于for
循环中。 - Hans Passant