July 03 2023

High Performance Collections in Unity C#

Still using native C# collections like List, Dictionary, HashSet?

Ok, that’s fine, I won’t judge you. In fact, I won’t tell you to stop using them, as they still have their place in game development.

But most of the time you’ll want to use the new goodies coming from this package called com.unity.collections. You’ll be surprised to see how many collections you now have access to. Each one fitting a different use case you’ll find during development.

These collections are so specific, that they’ll give you substantial performance gains (10x is not unheard of) that you can use to level up your project in other areas, such as better graphics, more realistic AI and so on. They also incur in no GC allocations btw.

This is pure gold because some collections support multithreading. Hi job system. Hi burst compiler.

Here are some of the collections you can start using today:

  • NativeStream
  • UnsafeAtomicCounter32
  • NativeArray
  • FixedList
  • FixedString
  • NativeParallelHashMap
  • FixedText

A word of caution. These collections are unmanaged, so you need to be taking care of the allocation and disposal of your collections. Easy once you have done it a couple of times.

If you want to dive in deeper in high-performance collections so you can start using them efficiently as of today, I cover this topic pretty much in depth in the Unity Performance Taskforce, module “2023.05”. This module also includes examples of these collections together with the job system and burst compiler so you can cover pretty much every use case you’ll find in your career.

Using high-performance collections to distort mesh vertices in run-time

Here is the link to the collections documentation. It includes updated performance benchmarks that compare against the native C# collections for your information.

Have a look and make a call!

Benchmark of high-performance collections in Unity

As of me, I’ll use high-performance collections in the ratio of approximately 8 out of 10 cases. And whenever I can’t, then I stick to the slower (yet more universal) standalone C# data structures.

If your studio could use some clarity going forward in your project, let’s have a chat.

Book a free call with my team through this link or shoot me an email.

The Gamedev Guru Logo

Performance Labs SL
Paseo de la Castellana 194, Ground Floor B
28046 Madrid, Spain

This website is not sponsored by or affiliated with Facebook, Unity Technologies, Gamedev.net or Gamasutra.

The content you find here is based on my own opinions. Use this information at your own risk.
Some icons provided by Icons8