What's the most efficient raycast distance, and why?

Assuming i’m casting against something specific (to see if the path to it is obstructed);

Is it most efficient to set the cast distance to exactly what i need, and no more Or is it best to use some constant value like infinity?

Also, does raycast performance depend solely on the colliders in its path? Does a raycast extending off infinitely into the distance cause any more overhead?

Like @Evil Tak says, physics engines usually optimise things by filtering out objects first by some really cheap calculation. For example by checking whether the bounding volume or AABB (in 2D) of the object is in range of the ray.

Only after that they check the remaining objects’ geometry to see if the ray actually passes through a hole in the object mesh’s geometry or does it really hit it, since this can be a very heavy calculation depending on the object’s complexity.

Of course Unity is not Open Source so we can’t know for sure… but I’d be very surprised if the ray length didn’t matter.

Raycasting also involves broad phasing, ideally first all objects outside the radius of the distance value are filtered out (thus infinite distance means no colliders are filtered out) . Then the bounds check comes into play. Collider.bounds is used (see Bounds.IntersectRay). Then as @NoseKills said, the actual raycast is done. The remaining colliders are raycasted (closest one first) and if it is a concave collider it can be pretty expensive depending on the complexity of the collider. Primitive colliders usually have better raycast performance than mesh colliders.