Open rikimaru0345 opened 5 years ago
Unity.Mathematics is almost exclusively intended for use with Burst, which has intrinsics for it. The C# implementation is mostly just a reference. Have you compared the generated assembly that Burst generates for the same functions?
Also sorry for closing, it was a misclick.
Correction! I had under-researched, and at least some people intend this for outside burst, so I believe we will look into this after all.
I could even see this library being used outside of Unity, because there's practically no coupling to it.
As I understand, it can not be used outside Unity because of license.
Thanks for the detailed report. We're hoping to look at Unity.Mathematics performance without Burst, so this information could be very useful!
Hi there! I noticed two problems that reduce performance and can easily be fixed :smile:
1. Not using throw helpers
The jit will never inline a method if it throws an exception, even if you add the AgressiveInlining attribute.
Example: int2.gen.cs line:775
The common fix for this is very simple, just use a "throw helper", like this:
2. Using the "fixed" statement instead of a reinterpret cast
The code used for indexers can be improved a lot! Example: int2.gen.cs line:616
The fixed statement is horrible because just like throwing exceptions, it prevents a ton of optimizations (it is implemented that way on all runtimes I tested!).
Take a look at the asm it generates:
Code:
(
z = test1[6]
is there on purpose, not a typo)Assembly:
This should instead be done using a reinterpret cast.
Keep in mind that even though the syntax of
Unsafe
can sometimes (fortunately not here) become really unwieldy, it pretty much always compiles down to very simple instructions. (In fact methods likeUnsafe.As<TFrom, TTo>(ref source)
are literally anop
because they only trick the C# compiler into accepting the type change). It's really just the C# name forreinterpret_cast
.By simply casting
ref this
to a little helper struct all overhead is gone.Btw
Unsafe
is defined inSystem.Runtime.CompilerServices.Unsafe
. TheAs()
method I'm using here has an extremely simple definition (in this case it does literally nothing as you can see). All other functions in there are just as trivial, so they should be really easy for Unity to add :+1:Here is what I did instead:
And with the exact same C# code, this time the disassembly looks like this:
This time the jit was able to absolutely decimate the code! Nice! Let me know what you think :smile: