Search Unity

Help me understanding why Burst is generating this code

Discussion in 'Burst' started by sebas77, Jan 13, 2020.

  1. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,642
    Code (CSharp):
    1. [BurstCompile]
    2. struct JobWithGenericJob<T> : IJob where T : unmanaged, ITest
    3. {
    4.     [NativeDisableUnsafePtrRestriction]
    5.     readonly unsafe T* _test;
    6.     public JobWithGenericJob(IntPtr test)
    7.     {
    8.         unsafe
    9.         {
    10.              _test = (T*) test;
    11.         }
    12.     }
    13.     public void Execute()
    14.     {
    15.     }
    16. }
    if Execute is:

    Code (CSharp):
    1.  public void Execute()
    2.     {
    3.         unsafe
    4.         {
    5.             var test = _test;
    6.  
    7.             for (int i = 0; i < 10_000_000; ++i)
    8.                 test[i].SetValue(i * i);
    9.         }
    10.     }
    the result is:

    upload_2020-1-13_18-10-57.png

    however if execute is this:

    Code (CSharp):
    1.  public void Execute()
    2.     {
    3.         unsafe
    4.         {
    5.             for (int i = 0; i < 10_000_000; ++i)
    6.                 _test[i].SetValue(i * i);
    7.         }
    8.     }
    the result is this:

    upload_2020-1-13_18-11-42.png

    can someone please explain the reason of the difference?
     
  2. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,642
    if I write this:

    Code (CSharp):
    1. struct Test : ITest
    2. {
    3.     public int _value;
    4.  
    5.     public void SetValue(int value)
    6.     {
    7.         _value = value;
    8.     }
    9. }
    10.      
    11. [BurstCompile]
    12. struct JobWithGeneric : IJob
    13. {
    14.      NativeArray<Test> _test;
    15.        
    16.      public JobWithGeneric(NativeArray<Test> test)
    17.      {
    18.          _test = test;
    19.      }
    20.        
    21.      public void Execute()
    22.      {
    23.          for (int i = 0; i < 10_000_000; ++i) _test[i].SetValue((int) (i * i));
    24.      }
    25. }

    Burst seems to be empty?

    upload_2020-1-13_18-44-13.png
     
  3. elcionap

    elcionap

    Joined:
    Jan 11, 2016
    Posts:
    138
    What happens if you change to this:
    Code (CSharp):
    1.  
    2. public void Execute() {
    3.     for (int i = 0; i < 10_000_000; ++i) {
    4.         var test = _test[i];
    5.         test.SetValue((int)( i * i));
    6.         _test[i] = test;
    7.     }
    8. }
    9.  
    Your code is the same as the above without the last line. So unless the struct does something unconventional (like settings an static variable or working with some pointer, etc) your could will go nowhere and the optimization will discard everything.

    []'s
     
  4. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,642
    right, I forgot that NativeArray returns a copy, I wonder if Burst should warn me about it. What about the first post?