5

Assuming we have the following model:

public class Father
{
    public Child Child { get; set; }
    public string Name { get; set; }

    public Father() { }
}

public class Child
{
    public Father Father;
    public string Name { get; set; }
}

And the following implementation:

var father = new Father();
father.Name = "Brad";
var child = new Child();
child.Father = father;
child.Name = "Brian";
father.Child = child;

Now my question: Is codesnippet #1 equivalent to codesnippet #2?

Or does it take longer to run codesnippet #1?

CodeSnippet #1:

var fatherName = father.Child.Father.Child.Father.Child.Name;

CodeSnippet #2:

var fatherName = father.Name;
5
  • 2
    My vote is for YES. Commented Jun 27, 2013 at 10:22
  • Yeah, the compiler won't optimize that. Commented Jun 27, 2013 at 10:23
  • It is worth noting that even if the compiler doesn't optimize the second to the first, all of the relevant memory locations for the code snippet will clearly fit in the cache, so actually resolving the references between parent and child again and a gain will be super fast since everything will already be in the cache.
    – Servy
    Commented Jun 27, 2013 at 15:29
  • The jitter was only written to optimize sane code, it is not going to burn precious cpu cycles looking for silly putty. Commented Jul 2, 2013 at 18:56
  • @HansPassant True. Though it is not always easy to determine what is considered to be 'sane'. Commented Jul 8, 2013 at 20:56

2 Answers 2

4

The C# compiler will not optimize this, and will emit just all operations for calling the property getters.

The JIT compiler on the other hand, might do a better job by inlining those method calls, but won't be able to optimize this any further, because it has no knowledge of your domain. Optimizing this could theorethically lead to wrong results, since your object graph could be constructed as follows:

var father = new Father
{
    Child = new Child
    {
        Father = new Father
        {
            Child = new Child
            {
                Father = new Father { ... }
            }
        }
    };

Or does it take longer to run codesnippet #1?

The answer is "Yes", it would take longer to run the first code snippet, because neither C# nor the JIT can optimize this away.

1
  • Any optimization could lead to wrong results. That's why compilers perform analysis to prove it safe before doing it.
    – user395760
    Commented Jun 27, 2013 at 10:39
2

No, the code snippets are not equivalent.

The first code snippet will give you a NullReferenceException just as expected, as you haven't assigned anything to father.Child.

Even if you did assign the child to the father.Child property, the compiler can't assume that the values will always stay that way, so it can't optimise away anything from the first snippet.

5
  • It appears to me that simple inlining and analysis can determine that the value will stay the same while (\.Father|\.Child)* is evaluated. Could you elaborate why you think the compiler (both the csc.exe and the JIT compiler) can't assume that?
    – user395760
    Commented Jun 27, 2013 at 10:32
  • @delnan: The values will stay the same while the snippet is executed, but the compiler can't assume that they always will contain the same values as first assigned.
    – Guffa
    Commented Jun 27, 2013 at 10:40
  • Okay, I was assuming the initialization and the attribute access were in the same method. Technically the compiler could still generate code that recognizes the cyclic reference and has a fast path for it, but this is purely hypothetical and I doubt any compiler would ever bother optimizing specifically for that case.
    – user395760
    Commented Jun 27, 2013 at 10:42
  • @Guffa: Thanks for the hint about the father.Child Property. I changed my code accordingly. Commented Jun 27, 2013 at 11:41
  • JIT can't know that the referenced objects won't change, first of all because it does point optimizations, not full program evaluations. Second because multi-threading. Commented Sep 1, 2020 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.