Search Unity

Question Burst static functions VS Inline in burst code

Discussion in 'Burst' started by DevDunk, Mar 20, 2023.

  1. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,048
    I have a helper script for common math functions I use. This is static and already burst compiled.
    I did not see a huge increase in performance, but that's fine since I will convert the majority of code into jobs anyways.

    The helper functions will be used in burstcompiled jobs. I wonder if I should keep these functions the way they are, or use `MethodImplOptions.AggressiveInlining` instead, like in the math class, so burst can optimize the whole job. I originally used return float3, which was incompatible with burst, but it's easy to get return back in there

    Any thoughts on this? (if you have tips to optimize stuff like this I'm also definitely open for it, first time working with jobs and burst)

    Current code example:

    Code (CSharp):
    1. [BurstCompile]
    2.         public static void GetPointOnQuadraticBezierCurve(in float3 point1, in float3 controlPoint3, in float3 point2, float t, out float3 result)
    3.         {
    4.             // Calculate the position on the curve based on the blending factors
    5.             result = ((1 - t) * (1 - t) * point1)
    6.                 + (2 * (float)(1 - t) * t * controlPoint3)
    7.                 + (t * t * point2);
    8.         }
    (Extra: Would there be a benefit if I use float3x4 instead of 4 float3 values if my job needs 4 float3 values?)
     
    Last edited: Mar 20, 2023
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,264
    Burst can do that regardless. If you aren't seeing a big increase in performance with that Bursted static method, my guess is that there is some non-Bursted stuff that is still fairly expensive, and that once you move that into a job (or even just a bigger static method) things will go a lot faster.

    Not float3x4, but a float4x3 can be faster if used in the right situations. Make sure to profile. https://github.com/Dreaming381/Latios-Framework/blob/v0.6.6/Core/Math/simdFloat3.cs
     
  3. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,048
    Thanks!

    First point: is there a reason the math library doesn't burst compile if this the case?
    Just curious about the inner workings. Everything is now jobified and since I complete the job in Late Update I barely touch the main thread :eek:

    Maybe I'll run a test tomorrow and see if I can measure any difference.

    And thanks for the second recommendation! I'll check it out
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,264
    Burst has a dependency on Mathematics and not the other way around. Mathematics doesn't know about Burst. Same for engine code. Engine code can't use Burst because Burst is a package.
     
    DevDunk likes this.
  5. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,048
    I tried inlining some commonly used (relatively simple) static methods and I think performance is slightly faster this way. Will test more later and if needed file a bug report
     
  6. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,048
    @xoofx Sorry for the ping, but we talked a bit at the Blitz Day.
    Would love to hear if you know of any preference from a performance standpoint of using inlining vs bursted static methods.

    Extra question:
    When returning an int in a bursted method (not inlined), would it be preferable to use return or out float? This, because I saw `CantVectorizeInstructionReturnType`, as well as return not being on https://docs.unity3d.com/Packages/com.unity.burst@1.8/manual/csharp-hpc-overview.html
     
    Last edited: Mar 21, 2023