#Performance of first initialized NativeList

1 messages · Page 1 of 1 (latest)

shut meadow
#

Hello,
I'm dealing with the initial performance spike when the scene loads. After using Profiler, I found that the first initialization of NativeList is more performance consuming than the following initializations of NativeLists.

For testing I created the following basic script:

using UnityEngine;
using Unity.Collections;
using UnityEngine.Profiling;

public class testScript : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Profiler.BeginSample("test list 1");
        NativeList<int> list1 = new NativeList<int>(10, Allocator.Temp);
        Profiler.EndSample();
        list1.Dispose();

        Profiler.BeginSample("test list 2");
        NativeList<int> list2 = new NativeList<int>(10, Allocator.Temp);
        Profiler.EndSample();
        list2.Dispose();
    }
}

Results for list1:

#

Results for list2:

#

Why is the initialization of the first NativeList so different in performance and many times larger than the initialization of the following NativeLists?

Am I doing something wrong? Can the first initialization of the NativeList be optimized?

Thank you 🙂

P.S: I just tried this problem in a new empty project and I have the same problem. I'm using version 2022.2.2f1

umbral wyvern
#

Have you tested in a build? Probably just the jitter.

#

Typically when benchmarking code you 'warm up' by ignoring the first iteration.

#

Unless you have aot compiled.

shut meadow
#

@umbral wyvern thank you for answer and informations 😉 This profiler data are from editor profiling. (I found it by accident when I was profiling new part of the code)

I am creating mobile game and for some time I have problem with long scene loading after Unity logo disappears. On old devices it is +-20 seconds and on new devices +- 4 seconds. So when I found this performance spike in Start function I wanted to reduce it.

shut meadow
umbral wyvern
#

@shut meadow when profiling this monobehavior code from the editor you are paying the cost of the just-in-time compilation when it hits new code for the first time. This is a common cause of people seeing big spikes on "first time" runs. It has nothing to do with native containers.

shut meadow
#

@umbral wyvern Thank you very much for the explanation, I appreciate it. Have a nice day and good luck in development ! 😉

umbral wyvern
#

You're welcome. To confirm that it's not a problem for your build you can profile your build by attaching the profiler to it.

#

You can use the auto attach profiler option in the build settings to make sure you catch your start code.