Native List vs SynchronizedCollection performance

Please forgive me if this is the wrong form to post about this, as I was not sure where would it be the best. I assumed that because Native List is actively used in Burst or job system, I thought this forum may be the best place to discuss it.

I was using SynchronizedCollection for multithreaded list usage, and recently I had to change it to something else so I tried the ConcurrentDictionary and I found it to be around 2~3 times slower than the SynchronizedCollection for simply adding and reading. So I looked around for alternatives. Please note that I am not using c# job to do my threaded operation but my own thread management.

And I remembered that Unity had its own NativeList for apprently thread safe collection so I gave a go at that.

The result is that it is working, but it was also around 2~2.5 times slower than the SynchronizedCollection for exactly the same operations at least in the Unity Editor. For Build, it was almost the same for the first time, and then it got bit faster.

I am wondering, if anyone had go at using NativeList and SynchronizedCollection before and compared them?

Not sure what’s going on under the hood, but why (if it is indeed so) slower than SynchronizedCollection? I am also guessing that performance for each of them could be different if I use them in single-main thread only?

Or is there any other alternatives for thread safe collection? (list type preferred)

I found :
SynchronizedCollection
ConcurrentDictionary
NativeList

The best place is the DOTS forums.

I can, of course, move your post if you wish.

Oh Yes please do! Thanks!

1 Like

NativeList isn’t naturally thread safe. There is a NativeList.ParallelWriter version which provides thread safe writing but you can’t safely read while writing. That said, NativeList.ParallelWriter is not particularly fast. Note these containers are generally meant to run in Burst jobs and outside of this often run quite a bit slower than managed counterparts.

There are other options though, for example NativeStream provides faster ParallelWrite and ParallelRead (though not at the same time).

1 Like