gpt4 book ai didi

c# - 为什么为泛型发出 'box' 指令?

转载 作者:可可西里 更新时间:2023-11-01 08:54:02 25 4
gpt4 key购买 nike

这是一个相当简单的泛型类。泛型参数被限制为引用类型。 IRepositoryDbSet 也包含相同的约束。

public class Repository<TEntity> : IRepository<TEntity>
where TEntity : class, IEntity
{
protected readonly DbSet<TEntity> _dbSet;
public void Insert(TEntity entity)
{
if (entity == null)
throw new ArgumentNullException("entity", "Cannot add null entity.");
_dbSet.Add(entity);
}
}

编译后的 IL 包含 box 指令。这是发布版本(调试版本也包含它)。

.method public hidebysig newslot virtual final 
instance void Insert(!TEntity entity) cil managed
{
// Code size 38 (0x26)
.maxstack 8
IL_0000: ldarg.1
>>>IL_0001: box !TEntity
IL_0006: brtrue.s IL_0018
IL_0008: ldstr "entity"
IL_000d: ldstr "Cannot add null entity."
IL_0012: newobj instance void [mscorlib]System.ArgumentNullException::.ctor(string,
string)
IL_0017: throw
IL_0018: ldarg.0
IL_0019: ldfld class [EntityFramework]System.Data.Entity.DbSet`1<!0> class Repository`1<!TEntity>::_dbSet
IL_001e: ldarg.1
IL_001f: callvirt instance !0 class [EntityFramework]System.Data.Entity.DbSet`1<!TEntity>::Add(!0)
IL_0024: pop
IL_0025: ret
} // end of method Repository`1::Insert

更新:

使用 object.Equals(entity, default(TEntity)) 看起来更糟:

  .maxstack  2
.locals init ([0] !TEntity CS$0$0000)
IL_0000: ldarg.1
>>>IL_0001: box !TEntity
IL_0006: ldloca.s CS$0$0000
IL_0008: initobj !TEntity
IL_000e: ldloc.0
>>>IL_000f: box !TEntity
IL_0014: call bool [mscorlib]System.Object::Equals(object,
object)
IL_0019: brfalse.s IL_002b

更新 2:

有兴趣的可以看看调试器中jit编译后的代码:

0cd5af28 55              push    ebp
0cd5af29 8bec mov ebp,esp
0cd5af2b 83ec18 sub esp,18h
0cd5af2e 33c0 xor eax,eax
0cd5af30 8945f0 mov dword ptr [ebp-10h],eax
0cd5af33 8945ec mov dword ptr [ebp-14h],eax
0cd5af36 8945e8 mov dword ptr [ebp-18h],eax
0cd5af39 894df8 mov dword ptr [ebp-8],ecx
//entity reference to [ebp-0Ch]
0cd5af3c 8955f4 mov dword ptr [ebp-0Ch],edx
//some debugger checks
0cd5af3f 833d9424760300 cmp dword ptr ds:[3762494h],0
0cd5af46 7405 je 0cd5af4d Branch
0cd5af48 e8e1cac25a call clr!JIT_DbgIsJustMyCode (67987a2e)
0cd5af4d c745fc00000000 mov dword ptr [ebp-4],0
0cd5af54 90 nop

//comparison or entity ref with zero
0cd5af55 837df400 cmp dword ptr [ebp-0Ch],0
0cd5af59 0f95c0 setne al
0cd5af5c 0fb6c0 movzx eax,al
0cd5af5f 8945fc mov dword ptr [ebp-4],eax
0cd5af62 837dfc00 cmp dword ptr [ebp-4],0
//if not zero, jump further
0cd5af66 7542 jne 0cd5afaa Branch
//throwing exception here

这个问题的原因其实是NDepend warns about using boxing/unboxing。我很好奇为什么它会在一些泛型类中发现装箱,现在很清楚了。

最佳答案

在查看生成 BOX 指令的 C# 编译器源代码时,我遇到了一条非常相关的评论。 fncbind.cpp 源文件有这个注释,与这个特定代码没有其他直接关系:

// NOTE: for the flags, we have to use EXF_FORCE_UNBOX (not EXF_REFCHECK) even when
// we know that the type is a reference type. The verifier expects all code for
// type parameters to behave as if the type parameter is a value type.
// The jitter should be smart about it....

所以它在那里是因为验证者需要它。

是的,抖动很聪明。它根本不为 BOX 指令发出任何代码。

关于c# - 为什么为泛型发出 'box' 指令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20683715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com