Doing some profiling using the Hint Likely/Unlikely intrinsic and I’m not sure what is going on here. So firstly the result.
The code
public int GetClosestSegmentNew(float3 position)
{
// Find the first point in the closest line segment to the path
float closestDist = DistToSegment(position, this.Path[0].Position, this.Path[1].Position);
int closestSegment = 0;
for (int i = 1; i < this.Length - 1; i++)
{
float dist = DistToSegment(position, this.Path[i].Position, this.Path[i + 1].Position);
if (Hint.Unlikely(dist <= closestDist))
{
closestDist = dist;
closestSegment = i;
}
}
return closestSegment;
}
Using Unlikely hint - the correct choice
24.22 without hint
20.97 with hint
A pretty significant improvement
Using Likely hint - the wrong hint
Ok now the results are weird. How is it also faster!? It’s not as fast (and I repeat this test a lot of times to confirm and it’s consistently a little slower than Unlikely) but it’s still significantly faster than not having a hint. How?
and just to confirm it’s not something odd, removing the hint.
Both tests run the same speed as expected.
So the bad and naive TLDR would be, just add hints to every condition and things will run faster!
What I’m trying to understand is what is going on here and how both likely and unlikely on a condition could be faster.
-edit-
totally forgot there is a burst forum now I’ve cross posted to it here: