Ohh the new @unity3D Mathematics library is now up on github! github.com/Unity-Technolo… It looks a lot like the math api you use for writing shaders. Why use this over the current maths types? Well this is designed for Burst! It can do SIMD optimizations that makes this HELLA fast!
Actually... lets demonstrate how much FASTER it is by making a tiny lil example with Unity C# jobs! lets do this in real time~ first, to use it you need Unity 2018.1, you can get the Matematics library from the Package Manager like this, also set the new .net runtime!
Lets code up a little example - here we have two jobs defined. They both do a simple bit of maths - its basic eluer physics intergration using the regular Unity Vector3 type, nothing fancy. one job does it single threaded and the other does it multithreaded.
Lets schedule both to run every frame so we can compare multithreaded vs single, annnnnd lets do the mats a million times every frame cos why not~ CPUs are fast so we can do a lot of maths ^^
Annnd OH GOSH THE PERFORMANCE IS TERRIBLE 177~ms to process both jobs! most of that is spent doing it single threaded. My system has a lot of CPU cores, so the threaded version is split over 11 worker threads, each taking about 13.6ms.
Turns out maths in C# is a bad time- if we multiply that 13.6ms by 11 it comes out at 150ish-ms, so basically the same total time as doing it on one thread - what you would expect really... OK lets change it to use Unity.Mathematics.
Yeah i know - lot of hard work there >-<;; Lets run it and we should have waaaay better performance... right?
WRONG!

Its basically exactly the same as the old maths types!? WHAT GIVES!?! Actually, this is totally expected, its still all pure C# at this point...
THIS IS A REAL IMPORTANT OBSERVATION! If your still living in the Unity GameObject land - this isnt a magic bullet, its not going to change anything :( You have to be using Burst and burst only works with jobs! OK.. how do we get burst? Package Manager time again!
And we have to poke the code a bit more too, this time we have to add the [ComputeJobOptimization] attribute to the jobs we want Burst to optimize, that way when our game runs Unity will swap in the burst optimized versions
Okay! Whats the performance like now? Waahh thats way better. Doing the same million operations on a single thread now takes us a (comparitivly) tiny 3ms, and <1ms when split up over threads. Thats a massive improvement because now Unity is using SIMD instructions! ITS REAL GOOD
Actually, that multithreaded time is bad. Added up its about 9.5ms - suggesting that batch setup time is taking longer than the actual maths code! This could be improved by setting a bigger batch size when we schedule the job
We went to 157ms for a million calcualtions to 3, you can expect similar speedups with other kinds of math operations. Heres a screenshot showing performance of my physics system running under burst with the new maths types-Vector, Matrix and Quaternion calculations all speed up!
Even more impressive? in thoes profiler samples my code was *totally* being dominated by Raycast calls. I have my own very, very, very WIP raycast engine... in this example it runs in 0.26ms rather than the 10ms PhsyX runs in, again thanks to Bust and Unity.Mathematics. ITS MAGIC
p.s if you found this useful you can always show your appreciation by getting me a coffee ^^ GetLotte.Coffee Always makes a massive difference! <3
Missing some Tweet in this thread?
You can try to force a refresh.

Like this thread? Get email updates or save it to PDF!

Subscribe to Lotte 💖💝🍓🌈🐹 ➡REZZED🎮☕🍫🍩
Profile picture

Get real-time email alerts when new unrolls are available from this author!

This content may be removed anytime!

Twitter may remove this content at anytime, convert it as a PDF, save and print for later use!

Try unrolling a thread yourself!

how to unroll video

1) Follow Thread Reader App on Twitter so you can easily mention us!

2) Go to a Twitter thread (series of Tweets by the same owner) and mention us with a keyword "unroll" @threadreaderapp unroll

You can practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just three indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member and get exclusive features!

Premium member ($3.00/month or $30.00/year)

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!