简体   繁体   English

C#JIT编译器是否优化空检查?

[英]Does C# JIT compiler optimize null-check?

There are many articles online that list the optimizations made by the C# JIT before executing a piece of code. 在线上有许多文章列出了在执行一段代码之前C#JIT所做的优化。 For example this post on MSDN talks about: 例如,有关MSDN的这篇文章讨论了:

Constant folding, Constant and copy propagation, Common subexpression elimination, Code motion of loop invariants, Dead store and dead code elimination, Register allocation, Method inlining, Loop unrolling (small loops with small bodies). 常量折叠,常量和副本传播,公共子表达式消除,循环不变量的代码运动,死存储和死代码消除,寄存器分配,方法内联,循环展开(带有小主体的小循环)。

My question is: does the JIT compiler deals also with useless null-checks? 我的问题是:JIT编译器还会处理无用的null检查吗? I can't find any source treating this question. 我找不到任何来源来处理这个问题。

In the same article I read: 在同一篇文章中,我读到:

since the C# language specification ensures that any call on a null object reference throws a NullReferenceException, every call site must ensure the instance is not null. 由于C#语言规范确保对null对象引用的任何调用均引发NullReferenceException,因此每个调用站点都必须确保实例不为null。 This is done by dereferencing the instance reference; 这是通过取消引用实例引用来完成的。 if it is null it will generate a fault that is turned into this exception. 如果为null,则将生成一个错误,该错误将变为此异常。

So, suppose I write a piece of code like this: 因此,假设我写了一段这样的代码:

if (person != null)
{
    Console.WriteLine(person.Name);
}

The person.Name calls again a second null-check that is cleary useless, and the compiler could remove it. person.Name再次调用第二个空检查,这显然是无用的,编译器可以将其删除。 Or not? 或不?

I read that in Java this is already done (some sources between many here , here and here ). 我读到在Java中这已经完成了( 这里这里这里的许多之间的一些资源)。

If C# does it too, do you know some source or documentation that talks about that? 如果C#也这样做,您是否知道一些有关此的源代码或文档?

If instead C# doesn't do it, do you know why? 相反,如果C#不这样做,您知道为什么吗? Is there an intrinsic difficulty in implementing such a feature for a .NET environment that the Java JIT does not encounter? 在Java JIT不会遇到的.NET环境中实现这种功能是否存在固有的困难?

Null check optimization done by the compiler (Roslyn, not Jitter) in several cases , when its completly save to do that. 在几种情况下 ,由编译器(Roslyn,而不是Jitter)完成Null检查优化,然后将其完全保存即可。

For example when you use ? 例如,当您使用? (Elvis operator). (猫王操作员)。

IL_0006: stloc.0              // Pop a value from stack into local variable 0
IL_0007: ldloc.0              // Load local variable 0 onto stack
IL_0008: brtrue.s IL_000c     // Branch to target if value is non-zero (true), short form
IL_000a: br.s IL_0013         // Branch to target, short form
IL_000c: ldloc.0              // Load local variable 0 onto stack
IL_000d: call instance void Foo::Bar() // Call method indicated on the stack with arguments

Another example is code like this: 另一个示例是这样的代码:

new Bar().Foo();

The compiler generates for this call instruction and not callvirt (which means, no null check on this ) 编译器为该call指令而不是callvirt (这意味着this没有null检查)

In other cases, you can't be sure that this not going to be null. 在其他情况下,您不能确保this不会为null。

Anyway, null check is really really fast. 无论如何,空检查确实非常快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#:如何对动态 object 执行空检查 - C#: How to perform a null-check on a dynamic object C# 编译器或 JIT 在什么级别优化应用程序代码? - At what level C# compiler or JIT optimize the application code? C# 中的空检查和空条件运算符之间是否存在性能差异? - Is there a performance difference between null-check and null-conditional-operator in C#? 是否有C#代码直接在对象引用上调用“ brtrue” /“ brfalse”指令的空检查功能? - Is there C# code to invoke the null-check feature of `brtrue`/`brfalse` instruction directly on an object reference? 用C#的方式将空检查包装在x代码周围 - C# Modular way to wrap a null-check around x code ..Invoke(x)的执行是否与null-check +直接调用一样好 - Does ?.Invoke(x) perform as well as null-check + direct call 默认情况下,C#编译器是否优化程序? - Does a C# compiler optimize programs by default? C#编译器是否优化空语句 - Does C# compiler optimize empty statements C#编译器或JIT可以优化lambda表达式中的方法调用吗? - Can the C# compiler or JIT optimize away a method call in a lambda expression? 有没有办法让.Net JIT或C#编译器优化掉空的for循环? - Is there a way to get the .Net JIT or C# compiler to optimize away empty for-loops?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM