Do unnecessary curly braces reduce performance?

After recently encountering this while programming, I have been wondering about this. Below are 2 snippets which are both legal and compile. Specifically, my question is this.. in the second case, do the brackets make the program slower? Also why is this allowed?

1st case:

if (statement)
{
 // do something
}

2nd case:

{
    if (statement)
    {
        // do something
    }
}

Additionally what if I had something like the code below.. Is the runtime the same as just calling function X without any of the curly brackets.

{
  {
    {
      // call function X
    }
  }
}
Jon Skeet
people
quotationmark

Most of the time it doesn't make any difference - and you should definitely code for readability more than anything else.

However, curly braces can have an effect on performance, in a surprising way, although it's pretty unusual. Consider this code:

using System;
using System.Collections.Generic;

class Test
{
    static void FewerCurlies()
    {
        List<Action> actions = new List<Action>();
        for (int i = 0; i < 100; i++)
        {
            int x;
            if (i % 3 == 0)
            {
                actions.Add(() => x = 10);
            }

            int y;
            if (i % 3 == 1)
            {
                actions.Add(() => y = 10);
            }
        }
    }

    static void MoreCurlies()
    {
        List<Action> actions = new List<Action>();
        for (int i = 0; i < 100; i++)
        {
            {
                int x;
                if (i % 3 == 0)
                {
                    actions.Add(() => x = 10);
                }
            }

            {
                int y;
                if (i % 3 == 1)
                {
                    actions.Add(() => y = 10);
                }
            }
        }
    }
}

The extra braces in MoreCurlies look redundant, right? Not quite... the generated code looks more like this:

using System;
using System.Collections.Generic;

class Test
{
    static void FewerCurlies()
    {
        List<Action> actions = new List<Action>();
        for (int i = 0; i < 100; i++)
        {
            FewerCurliesCapture capture = new FewerCurliesCapture();
            if (i % 3 == 0)
            {
                actions.Add(capture.Method1);
            }

            if (i % 3 == 1)
            {
                actions.Add(capture.Method2);
            }
        }
    }

    static void MoreCurlies()
    {
        List<Action> actions = new List<Action>();
        for (int i = 0; i < 100; i++)
        {
            {
                MoreCurliesCapture1 capture = new MoreCurliesCapture1();
                if (i % 3 == 0)
                {
                    actions.Add(capture.Method);
                }
            }

            {
                MoreCurliesCapture1 capture = new MoreCurliesCapture2();
                if (i % 3 == 1)
                {
                    actions.Add(capture.Method);
                }
            }
        }
    }

    private class FewerCurliesCapture
    {
        public int x;
        public int y;

        public void Method1()
        {
            x = 10;
        }

        public void Method2()
        {
            y = 10;
        }
    }

    private class MoreCurliesCapture1
    {
        public int x;

        public void Method()
        {
            x = 10;
        }
    }

    private class MoreCurliesCapture2
    {
        public int y;

        public void Method()
        {
            y = 10;
        }
    }
}

The differences here are:

  • An instance of the capture class is created in each iteration of the loop in FewerCurlies, even if it's not used
  • Each instance of the capture class used in FewerCurlies contains both variables, even though each delegate will only actually use one of them, whereas in MoreCurlies each capture class only captures a single variable

This is all somewhat implementation-specific, but it shows that redundant-looking curlies can have an impact.

people

See more on this question at Stackoverflow