liveBook · Manning

Chapter 11: Branching, performance, and extendibility

What you will learn in chapter 11 of High Performance Unity Game Development

The introduction explains how branches affect code complexity and runtime performance, and how grouping data can remove checks from hot loops.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains how branches affect code complexity and runtime performance, and how grouping data can remove checks from hot loops.

Key terms: branches, code complexity, branch prediction, branch misprediction, pipeline, comparison and jump instructions, data-oriented design, alive and dead enemy indices, arrays, branch checks

11.1 Branch prediction

This section uses a controlled comparison to show that predictable branch results are easier for a CPU to predict than random ones.

Key terms: branch prediction, predictable branch results, branch misprediction, Array, Mod2Array, RandomArray

11.2 Avoiding early return

The section explains why moving branches and repeated validation out of a hot function makes its behavior clearer and avoids unnecessary checks.

Key terms: moveEnemy(), branch, paralyzed, early-return branch checks, edges of the system, controlled runtime loop, hot code

11.3 Unity and null checks

Explains why Unity object null checks differ from ordinary C# checks, why some C# null syntax is unsafe for them, and how much repeated checks can cost.

Key terms: UnityEngine.Object, managed reference, native object, equality operator, null-conditional operator, null-coalescing operator, == null, null check, GameObject, Android

11.3.1 Check once

This subsection explains how to avoid repeated Unity null checks for asset references while recognizing that objects can still be destroyed unexpectedly.

Key terms: AssetManager, null check, #if UNITY_EDITOR, dynamically download assets, separate boolean, Unity object, GameObject, parent object, coroutines, asynchronous calls

11.3.2 Zero Tolerance

This section explains why Unity teams should treat development errors as stop-work events and how to make logged errors hard to miss.

Key terms: zero-tolerance approach, null reference, console errors, logMessageReceived, error, exception, editor

11.3.3 Own the lifetime

Owning GameObject lifetimes makes their validity predictable, allowing unnecessary runtime null checks to be removed.

Key terms: GameObjects, lifetime, null checks, Init(), Tick(), dynamic pool

11.3.4 Avoid fire-and-forget behavior

This section explains how work that outlives a Unity GameObject causes hard-to-find errors and why explicitly controlled ticking is safer for the book’s frame-by-frame gameplay systems.

Key terms: coroutine, static event subscriptions, GameObject, MonoBehaviour, fire-and-forget behavior, timers, Tick(), null checks

11.4 Separating data to reduce branch checks

The section shows how storing indices by their branch result reduces checks when iterating enemies and finding unused GameObjects in a pool.

Key terms: branch checks, aliveEnemyIndices, deadEnemyIndices, isAlive, GameObject pool, m_enemyPoolUnusedIndices, m_enemyPoolUnusedIndicesCount, m_enemyPoolUsed, Show(), Board.Tick()

11.5 Separating objects to avoid branch checks

The section argues for separate UI scripts when menus need different behavior, while preserving visual consistency through shared assets and data.

Key terms: top bar, branches, data-oriented design, shared assets, shared data, QA, MainMenuTopBar

11.6 Default parameter values can hide branches

This section shows how a default parameter can make a branch inside a method easy to overlook at its call sites.

Key terms: default parameter values, moveEnemy(), enemyParalyzed, hidden branch

11.7 Conclusion

The conclusion connects the performance and readability costs of branches to ways of reducing checks in Unity code.

Key terms: branches, branch prediction, default parameter values, GameObjects, null checks, overloaded equality operator, zero-tolerance policy, fire-and-forget behavior, alive and dead enemies, unused pool object indices

11.8 Summary

The summary explains why branches can hurt performance and maintainability, and how managing object lifetimes and separating data can reduce repeated checks.

Concepts explained in this chapter

← Chapter 10: DOD and Dictionaries Chapter 12: Unity DOTS →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.