首页 文章

CLR 中的 castclass 操作码的目的是什么?

提问于
浏览
3

我遇到了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 回答

  • 3

    所有callvirt instance string IShow::show指令都调用相同的存根,跳转到与接口方法关联的查找存根。查找存根将解析要调用的方法,具体取决于接收调用的对象的类型。在这种情况下,对象确实实现IShow,所以一切正常,你可以看到。但是,如果传递的对象未实现IShow,则查找存根将不会在对象的方法表中找到IShow::show,因此会抛出类型EntryPointNotFoundException的异常。

    IL 虚拟机的评估堆栈在执行callvirt指令时包含object类型的对象。目标方法是IShow::show()。根据 CLI 规范 Section III.4.2,类型object必须是 verifier-assignable-to IShow才能使 IL 代码可以验证。 castclass使代码可验证。在这种情况下,由于代码完全受信任,因此会自动跳过验证,因此不会抛出验证异常,并且方法会被 JIT 编译和执行。

    从技术上讲,在这种情况下,showWithoutCast不包含任何应该根据规范引发类型EntryPointNotFoundException异常的 IL 指令。但是,由于代码不可验证,标准的第 II.3 节声明在验证失败的情况下未指定行为。也就是说,不需要实现来记录发生的行为。另一方面,指定了可验证代码的行为,castclass使验证成功。

    请注意,当您在计算机上本地构建 IL 代码并运行它时,它将自动被视为完全受信任。所以 JIT 编译器不会验证任何方法。

相关问题