Menu

Unity3D: Mythbusting performance

While we don’t have any actual development progress to report on, I’ve decided to use this blog for some (hopefully) useful articles on game development. Unless you’re working with Unity3D engine, you can probably skip this post entirely.

I’ve been thinking lately about some performance “tips” for Unity3D that everyone seems to be repeating. One of the common tips is “don’t use GetComponent, it’s slow” and its more strong version “don’t use built-in properties like transform, they’re just a wrapper around GetComponent.” This seems right, but has anyone tested it? I know of one attempt, but seems kinda lacking. I’ve repeated that one myself some time ago, but that was with older version of Unity… and, I think I can measure a lot more things. So, let’s get to benchmarking!

We’ll be using this little helper class for measuring performance:Timer code

Not Found

Obligatory benchmarking note: the times presented only make sense for one particular computer. Only use absolute values as ballpark estimates, if at all. I’ve also ran the tests multiple times; the figures used are some typical values, not exact mean values or anything.

Let’s start with simple things. Measuring absolutely nothing:

using (new Timer("Empty")) { }

Profiled Empty: 0.00ms

Just as expected, this takes no time at all. Sanity check passed! Now, since we’re measuring pretty fast things, we’re gonna repeat every test a bunch of times. 100 000 000 times, to be exact. So, let’s measure an empty cycle:

This is gonna give us a baseline: on my machine, just the overhead of counting to 100 000 000 times takes 38ms. Or, rather, 28-48 – I’ve tested a few times, and it’s not very stable. Let’s actually do something in the cycle now. The fastest methods of accessing anything should be when it’s cached in a class field; and let’s throw in a property access as well:

Field access is just as fast as an empty cycle! This can be explained in two ways: first, the compiler (or JIT) might have just optimised the access away – since we’re not doing anything with it. Second, field access itself is so fast that it’s lost in the noise. I think the first is more likely to be right, because accessing a property, as opposed to field, added a whole 150ms to our running time. While property should add some overhead, I don’t think it’s adding more than an order of magnitude – so probably, some of these 150ms is just field access that wasn’t optimised away. In any case, it’s still nothing to worry about: 150ms over 100 000 000 times gives about 1.5 nanoseconds per access.

OK, that was just a property that we declared ourselves – basically, we just confirmed that accessing a cached component is fast. What about the “evil” built-in property?

There are two surprises here. First, GetComponent is actually slower than built-in property. Second, generic version of this method is about twice as slow as its non-generic counterpart! The latter observation seems to be relevant for all generic methods: the overhead of calling a generic method is considerably larger than that of a non-generic one. I’ve experimented with different methods, including ones I’ve written myself, and this seems to hold.

The first observation is more puzzling though. The built-in property clearly does not call GetComponent internally (or it wouldn’t have been faster), but it’s still slower than my own. So, what does it do? Before we answer that, let’s check out another surprising thing. While transform property might be kinda complex, Transform.position should be really straightforward. I mean, just return the vector, that’s all! Measuring it now:

Whaat?! Even when we use lightning-fast field access, the moment we try to read position, execution time jumps to 2 seconds! In fact that time is surprisingly close to what we got when accessing built-in transform… and when we use both built-in properties, we got 4 seconds. It seems that any built-in Unity3D property has around 20ns overhead! To understand how that happens, let’s look at Unity code. I believe decompiling UnityEngine.dll is technically illegal, but since it’s such an awesome source of information, I’m gonna do it anyway:

They seem pretty similar, don’t they? In case you’re not very familiar with C#, what this code means is that both properties are actually implemented in native code. Communication between C# and native C++ carries its own overhead; and I believe this is what we’re seeing. Just calling any native code from C# takes additional 20ns (on my machine.)

This explains the cost of using built-in properties. This could also make you wary of using any built-in properties – including coordinates, object names, velocities etc. Unfortunately, these are not practical to cache, so you have to eat up the cost. Fortunately, this cost is actually pretty small! 15 times relative increase might seem like a lot, but in absolute terms it’s still peanuts.

Actually, even the dreaded GetComponent call doesn’t look all that bad, especially the non-generic version. It’s only about 2-3 times slower than accessing the built-in property.

There’s still one interesting benchmark left, though. We don’t really know what GetComponent method does in native code, but it clearly has to do some sort of search through all attached components. If that’s the case, it should become slower, the more components we add to our gameobject. Let’s test that!

Just as expected, having a bunch of components on the gameobject can slow GetComponent down significantly. Note that if we tried to access Transform component, there would be no slowdown (I’ve actually tried). This seems to indicate that GetComponent uses a simple linear search under the hood. It actually makes sense – given that most objects have less than 10 components, simple linear search would probably be faster than any fancy-pants O(ln(n)) algorithms, due to lack of overhead.

tl;dr

The takeaway from all this is:

Unity’s built-in properties are slower than direct access, and that includes all properties, not just those for different components.

However, they’re not slow enough to actually matter most of the time.

GetComponent method is slower still, but even that is not the performance-killer. A single raycast is probably enough to drown a GetComponent call.

You should probably cache the GetComponent calls that happen every frame. Don’t bother with occasional calls, they’re not that bad.

There’s no need to cache built-in properties, unless the script in question runs literally thousands of times per frame.

You should prefer non-generic methods to generic ones, especially in performance-critical sections

Note that I’ve done all these benchmarks on a Windows PC. The overheads might be different on different platforms, so it’s best to repeat all tests yourself. Only then you would know for sure.

And the last note. While I called GetComponent call “slow”, there is no faster alternative. I’ve tried to create one, by caching an array of all components in OnEnable, and searching through it. Without going into details, I can say that no code of mine outperformed the GetComponent, despite having no managed-to-native transitions. So, in fact, GetComponent is actually quite fast for what it does!

Share this:

11 responses on “Unity3D: Mythbusting performance”

The reason the generic versions of various methods are slower than the non-generic one is quite simple:

They were tacked-on after-the-fact and essentially just call the non-generic ones, after getting the name of the type parameter as a String. That conversion to String is not a cheap operation (especially on mobile, although to be fair I last tested String-related mobile vs. desktop performance on a first-yen iPhone so I’m guessing things have come a ways since then).

Behind the scenes, Unity is still indexing and working with everything by String rather than by type — the generic methods are there only to allow developers using Unity to get some compile-time type safety benefits.

This was my first thought too, but it’s does not explain everything. I’ve tried my own generic and non-generic methods, and generic methods are slower. Even when literally the only difference between them is the addition of <T> in the generic one.

There may also be a performance overhead in terms of generated code, which may explain a lack of interest by UT in restructuring things around types instead of strings, but ISTR looking at the disassembled code and seeing it use the Type.Name property under the hood…

I never thought to measure which was the greater overhead, as at the time it hadn’t occurred to me generics might be slower.

Nice blog post, but I feel like your tests should also cover garbage before you start making performance tips. Because I have a feeling that a lot of the things you say aren’t so bad will in fact generate garbage, and that adds up on a handheld device and then the garbage collector comes more often, resulting in choppy performance.

Which platform did you profile?
btw, transform.position is actually more complex than most people think. It computes the absolute position (which is not cached), not the local position. that’s why it does not just return a vector.