C# 編譯器對局部變量的優化

C# 編譯器對局部變量的優化

C# 的編譯器可以對代碼進行優化,所以,我們在寫代碼的時候,可以更多地考慮一下代碼的易讀性問題。

不考慮基本的對齊和換行美化。看一下局部變量優化問題。

C# 示例代碼

例如,我們有一段如下的代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        { 
            var s = DoSomething();
            Console.WriteLine(s);
        }

        static string DoSomething()
        {
            var s1 = "Hello, world.";
            var s2 = s1.ToUpper();
            return s2;
        }
    }
}

在 DoSomething() 這個方法中,裏面定義了兩個局部變量:

  • s1
  • s2

在 Main() 方法中,定義了一個局部變量:

  • s

定義 s1 和 s2 是為了提高代碼的可讀性,它們會導致生成冗餘的代碼,降低執行效率嗎?

我們分別在 Debug 模式下和 Release 模式下進行編譯,使用 ILDasm 查看生成的中間代碼。

Debug 模式下生成的中間代碼

在 Debug 下編譯之後,DoSomething() 生成的中間代碼如下,可以看到實際上有 3 個局部變量。除了我們自己定義的 s1 和 s2 之外,還有一個生成的 V_2,代碼的尺寸為 20。

.method private hidebysig static string  DoSomething() cil managed
{
  // Code size       20 (0x14)
  .maxstack  1
  .locals init ([0] string s1,
           [1] string s2,
           [2] string V_2)
  IL_0000:  nop
  IL_0001:  ldstr      "Hello, world."
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  callvirt   instance string [mscorlib]System.String::ToUpper()
  IL_000d:  stloc.1
  IL_000e:  ldloc.1
  IL_000f:  stloc.2
  IL_0010:  br.s       IL_0012
  IL_0012:  ldloc.2
  IL_0013:  ret
} // end of method Program::DoSomething

看一下 Main() 方法。

有我們定義的 s 這一個局部變量,代碼尺寸為 15 個位元組。

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       15 (0xf)
  .maxstack  1
  .locals init ([0] string s)
  IL_0000:  nop
  IL_0001:  call       string ConsoleApp1.Program::DoSomething()
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_000d:  nop
  IL_000e:  ret
} // end of method Program::Main

Release 模式下生成的中間代碼

而在 Release 模式下,實際上,DoSomething() 中所有的局部變量都被優化掉了。代碼尺寸也只有 11 個位元組。

.method private hidebysig static string  DoSomething() cil managed
{
  // Code size       11 (0xb)
  .maxstack  8
  IL_0000:  ldstr      "Hello, world."
  IL_0005:  callvirt   instance string [mscorlib]System.String::ToUpper()
  IL_000a:  ret
} // end of method Program::DoSomething

還可以看一下 Main() 方法,這個局部變量 s 也被優化掉了。代碼尺寸也只有 11 位元組了。

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       11 (0xb)
  .maxstack  8
  IL_0000:  call       string ConsoleApp1.Program::DoSomething()
  IL_0005:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_000a:  ret
} // end of method Program::Main

結論

編譯器會儘可能對代碼進行優化,我們可以為了提高代碼的易讀性增加一些局部變量,這並不會導致生成冗餘代碼並導致執行性能的下降。