在创建一个尽可能快的小型示踪剂类时,我正在查看生成的汇编代码。这个想法是创建包装器类,该类将跟踪方法在离开时进入,而当方法离开时则跟踪方法离开。这可以通过使用语句来实现
public void DoStruct()
{
using var tmp = Wrapper.Create(nameof(DoStruct));
}
这将创建一个结构跟踪器实例,该实例将自动放置在方法离开处。到现在为止还挺好。现在让我们看一下生成程序集:
NetCoreJitStruct.User.DoStruct()
Begin 00007FFC91651860, size 61
00007ffc`91651860 push rbp
00007ffc`91651861 sub rsp,30h
00007ffc`91651865 lea rbp,[rsp+30h]
00007ffc`9165186a xor eax,eax
00007ffc`9165186c mov qword ptr [rbp-8],rax
00007ffc`91651870 mov qword ptr [rbp-10h],rsp
00007ffc`91651874 mov qword ptr [rbp+10h],rcx
00007ffc`91651878 mov rcx,268F07F30D8h
00007ffc`91651882 mov rcx,qword ptr [rcx]
00007ffc`91651885 call 00007ffc`91650800 (NetCoreJitStruct.Wrapper.Create(System.String) *** Ctor called
00007ffc`9165188a mov qword ptr [rbp-8],rax
00007ffc`9165188e jmp 00007ffc`91651890
00007ffc`91651890 mov rcx,rsp
00007ffc`91651893 call 00007ffc`9165189f (NetCoreJitStruct.User.DoClass() *** Dispose Called via extra method Call!
00007ffc`91651898 nop
00007ffc`91651899 lea rsp,[rbp]
00007ffc`9165189d pop rbp
00007ffc`9165189e ret
00007ffc`9165189f push rbp ** Dispose wrapper method
00007ffc`916518a0 sub rsp,30h
00007ffc`916518a4 mov rbp,qword ptr [rcx+20h]
00007ffc`916518a8 mov qword ptr [rsp+20h],rbp
00007ffc`916518ad lea rbp,[rbp+30h]
00007ffc`916518b1 lea rcx,[rbp-8]
00007ffc`916518b5 call 00007ffc`91650818 (NetCoreJitStruct.Wrapper.Dispose()
00007ffc`916518ba nop
00007ffc`916518bb add rsp,30h
00007ffc`916518bf pop rbp
00007ffc`916518c0 ret
我不明白的是为什么JIT编译器将dispose方法调用分解为额外的包装器方法。 .NET 4.8的行为相同,但效率更高一些的代码生成器。 我已经检查了struct方法内联是否存在问题,但是对于已处置的类,其行为是相同的。
这是使用.NET可以获得的最快速度吗?还是想念一些使其变得更快/对JIT友好的模式?
编译器:C#7+ .NET:.NET 4.8或.NET Core 3.1
下面是完整的源代码
使用系统; 使用System.Collections.Generic; 使用System.Diagnostics; 使用System.Runtime.CompilerServices;
命名空间NetCoreJitStruct { 班级计划 {
[MethodImpl(MethodImplOptions.NoInlining)]
static void Main(string[] args)
{
Queue<string> argList = new Queue<string>(args);
bool useStruct = true;
bool nofactory = false;
bool nop = false;
bool direct = false;
while (argList.Count > 0)
{
string arg = argList.Dequeue().ToLower();
switch (arg)
{
case "-trace":
CustomData.IsEnabled = true;
break;
case "-struct":
break;
case "-class":
useStruct = false;
break;
case "-direct":
direct = true;
break;
case "-nop":
nop = true;
break;
case "-nofactory":
nofactory = true;
break;
default:
Console.WriteLine("NtCoreJitStruct [-trace] [-struct or -class]");
return;
}
}
var user = new User();
user.DoClass_Factory();
user.DoStruct_Factory();
user.DoStructTryFinally();
user.DoStructNoFinally_Factory();
user.DoStructNoFinally_NoFactory();
const int Runs = 1500_000_000;
var sw = Stopwatch.StartNew();
if (nop) // measure loop overhead
{
for (int i = 0; i < Runs; i++)
{
}
}
else
{
if (useStruct)
{
if (direct)
{
if (nofactory)
{
for (int i = 0; i < Runs; i++)
{
user.DoStructNoFinally_NoFactory();
}
}
else
{
for (int i = 0; i < Runs; i++)
{
user.DoStructNoFinally_Factory();
}
}
}
else
{
if (nofactory)
{
for (int i = 0; i < Runs; i++)
{
user.DoStruct_NoFactory();
}
}
else
{
for (int i = 0; i < Runs; i++)
{
user.DoStruct_Factory();
}
}
}
}
else
{
for (int i = 0; i < Runs; i++)
{
user.DoClass_Factory();
}
}
}
sw.Stop();
string scenario = useStruct ? "Struct" : "Class";
Console.WriteLine($"Scenario: {scenario} NoFactory: {nofactory} Nop: {nop} Direct: {direct} Did execute {Runs:N0} Trace calls in {sw.Elapsed.TotalMilliseconds:F0} ms");
}
}
class User
{
[MethodImpl(MethodImplOptions.NoInlining)]
public void DoClass_Factory()
{
using var tmp = CWrapper.Create(nameof(DoClass_Factory));
}
[MethodImpl(MethodImplOptions.NoInlining)]
public void DoStruct_Factory()
{
using var tmp = Wrapper.Create(nameof(DoStruct_Factory));
}
[MethodImpl(MethodImplOptions.NoInlining)]
public void DoStructTryFinally()
{
var tmp = Wrapper.Create(nameof(DoStruct_Factory));
try
{
}
finally
{
tmp.Dispose();
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
public void DoStructNoFinally_Factory()
{
var tmp = Wrapper.Create(nameof(DoStruct_Factory));
tmp.Dispose();
}
[MethodImpl(MethodImplOptions.NoInlining)]
public void DoStructNoFinally_NoFactory()
{
var tmp = new Wrapper(nameof(DoStruct_Factory));
tmp.Dispose();
}
[MethodImpl(MethodImplOptions.NoInlining)]
internal void DoStruct_NoFactory()
{
using var tmp = new Wrapper(nameof(DoStruct_NoFactory));
}
}
public struct Wrapper : IDisposable
{
CustomData data_;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Wrapper Create(string input)
{
return CustomData.IsEnabled ? new Wrapper(input) : default;
}
public Wrapper(string a)
{
data_ = CustomData.IsEnabled ? new CustomData(a) : null;
}
public void Dispose()
{
if (data_ != null)
{
data_.Dispose();
}
}
}
public class CWrapper : IDisposable
{
CustomData data_;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static CWrapper Create(string input)
{
return CustomData.IsEnabled ? new CWrapper(input) : default;
}
public CWrapper(string a)
{
data_ = CustomData.IsEnabled ? new CustomData(a) : null;
}
public void Dispose()
{
if (data_ != null)
{
data_.Dispose();
}
}
}
class CustomData : IDisposable
{
public static bool IsEnabled;
string myData;
public CustomData(string data)
{
myData = data;
Console.WriteLine("Entered method {0}", data);
}
public void Dispose()
{
Console.WriteLine("Left method {0}", myData);
}
}
}
一些测试结果:
JitStruct.exe -nop
Scenario: Struct NoFactory: False Nop: True Direct: False Did execute 1,500,000,000 Trace calls in 404 ms
JitStruct.exe -struct
Scenario: Struct NoFactory: False Nop: False Direct: False Did execute 1,500,000,000 Trace calls in 4837 ms
JitStruct.exe -struct
Scenario: Struct NoFactory: False Nop: False Direct: False Did execute 1,500,000,000 Trace calls in 4832 ms
JitStruct.exe -struct -direct
Scenario: Struct NoFactory: False Nop: False Direct: True Did execute 1,500,000,000 Trace calls in 4146 ms
JitStruct.exe -struct -direct
Scenario: Struct NoFactory: False Nop: False Direct: True Did execute 1,500,000,000 Trace calls in 4156 ms
JitStruct.exe -struct -direct -nofactory
Scenario: Struct NoFactory: True Nop: False Direct: True Did execute 1,500,000,000 Trace calls in 6424 ms
JitStruct.exe -struct -direct -nofactory
Scenario: Struct NoFactory: True Nop: False Direct: True Did execute 1,500,000,000 Trace calls in 6389 ms
JitStruct.exe -struct -direct -nofactory
Scenario: Struct NoFactory: True Nop: False Direct: True Did execute 1,500,000,000 Trace calls in 6417 ms
JitStruct.exe -struct -nofactory
Scenario: Struct NoFactory: True Nop: False Direct: False Did execute 1,500,000,000 Trace calls in 6063 ms
答案 0 :(得分:0)
您所看到的是分支的成本,如果删除了if并尝试使用,则该结构然后更改代码并尝试使用该类,您将在运行时看到巨大的差异
您还可以交换if / else,您将大体上得到与当前相反的结果
复制for,一个用于结构,一个用于类,您将得到另一种结果
说明;
for循环
尝试,然后编译,运行
for (int i = 0; i < Runs; i++)
{
user.DoStruct();
}
然后尝试,编译,运行
for (int i = 0; i < Runs; i++)
{
user.DoClass();
}
您将得到不同的结果。
我知道我的答案与标题不符,但是我在回答帖子的第一行。