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. Dismiss Notice

Compilator question

Discussion in 'Burst' started by Alex1337rus, Feb 3, 2022.

  1. Alex1337rus

    Alex1337rus

    Joined:
    Mar 30, 2016
    Posts:
    37
    Will all if expressions be removed when compiling? And will this code become equivalent to just calling Method1?

    Code (CSharp):
    1.  
    2. [BurstCompile]
    3. public struct TestJob : IJob
    4. {
    5.     public void TestConst(int k)
    6.     {
    7.         if (k == 2)
    8.         {
    9.             Method1();
    10.         }
    11.  
    12.         if (k == 5)
    13.         {
    14.             Method2();
    15.         }
    16.  
    17.         if (k == 10)
    18.         {
    19.             Method3();
    20.         }
    21.  
    22.     }
    23.  
    24.     public void Execute()
    25.     {
    26.         TestConst(2);
    27.     }
    28. }
    29.  
     
  2. Yury-Habets

    Yury-Habets

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    1,165
    Yes. Execute() method will just have a call into Method1().

    You can check Burst Inspector https://docs.unity3d.com/Packages/com.unity.burst@1.6/manual/docs/QuickStart.html#burst-inspector for the output assembly to make sure the output matches your expectations.

    There's also IsConstantExpression() intrinsic to make sure that something is reduced to a constant during compile time in your code : https://docs.unity3d.com/Packages/c...ptimizationGuidelines.html#constant-intrinsic, but it's not for your example because 2 _is_ a constant. :)
     
    Alex1337rus likes this.
  3. Alex1337rus

    Alex1337rus

    Joined:
    Mar 30, 2016
    Posts:
    37