1 Update(), or 20? Performance question

Okay, so I’ve seen a lot of talk about how having too many Monobehavior-derived objects can lead to major performance problems on iPhone. So having 20 Update() calls is slower than having a single Update() call that consolidates all your game logic.

The simplest approach would seem to be to merely redefine your classes so that they do not inherit from Monobehavior, but keep a method called Update(), and simply loop through all your game objects in your single main Update(), calling each of the various object’s own Update() methods. But then it seems like this is basically just doing the same thing.

Or is it? Is there a difference between calling a method (be it named “Update”, or “JohnnyCarson”, or whatever) from within your scripts, and having Unity automatically call an Update() that is part of a Monobehavior-derived object?

If not, then it would seem that the only way to realize a performance improvement would be to put all of your game logic inline inside of your main Update(). Is this the case?

The precise answer is as few as possible but as many as needed to not lead to super functions.

Generally Update, FixedUpdate and OnGUI are a bad thing if they are spread around (OnGUI especially) so only have it on objects really needing it.
Depending on the game that can be a single manager class, different people have used the approach

In addition, I’d like to know exactly how methods like Update are being called. They aren’t being called using normal v-tables because:

a) Update doesn’t override anything, nor is it virtual.

b) Creating a script that derives from a class with an Update method that in turn derives from MonoBehaviour doesn’t actually cause the base Update method to be called (i.e. Update will ONLY be called if it is in the most derived class)

c) Examining the meta-data of MonoBehaviour reveals no virtual or abstract methods at all.

Right, I understand that part. But what I’m wondering is if function calls of any sort incur the same overhead, or if there’s something more taxing on resources about a Monobehavior Update()?

So in other words, would I be getting a performance increase from the following, or would it be more or less a wash performance-wise?:

Update()
{
   foreach(MyObject o in enemies)
      o.DoStuff();
}

Assuming I have 20 MyObjects, I’m still incurring 20 “DoStuff()” calls per frame, but are “DoStuff()” calls lower-overhead than if MyObject was derived from Monobehavior and instead of “DoStuff()”, I just put that code into MyObject’s “Update()” method? Either way, there are 20 calls taking place, so it seems it would be a wash to me, unless there’s something about Monobehavior I don’t understand.

There is no information on how those callbacks work differently.
Its though to assume that they are used as delegates behind the scene.

i’m assuming this is on an iphone not in editor but try it. that’s way slower. you would be better off having a manager and caching your objects at start. then doing something like…

function Update()
{
  for (i = 0; i < myObj.length; i++)
  {
    if (someCondition)
    {
       DoStuff(myObj[i});
    }
   }
}

function DoStuff(GO : GameObject)
{
   GO.transform....
}

ok that doesn’t really answer your question… or does it? i don’t believe there’s a big difference. yes, there’s “some” overhead per script but if you call a function every frame, you call a function every frame. you’re calling 21 vs 20. so yeah a little more but, whoopie, it’s not much. as i see it the difference in your example is that it looks like you’re declaring a class 20x every frame which will bloat the heap and cause a lot of GC. in that case yeah a simple update on each would be better afaik…

Well, what I wrote was intended as pseudocode, so yeah, that’s pretty much what I was talking about. It seems to me that there’s still 20 function calls. If function call overhead is so high, then I guess I really need to inline every bit of code I can, eh?

They are called directly from C++ code. Unity engine will locate your Update/FixedUpdate and such methods by name upon loading MonoBehavior components. At the run-time engine will call those methods using Mono mechanisms crossing “boundary” between C++ and C#. That is somewhat expensive operation - more expensive than calling virtual methods directly in C# land.

To increase performance, you should always use “Fast, but no Exception” setting in Player Settings for release builds. That will disable exception support for Update/FixedUpdate functions, but will increase performance of C++ → C# calls. Performance of such calls will improve with 1.1 too (together with overall C#/JS code performance).

just a quick (OT somehow) performance related question:

in Actionscript introducing variables in a iterated code-block (eg. loops) is a big NO NO NO (of course). How is the performance impact in Unity (iPhone especially) regarding variable allocation (generated on demand) vs. global variables (generated on launch)

Of course it would be bad design in loops etc., but is the overhead noticable for local variables in Update() etc ?

ReJ, just to clarify then, you’re saying that 20 calls from C++ → C# (i.e. Monobehavior Update() calls) would be slower than 20 calls from C# → C#?

Yes, I believe that’s what he’s saying.

Recently in 1.03 I moved all the code of some objects on my game (around 7 on screen at the same time) to a single Update function, that would iterate over an array of objects and call their custom update function. This increased performance noticeably.

I tried the same in 1.1 and the gains were marginal (if even any). If I factor in the cost of this non OO approach, there clearly isn’t any benefit for me in placing everything in a single update.

I suggest you try it yourself (it’s really not that much work, just rename your object’s update function to some other name and on the main update funtion iterate with a foreach) and see if it pays off.

BR,
Alex

Yes.
For 1.0.3 C++ → C# is more expensive than C# → C#.
But in 1.1 C++ → C# should be almost as fast as C# → C#.

Awesome, thanks! I can’t wait for 1.1.

I would like to clarify something. If I have a class derived from Monobehavior but I do not “override” Update(), is it still doing a C++ → C# call that causes the performance issues we are discussing?

It is not somehow related to “override”, just engine main loop runs as native C++ code, and scripts are running as precompiled managed .NET code. When engine needs to invoke any script method (like Update, FixedUpdate, etc.) C+±>C# boundary should be crossed. This boundary crossing has a bit more overhead than C#->C# method invocation, but with upcoming Unity iPhone 1.1 we optimized these things a lot and you can expect C+±>C# call performance to be very similar to the C#->C# call performance. Recommendation to use OnGUI as little as possible still remains valid.

I understand that the overhead has been minimized in 1.1, but I am still just trying to understand if there is a call taking place to Update() on all Monobehaviour derived classes even if the Update() is not being used? If so, then is there an argument to using Object derived classes whenever possible instead?

EDIT:
Engine searches for explicitly implemented/overridden Update methods inside scripts that are attached to the live game objects. So if you don’t write Update method in your script then there is no performance overhead.

Ahh very good to know, thanks. One last question in this regards then. Considering I do not actually need to derive from Monobehaviour since I am using just 1 update loop, isn’t there at least some memory benefits to deriving other classes from Object instead then?