[Suggestion] Unity add an option to inline maths operations.

Just to clarify calling a function to add 3 pairs of values together is slower than just adding those values together as you are no longer calling the function and passing the values.

So

VectorA = VectorA + VectorB

is slower than

VectorA.x += VectorB.x;
VectorA.y += VectorB.y;
VectorA.z += VectorB.z;

And as Unity are doing all kinds of code conversion tools technology, e.g. Updating Unity 4 projects to Unity 5 or IL2CPP ing code to C++ and native builds or ASM.js.

Could Unity add an option to inline code?

In effect maths functions could be inserted into the code base as part of the build process and this could have a great impact on performance.

So calls to Mathf., Vector Ops, Matrix, Quaternions etc. could all be inlined.

Now this would increase the build size but give a speed boost, also UT could provide options to inline all math functions or inline only marked e.g [inline] code segments.

Or some enterprising programmer could just write a tool and sell it on the store ;0)

1 Like

Mono/.NET already does function inlining. Presumably when Mono is eventually upgraded in Unity, there would also be the option to use the AggressiveInlining hint.

–Eric

But this feature/tool could be added very quickly and much easier than an upgrade to the version of Mono/.Net.

Seems unlikely. Also, automatically inlining with no regard for actual usage is a bad idea, because the code bloat cancels out the inlining gains; bigger code is less likely to fit in CPU caches. Compilers are statistically better at inlining than programmers, so it should be left to compilers 99% of the time.

–Eric

1 Like

But as you said earlier Unity are using an old version of Mono/.Net which may not perform optimally.

Even when the compile via IL2CPP the code generated is dependent upon the compiler and flags used.

But why not test it for yourself → http://forum.unity3d.com/threads/vector3-profiling-and-optimising-unrolling-x-y-z-faster-than-v3-op-v3.108052/

As such, the Unity devs should be focused on getting us up to more recent .Net support, which has universal implications in all areas and not just… umm… addition. Anything that’s requested as a stopgap measure in a tiny area like this, and which slows them down from that pursuit, should be burned with all of the vehemence we can muster.

The same was true for all of the demands for OnGUI additions when they were working on the Canvas system to replace it, and I for one am glad that the Unity team decided to ignore such requests- it took them long enough even when their attention wasn’t divided on stopgap measures.

2 Likes