Code optimizations in .NET on MSIL level

9:42 PM

Introduction 


The purpose of the article is to get familiar with compiler optimization in .Net. This is a brief overview that should give you a start vector and to introduce some basic showcases. Article is intended for beginners+ level.

I hope you find useful this article.

Points of Interest 


When we write a code, its eventually transformed into machine code and its able to run, provide some outputs or demand some input from user following its business logic that we put it in our code.

But the question, is exactly our code was transformed into machine code as we wrote it or there were some changes in the middle of the way in our code. The short answer is yes, some code change was occur. The idea behind that is very simple: .NET optimize code before it turns into machine code. Before we will dive in examples and explanation, lets try understand whole process.

When we compile and run:

  1. 1. CLR (Common Language Runtime) transform our code into MSIL (Microsoft Intermediate Language).
  2. 2. CLR compile MSIL into platform-specific code.
  3. 3. In case we run program, MSIL turns into machine code by JIT.

I know may for some of you it will sound complicated and for some of you its just collection of Chars (CLR,MSIL,JIT...), but do not worry!  We interested only in first step, which is compiler optimizations.

Now lets see some example:

using (TextWriter w = File.CreateText("log.txt"))
{
    w.WriteLine("This is line one");
}

Lets try to remember what is 'using' for in this context with inner scope. In case we have some resources that we should release as quick as possible, we will use "using" in such statement (with using like this).

And the reason we will do it, because it will ensure that those resources will be released right after exiting from the scope (by invoking Dispose() method). Actually this is short form will be translated later in form like following:

    
    bool flag;
    TextWriter w = File.CreateText("log.txt");
    try
    {
        w.WriteLine("This is line one");
    }
    finally
    {
        flag = w == null;
        if (!flag)
        {
            w.Dispose();
        }
    }

Looks like after optimization, this code looks much convenient.

By the way:
  for (int i = 0; i < 3; i++)
  {
     Console.Write(i);
  }

After optimization will looks like following:
    bool flag;
    int i = 0;
    while (true)
    {
        flag = i < 3;
        if (!flag)
        {
            break;
        }
        Console.Write(i);
        i++;
    }

How to see code after optimization


I used JustDecompile from Teleric. After instalation it will look like this(ver. 2012.3.1119.12).

Create Console Application and put following code in Main() method and press F5:
using (TextWriter w = File.CreateText("log.txt"))
{
    w.WriteLine("This is line one");
}

Now press Open button in JustDecompile and then File(s) and choose exe file from project you just build (usualy can be found in  ..... /Bin/Debug/) Expand left panel of the  JustDecompile application as shown and you will see the desired result:


What Next:

Next is your practice. Experiment and get you knowledge more sharp.
Thank you for your patience !

You Might Also Like

0 comments.

Popular Posts

Total Pageviews