Version: Unity 6.6 Beta (6000.6)
Language : English
Constrain integer values to a range
Check for compile-time constraints

Add optimization hints to your code

Use the Hint intrinsics to add information to your code that helps Burst optimize it. It has the following methods:

Likely intrinsic

The Likely intrinsic indicates that a branch condition has a high probability of being true, which means Burst can focus on the branch in question for optimization purposes:

if (Unity.Burst.CompilerServices.Hint.Likely(b))
{
    // Any code in here will be optimized by Burst with the assumption that we'll probably get here!
}
else
{
    // Whereas the code in here will be kept out of the way of the optimizer.
}

Unlikely intrinsic

The Unlikely intrinsic indicates the opposite of the Likely intrinsic: the condition is unlikely to be true, which allows Burst to optimize for the false scenario:

if (Unity.Burst.CompilerServices.Hint.Unlikely(b))
{
    // Whereas the code in here will be kept out of the way of the optimizer.
}
else
{
    // Any code in here will be optimized by Burst with the assumption that we'll probably get here!
}

The Likely and Unlikely intrinsics ensure Burst places the code most likely to be hit after the branching condition in the binary. This means that the code has a high probability of being in the instruction cache. Burst can also hoist the code out of the likely branch and spend extra time optimizing it, while spending less time on the unlikely code.

An example of an unlikely branch is checking if the result of an allocation is valid. The allocation is valid most of the time, so you want the code on that path to be fast, but you also want an error case to fall back to.

Assume intrinsic

The Assume intrinsic indicates that Burst can assume a condition is always true. Use this with caution.

Use the Assume intrinsic to arbitrarily tell Burst that something is true. For example, you can use Assume to tell Burst to assume that a loop end is always a multiple of 16, which means that it can provide perfect vectorization without any scalar spilling for that loop. You can also use it to tell Burst that a value isn’t NaN, or that it’s negative.

Unity.Burst.CompilerServices.Hint.Assume(b);

if (b)
{
    // Burst has been told that b is always true, so this branch will always be taken.
}
else
{
    // Any code in here will be removed from the program because b is always true!
}

Additional resources

Constrain integer values to a range
Check for compile-time constraints