Skip to content

Performance critical context and costly methods

Matt Ellis edited this page Feb 19, 2019 · 11 revisions

Performance critical context

Unity event functions Update, LateUpdate and FixedUpdate are called very frequently, either every frame or often enough to match the fixed frame rate. Similarly, methods invoked as coroutines can be suspended and resumed per frame. It is clear that these methods can affect the performance of your game, and it is important to be aware of, and hopefully avoid, expensive operations within these methods, and within the methods that call them.

Rider will highlight these frequently called methods and each method called from them via line markers in the editor gutter. These methods are considered to be a performance critical context. Inside this context, Rider will add performance indicators to highlight expensive methods and operations.

These highlights are not traditional inspections such as warnings or suggestions - the code isn't "wrong", but is doing something that is known to be expensive, and avoiding that code pattern is likely to be a good thing for the performance of your game. Typically, there is no simple mechanical fix for these highlights, and avoiding the operations usually requires larger work, such as rewriting or even some element of rearchitecting. Essentially, these performance indicators are intended simply to provide awareness that expensive operations are taking place. It is up to you to decide if you wish to avoid these operations, and how to do so.

It is important to note that these performance indicators are not a substitute for performance profiling. While an operation might be highlighted as expensive, it is entirely possible that performance is "good enough" with the code as it stands. Always be measuring!

Expensive operations

Rider will highlight various well-known expensive operations inside a performance critical context:

  1. Calls to AddComponent methods
  2. Calls to Find methods
  3. Calls to GetComponent methods
  4. Calls to Debug.Log methods
  5. String based method invocation
  6. Usage of the Camera.main property
  7. Comparing Unity objects with null

If a method contains one of the above expensive operation, it is itself marked as expensive, and this propagates back to the original calling method. In other words, Rider will highlight any method as an expensive operation if it contains an expensive operation or calls another method that contains an expensive operation. For example, given the following:

public void Update()
{
  DoSomething();
}

private void DoSomething()
{
  DoSomethingExpensive();
}

private void DoSomethingExpensive()
{
  var c = GetComponent<Grid>();
  // ...
}

Then all methods, Update, DoSomething and DoSomethingExpensive are marked as a performance critical context, with a marker in the editor gutter. The call to GetComponent is marked as an expensive operation, and this propagates back to the original caller. So the call to DoSomethingExpensive is marked as an expensive operation, and finally the call to DoSomething is also marked as expensive.

You can disable the expensive method highlights individually in the Preferences | Editor | Inspection Settings | Inspection Severity | C# settings page, and disable the performance indicator gutter line marker in the Preferences | Languages & Frameworks | Unity Engine settings page. You can also edit the colours of these highlights in the Preferences | Editor | Color Scheme | Unity page.

Clone this wiki locally