显式空检查与空合并运算符的编译器评估?
考虑以下代码,它使用两种稍微不同的方法来检查 _instance
并在尚未设置时对其进行分配。
class InstantiationTest
{
private Object _instance;
public void Method1() {
if(_instance == null) {
_instance = new Object();
}
}
public void Method2() {
_instance = _instance ?? new Object();
}
}
VS 或 Resharper 不断强调我的显式空检查,并提示我使用空合并运算符进行重构。
我想知道编译器是否足够聪明来检测 Method2()
中的情况,其中 _instance
被重新分配给自身(实际上是 nop?) 并将 Method2()
重写为方法1()
。
我发现实际情况并非如此:
Test.Method1:
IL_0000: ldarg.0
IL_0001: ldfld UserQuery+Test._instance
IL_0006: brtrue.s IL_0013
IL_0008: ldarg.0
IL_0009: newobj System.Object..ctor
IL_000E: stfld UserQuery+Test._instance
IL_0013: ret
与:
Test.Method2:
IL_0000: ldarg.0
IL_0001: ldarg.0
IL_0002: ldfld UserQuery+Test._instance
IL_0007: dup
IL_0008: brtrue.s IL_0010
IL_000A: pop
IL_000B: newobj System.Object..ctor
IL_0010: stfld UserQuery+Test._instance
IL_0015: ret
我的问题是为什么?
在编译器级别实现是否很棘手,是否太微不足道,不值得付出努力,或者我缺少什么?
Consider the following code, which uses two slightly different methods to check _instance
and assign it when not already set.
class InstantiationTest
{
private Object _instance;
public void Method1() {
if(_instance == null) {
_instance = new Object();
}
}
public void Method2() {
_instance = _instance ?? new Object();
}
}
Either VS or Resharper keeps underlining my explicit null checks, and prompting me to refactor using the null-coalescing operator.
I wondered whether the compiler is smart enough to detect the case in Method2()
where _instance
is reassigned to itself (effectively a nop?) and rewrite Method2()
into Method1()
.
I see this is not actually the case:
Test.Method1:
IL_0000: ldarg.0
IL_0001: ldfld UserQuery+Test._instance
IL_0006: brtrue.s IL_0013
IL_0008: ldarg.0
IL_0009: newobj System.Object..ctor
IL_000E: stfld UserQuery+Test._instance
IL_0013: ret
versus:
Test.Method2:
IL_0000: ldarg.0
IL_0001: ldarg.0
IL_0002: ldfld UserQuery+Test._instance
IL_0007: dup
IL_0008: brtrue.s IL_0010
IL_000A: pop
IL_000B: newobj System.Object..ctor
IL_0010: stfld UserQuery+Test._instance
IL_0015: ret
My question is why?
Is it tricky to implement at the compiler level, too trivial to be worth the effort of implementation, or something I'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般来说,C# 编译器很少对 IL 进行优化,而将其留给 JIT,JIT 可以更好地针对特定体系结构进行优化。所以它根本没有在编译器中实现,因为这会占用其他事情的时间。
Generally, the C# compiler does very little optimizing of the IL, leaving that up to the JIT, which optimizes things much better for a specific architecture. So it's simply not been implemented within the compiler, as that would take time away from other things.