我遇到了castclass
操作码,它在Standard ECMA - 335, III.4.3
定义。我写了几个使用callvirt
操作码的例子,有铸造而没有。事实证明,castclass
操作码对性能有很大影响。
为了测试,我使用了以下“粗略”(就方法执行的不精确时序而言)程序(在Release mode
中编译msvc 2015
):
public class Program
{
public interface IShow { string show(); }
public class ObjectWithShow : IShow
{
public string show() => "Hello, that's the show method.";
}
// Such functions remains unchanged
public static string showWithCast(object o) => ((IShow)o).show();
public static string show(IShow o) => o.show();
// Such function will be patched later
public static string showWithoutCast(object o) => ((IShow)o).show();
public static void Main()
{
int N = 10000000;
var show_object = new ObjectWithShow();
{
var watch = System.Diagnostics.Stopwatch.StartNew();
for (int i = 0; i < N; ++i)
{
showWithCast(show_object);
}
watch.Stop();
Console.WriteLine($"With cast {watch.ElapsedMilliseconds}");
}
{
var watch = System.Diagnostics.Stopwatch.StartNew();
for (int i = 0; i < N; ++i)
{
showWithoutCast(show_object);
}
watch.Stop();
Console.WriteLine($"Without cast {watch.ElapsedMilliseconds}");
}
{
var watch = System.Diagnostics.Stopwatch.StartNew();
for (int i = 0; i < N; ++i)
{
show(show_object);
}
watch.Stop();
Console.WriteLine($"Without cast {watch.ElapsedMilliseconds}");
}
}
}
这是show/showWitCast
函数的IL
代码:
.method public hidebysig static string show (class IShow o) cil managed
{
.maxstack 8
IL_0000: ldarg.0
IL_0001: callvirt instance string IShow::show()
IL_0006: ret
} // end of method Program::show
.method public hidebysig static string showWithCast (object o) cil managed
{
.maxstack 8
IL_0000: ldarg.0
IL_0001: castclass IShow
IL_0006: callvirt instance string IShow::show()
IL_000b: ret
} // end of method Program::showWithCast
这是showWithoutCast
的代码(注意:我通过在IL
编辑器中删除castclass IShow
来修补它.原始版本与showWithCast
相同)
.method public hidebysig static string showWithoutCast (object o) cil managed
{
.maxstack 8
IL_0000: ldarg.0
IL_0001: callvirt instance string IShow::show()
IL_0006: ret
} // end of method Program::showWithoutCast
执行结果(i7-3370 CPU@3.40GHz,8GB RAM)显示以下结果:
随着演员 46
没有演员 24
没有演员 23
事实证明,没有castclass
的对象上的callvirt
显示与我们使用的接口实例几乎相同的结果。那么castclass
的目的是什么?我猜c# compiler
发出这样的代码来确保callvirt
操作码不会用在不同的类型上(因为它不能在编译时执行这样的检查)。所以,下面的问题 - 它是否是一致的CIL
代码,我故意在地方删除castclass
的使用,其中我保证该方法将仅使用实现IShow
的类型?
P.S。当然,你可以问一下,也许应该使用show
方法吗?但有些情况下,这种功能无法使用。如果简短,我动态生成代码,我想实现通用容器(它继承IShow
),但它的泛型参数可以选择实现接口IShow
。如果泛型参数没有实现接口(例如它是int
),那么我保证不会使用容器的方法show
。
1 回答
所有
callvirt instance string IShow::show
指令都调用相同的存根,跳转到与接口方法关联的查找存根。查找存根将解析要调用的方法,具体取决于接收调用的对象的类型。在这种情况下,对象确实实现IShow
,所以一切正常,你可以看到。但是,如果传递的对象未实现IShow
,则查找存根将不会在对象的方法表中找到IShow::show
,因此会抛出类型EntryPointNotFoundException
的异常。IL 虚拟机的评估堆栈在执行
callvirt
指令时包含object
类型的对象。目标方法是IShow::show()
。根据 CLI 规范 Section III.4.2,类型object
必须是 verifier-assignable-toIShow
才能使 IL 代码可以验证。castclass
使代码可验证。在这种情况下,由于代码完全受信任,因此会自动跳过验证,因此不会抛出验证异常,并且方法会被 JIT 编译和执行。从技术上讲,在这种情况下,
showWithoutCast
不包含任何应该根据规范引发类型EntryPointNotFoundException
异常的 IL 指令。但是,由于代码不可验证,标准的第 II.3 节声明在验证失败的情况下未指定行为。也就是说,不需要实现来记录发生的行为。另一方面,指定了可验证代码的行为,castclass
使验证成功。请注意,当您在计算机上本地构建 IL 代码并运行它时,它将自动被视为完全受信任。所以 JIT 编译器不会验证任何方法。