Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Null Check Inconsistancy

Discussion in 'Scripting' started by BmxGrilled, Sep 16, 2014.

  1. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    238
    I know this has been discussed and discussed, however I felt there wasn't a real investigation into potential base benefits of the == operator being restored to base functionality, and a property such as "isDestroyed" take it's place. Here's a design point of view with actual benchmark results.

    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. using System.Collections;
    4.  
    5. public class Test : MonoBehaviour
    6. {
    7.     void Start()
    8.     {
    9.         GameObject gameObject = new GameObject();
    10.         GameObject.Destroy(gameObject);
    11.         bool isNull = gameObject == null;
    12.         DateTime endTime = DateTime.Now;
    13.         int counter = 0;
    14.         DateTime startTime = DateTime.Now;
    15.         for (int i = 0; i < 1000000; i++)
    16.         {
    17.             if (isNull) { counter++; }
    18.         }
    19.         endTime = DateTime.Now;
    20.         TimeSpan totalTime = endTime - startTime;
    21.         Debug.Log(totalTime.TotalMilliseconds);
    22.      
    23.         counter = 0;
    24.         startTime = DateTime.Now;
    25.         for (int i = 0; i < 1000000; i++)
    26.         {
    27.             if (gameObject == null) { counter++; }
    28.         }
    29.         endTime = DateTime.Now;
    30.         totalTime = endTime - startTime;
    31.         Debug.Log(totalTime.TotalMilliseconds);
    32.      
    33.     }
    34. }
    35.  
    The above code is basically a benchmarking script, 1m cycles per benchmark, benchmarking bool check, and benchmarking == check with the existing == operator provided to us by Unity.
    This produced results such as 10ms to bool checking, and 40ms to the == operator.
    The reason I checked bool against ==, is because if Unity replace the == functionality with a bool value such as isDestroyed, it would be a bool value, which would be assigned only once (when the object is destroyed). So any check of a bool vs == of the object using the existing method, will be as much as 4x faster.

    This is my professional review of the null inconsistancy issue, although I'm pretty sure Unity have already decided they will implement isDestroyed vs the == operator, simply because if I know these benchmark values, so do they.

    My suggestion is to update the unity documentation to inform people that this change may happen in U5. And perhaps keep the Equals method overrided, but leave the == operator normal?

    Any feedback is of course welcome, this is of course just one point of view, however I felt that there wasn't a professional outlook on this given, rather just a dispute that at a glance could be summarised as "No update unity, evolution must take place, backwards compatibility sucks" vs "No don't update unity, evolution sucks, backwards compatibility rocks", my point is that no conclusive evidence was given to prove either statement, so here it is, I hope this is useful and helps. :)

    ~
     
  2. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    calling destroy does not immediately destroy the object. It just marks it for deletion and it won't actually happen until the end of the frame so that check will not return null. I believe DestroyImmediate would destroy it immediately but if I recall you shouldn't use DestroyImmediate for performance reasons outside of editor scripts (I could be wrong on that one).
     
  3. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    238
    regardless of if the benchmark checks against true, or false, the result is the same, it's irrelevant to the actual problem, the performance difference is still there. Hope this explains a little more what the actual issue is.