Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Burst is amazing !! :)

Discussion in 'Entity Component System' started by CodeSmile, May 6, 2022.

  1. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,191
    Just wanted to say this out loud! :)

    Burst compiler continues to baffle me!
    And of course Unity.Mathematics and Unity.Collections which play a big part!

    I have Jobs in unit tests with the Unity.PerformanceTesting package and keep switching Burst Compiler on and off and still can't believe how much of an effect it has.

    Case in point: a job that calculates vertex positions for axis oriented quads. This is the IJobParallelForBurstSchedulable code:
    Code (CSharp):
    1.         public void Execute(int quadIndex)
    2.         {
    3.             var quad = _quads[quadIndex];
    4.             var position = quad.Position;
    5.             var orientation = quad.Orientation;
    6.             var scale = quad.Scale;
    7.  
    8.             var vertexAttributes = new VertexPositionNormal();
    9.             vertexAttributes.Normal = _orientedNormals[(int)orientation];
    10.      
    11.             var vertexPositions = _orientedPositions[(int)orientation];
    12.             var pos0 = position + vertexPositions.LeftDown * scale;
    13.             var pos1 = position + vertexPositions.RightDown * scale;
    14.             var pos2 = position + vertexPositions.LeftUp * scale;
    15.             var pos3 = position + vertexPositions.RightUp * scale;
    16.      
    17.             var vertexIndex = quadIndex * 4;
    18.             vertexAttributes.Position = pos0;
    19.             _vertexData[vertexIndex++] = vertexAttributes;
    20.             vertexAttributes.Position = pos1;
    21.             _vertexData[vertexIndex++] = vertexAttributes;
    22.             vertexAttributes.Position = pos2;
    23.             _vertexData[vertexIndex++] = vertexAttributes;
    24.             vertexAttributes.Position = pos3;
    25.             _vertexData[vertexIndex] = vertexAttributes;
    26.         }
    27.  
    For 100.000 Quads (436.880 vertices) it takes about 50 ms without Burst:
    upload_2022-5-6_11-42-56.png

    Then I turn on Burst and ... wooooow :eek: ... wuuuuut :D ... it's down to 0.15 ms !!
    upload_2022-5-6_11-42-18.png

    That's 339 times faster! :eek::D:cool::eek::D:cool::eek::D:cool::eek::D:cool::eek::D:cool::eek::D:cool:
    Ryzen 9 3900 (12-core)

    Though of course you have to be super-careful in how you write your code. But I find it highly rewarding just trying to squeeze out some more performance and I end up with cleaner code, too.
     
    Last edited: May 6, 2022
  2. Singtaa

    Singtaa

    Joined:
    Dec 14, 2010
    Posts:
    485
    Yup, it's a very nice feeling to write some thing in C# and have it reach the theoretical limit of your CPU power. And because of the naming in Unity.Mathematics, I can often just copy&paste burst-compatible code into a Compute shader (you know... when you need to reach that full potential on your GPU too) and it'll just work with very minimal refactoring.
     
    apkdev and JesOb like this.
  3. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,064
    This is why I like the Mathematics package while I don’t have Burst installed.

    Because if I really care about performance I reach for a compute shader instead of Burst.
     
    Anthiese likes this.
  4. vectorized-runner

    vectorized-runner

    Joined:
    Jan 22, 2018
    Posts:
    383
    Yeah Burst is really good but also Editor compiler is awful... I'd rather compare it with latest JIT compiler.

    Also the Job code was only meant to run with Burst (unless debugging) so it would be even slower without Burst on

    Here is a more fair comparison: https://github.com/nxrighthere/BurstBenchmarks, you can see that it's never that much fast vs. the latest compilers
     
    Last edited: May 7, 2022
    jdtec likes this.
  5. JesOb

    JesOb

    Joined:
    Sep 3, 2012
    Posts:
    1,081
    No it is not fair comparison
    It is not burstable/burst aware code

    This tests show that with arbitrary code you will have performance comparable with c++ compilers and latest .net is very close sometimes too

    And those test very very old

    more recent but very old too is this: upload_2022-5-7_13-28-44.png
     
    Last edited: May 7, 2022
    MNNoxMortem likes this.
  6. vectorized-runner

    vectorized-runner

    Joined:
    Jan 22, 2018
    Posts:
    383
    I don't know what you mean to say, probably if they updated the BurstBenchmarks results wouldn't be much different, other compilers are improving too. I'm just saying Burst isn't that much faster than modern compilers, sometimes even slower.

    It's pretty amazing that it's at the same level with c compilers though, since we're writing c#
     
  7. Enzi

    Enzi

    Joined:
    Jan 28, 2013
    Posts:
    910
    GCC is the gold standard and Burst is pretty close. I don't know what the argument is here? That it's not faster than GCC? Having C performance in C# is pretty awesome. No JIT or .net core comes close right now.
     
    Anthiese likes this.
  8. vectorized-runner

    vectorized-runner

    Joined:
    Jan 22, 2018
    Posts:
    383
    I'm just saying stop kidding ourselves... Burst is really good but most of the speed up is because Editor compiler is garbage. That's it, the whole point
     
    s_schoener likes this.
  9. calabi

    calabi

    Joined:
    Oct 29, 2009
    Posts:
    232
    I kind of confused how is at least 10 to almost 100x better performance increase, just for enabling something, which I've seen in my own projects not that good. How else would I get that performance, build my own engine and program in a different language. Unity was just that bad in general before, I don't see how that is a credible statement.
     
  10. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,191
    Editor compiler is simply the C# compiler. C# was never meant to be high-performance at runtime, but fast to compile code (try compiling the same code with C++ and you'll be waiting....).

    Burst CAN be that fast. Just enabling it and placing the attribute on a method won't work. In combination with jobs and careful data structures (ie avoid branching) the speed increase is enormous because the compiler can make use of SSE/AVX extensions of modern CPUs. It makes the difference between copying byte by byte vs memcpy.
     
    Anthiese likes this.
  11. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,184
    Burst is not a general purpose compiler but a highly specialized one that is designed to optimize game code. The examples in that repository are too general purpose to properly benchmark Burst and therefore will lead to biased results. If you want to see real results you need to compile code intended for games not Fibonacci and Mandlebrot.
     
    Last edited: May 14, 2022
    JesOb and Anthiese like this.
  12. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,078
    It's very interesting!
    How do you do exactly?
     
  13. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    1,131
    CTRL+C & CTRL+V
     
  14. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,078
    I was asking how to do it, like from where you copy the burst compiled C# code that can be used as HLSL for compute shaders.
    btw, I thought you were doing it with the mouse right bouton :)
     
  15. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    1,131
    :) sorry for the silly post, just could not help it. I think this was more related to math package and that you literally can copy & past with little modification…
     
    Opeth001 likes this.
  16. apkdev

    apkdev

    Joined:
    Dec 12, 2015
    Posts:
    263
    You copy the C# source code. If you're using Unity.Mathematics, a lot of the types (float3, float4, etc) and built-in functions (lerp, normalize, dot, saturate, etc) are named to match their shader equivalents.

    Here's the example from readme, it (the math part) pretty much compiles as HLSL (maybe except
    var
    ):
    Code (CSharp):
    1. using static Unity.Mathematics.math;
    2. namespace MyNamespace
    3. {
    4.     using Unity.Mathematics;
    5.  
    6.     ...
    7.     var v1 = float3(1,2,3);
    8.     var v2 = float3(4,5,6);
    9.     v1 = normalize(v1);
    10.     v2 = normalize(v2);
    11.     var v3 = dot(v1, v2);
    12.     ...
    13. }
    Also see this FAQ for some interesting points: https://github.com/Unity-Technologies/Unity.Mathematics#frequently-asked-question
     
    Opeth001 likes this.
  17. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,078
    thanks!
     
  18. Singtaa

    Singtaa

    Joined:
    Dec 14, 2010
    Posts:
    485
    Yes, right. With Unity.Mathematics' naming, you can re-use your code easily across different places:

    C# land <-> Computer Shader (HLSL) <-> Shadertoy (GLSL)