Search Unity

Caching Variables or accessing with Pointers?

Discussion in 'Entity Component System' started by alexirotu, Jan 6, 2020.

  1. alexirotu

    alexirotu

    Joined:
    Jul 20, 2015
    Posts:
    3
    Hi, so, I have a question regarding pointers, variables and performance critical code

    I have a Job where is pass on a struct via Reference, and inside the Job i do some calculations and use the variable in different parts of the calculation a few times. Its also used in other Jobs

    Following is some Pseudo Code to paint the picture

    Code (CSharp):
    1. [BurstCompile] struct PerformanceJob: IJob
    2.  
    3. [ReadOnly][NativeDisableUnsafePtrRestriction]
    4. public unsafe SomeStruct* rpSomeStructPointer;
    5. [ReadOnly]
    6. public SomeStruct rSomeStruct;
    7.  
    8.  
    9. public void Execute()
    10.  
    11. //////////////////////////////////////////////////////////////////
    12. ///1 - Pointer Reference
    13. unsafe {
    14. DoCalculation(rpSomeStructPointer->Value)
    15. DoCalculation(rpSomeStructPointer->Value)
    16. DoCalculation(rpSomeStructPointer->Value)
    17. DoCalculation(rpSomeStructPointer->Value)
    18. }
    19.  
    20. //////////////////////////////////////////////////////////////////
    21. //2 - Pointer Cache
    22. unsafe {
    23. float val = rpSomeStructPointer->Value
    24. }
    25.  
    26. DoCalculation(val)
    27. DoCalculation(val)
    28. DoCalculation(val)
    29. DoCalculation(val)
    30.  
    31. //////////////////////////////////////////////////////////////////
    32. //3 - Struct Reference
    33. DoCalculation(rSomeStruct.Value)
    34. DoCalculation(rSomeStruct.Value)
    35. DoCalculation(rSomeStruct.Value)
    36. DoCalculation(rSomeStruct.Value)
    37.  
    38. //////////////////////////////////////////////////////////////////
    39. //4 - Struct Cache
    40. float val = rSomeStruct.Value
    41. DoCalculation(val)
    42. DoCalculation(val)
    43. DoCalculation(val)
    44. DoCalculation(val)
    Which is faster, prefferable?
    I know that [4] is faster than [3] (because less Calls)? but I believe that Pointers should be faster, since I dont create a Copy like in [4] and no copies across different Jobs

    Now I believe that [1] and [2] are faster, also in regards to reuse in a different Job (since no copies will be created and the pointer works kinda like a reference type and its always one location). But is it really?

    Some additional Info: SomeStruct has different variables and it is used in different Jobs , so its not just SomeStruct.Value but also Value2 and so forth

    I'm looking for the fastest, most (memory) efficient way (will be running on a mobile platform)
     
  2. Deleted User

    Deleted User

    Guest

    Raw pointers with method calls should win and get inlined with Burst. Mono rarely inlines.
     
  3. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,267
    For such low level things with the Burst compiler, your best bet is to profile each approach with your algorithm and see what happens and also look at the Burst-generated assembly. Sometimes it just comes down to one approach producing a pattern that the compiler's optimization algorithm recognizes better.