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

Discussion Requesting: performance/optimizations considering the compiler

Discussion in 'Scripting' started by downfall318, Aug 6, 2023.

  1. downfall318

    downfall318

    Joined:
    Aug 2, 2023
    Posts:
    2
    I am coming back to Unity after a few year hiatus; is there a place where I can look up or is there a place where someone has a compiled list of optimizations that are non-obvious, such as (in the past definitely), and how they might have changed over the years?


    Code (CSharp):
    1. int i = 0; //faster
    2. for(;i < length; i++){}
    3.  
    4. //slower
    5. for(int j = 0;j < length; j++){}
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,557
    DO NOT OPTIMIZE "JUST BECAUSE..." If you don't have a problem, DO NOT OPTIMIZE!

    If you DO have a problem, there is only ONE way to find out. Always start by using the profiler:

    Window -> Analysis -> Profiler

    Failure to use the profiler first means you're just guessing, making a mess of your code for no good reason.

    Not only that but performance on platform A will likely be completely different than platform B. Test on the platform(s) that you care about, and test to the extent that it is worth your effort, and no more.

    https://forum.unity.com/threads/is-...ng-square-roots-in-2021.1111063/#post-7148770

    Remember that optimized code is ALWAYS harder to work with and more brittle, making subsequent feature development difficult or impossible, or incurring massive technical debt on future development.

    Notes on optimizing UnityEngine.UI setups:

    https://forum.unity.com/threads/how...form-data-into-an-array.1134520/#post-7289413

    At a minimum you want to clearly understand what performance issues you are having:

    - running too slowly?
    - loading too slowly?
    - using too much runtime memory?
    - final bundle too large?
    - too much network traffic?
    - something else?

    If you are unable to engage the profiler, then your next solution is gross guessing changes, such as "reimport all textures as 32x32 tiny textures" or "replace some complex 3D objects with cubes/capsules" to try and figure out what is bogging you down.

    Each experiment you do may give you intel about what is causing the performance issue that you identified. More importantly let you eliminate candidates for optimization. For instance if you swap out your biggest textures with 32x32 stamps and you STILL have a problem, you may be able to eliminate textures as an issue and move onto something else.

    This sort of speculative optimization assumes you're properly using source control so it takes one click to revert to the way your project was before if there is no improvement, while carefully making notes about what you have tried and more importantly what results it has had.
     
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    So what, variable names affect performance now? Is using i as the loop variable faster than j?

    If you want performance, look into these packages: Burst, Jobs, Mathematics, Collections. Specifically you can replace all Vector3 etc instances with math types, and Mathf with Mathematics.math and you get yourself a consistent speed boost just by changing habits of using the newer math types. Collections are a bit trickier due to memory management, and restricted to non-managed objects.
     
    DevDunk likes this.
  4. downfall318

    downfall318

    Joined:
    Aug 2, 2023
    Posts:
    2
    In the past the case in the OP was faster, the compiler would create code that was slightly faster if the int declaration was outside the for loop declaration, something to do with the scope being different at run time.

    edit: This is no longer the case as I had just tested this, not sure about the case in specific deployments though
     
    Last edited: Aug 6, 2023