So I have a lot of static math functions I ported to unity mathematics. Some of the functions are also used by monobehaviours is there a reason for me to maintain the old math code or should I remove it? As far as I can see mathematics is a better math library then Mathf.
I guess I could save on boxing and unboxing.
Not only should you probably remove it, but you should probably add some new [BurstCompile] attributes because this is a thing: GitHub - keijiro/BurstTestbed: Collection of Unity Burst samples
2 Likes
If you work with burst, you should be using float related DOTS Unity math.
However, unity classic mathf has certain methods optimised, to work better with vectors. That is, when you donāt need burst, and keep working with game object and classes.
Iāve found unity.mathematics & NativeArray<> stuff in general to be significantly slower when called in regular boring managed code. Likely the compiler has problems inlining all the nested methods in unity.mathematics. Youāll also be tempted to use various SIMD-based methods that than run on their slow software fallback because mono doesnāt optimize it. NativeArray overhead is probably because of checking safety on every index op, but I havenāt confirmed that.
Itās a common mistake in āburst vs managedā benchmarks, where they benchmark the burst job without burst as the āmanagedā version. While a managed version using Mathf/Vector3 & regular arrays would outperform the mathematics/nativearray based version by a few factors.
So, Iād say, keep your own math library alive for now. But for every new system or rework of existing systems, strongly consider if you can reasonably make a burst variant of it. Especially with the direct function call stuff linked above, you may be able to isolate the math heavy bits into a burst function.
P.S edit: I may be slightly out of date on the unity.mathematics being significantly slower in managed code, I just recalled there have been package updates that have tried to address it somewhat (like basically all of it is marked with AggressiveInlining now). But Iāll leave my paragraphs as is for posterity)
Unity.Mathematicās Pow() is actually System.Math.Pow() according to their git of the hub.