In last week’s tips for using collections like List<T>
, we saw how struct types are sometimes boxed resulting in GC allocations. This week we’ll see how to avoid boxing and learn some of the clever tricks that .NET collection types use to make this possible.
Posts Tagged list
Collection types like List<T>
and Dictionary<TKey, TValue>
are fundamental tools in C#. Sadly, I keep seeing the same misuses of them in codebase after codebase. Today we’ll look at the top 5 problems and learn how to easily avoid them!
Sometimes you just want a small array without the heap allocations and GC. Existing solutions like stackalloc
require unsafe
code, don’t allow for dynamic growth, and don’t support foreach
loops. So today we’ll design and build a code generator that puts a new tool in your toolbox!
NativeArray<T>
is great, but very limited in functionality. We can fix this surprisingly easily! Today we revive a two year old series that created the iterator project. Iterators are like a no-GC version of IEnumerable<T>
and LINQ which have a lot of power but only support managed arrays (T[]
) and List<T>
. Today we’ll add support for NativeArray<T>
and inherit support for the same functionality. We’ll also spruce up the project with proper unit tests, assembly definitions, and runtime tests to confirm that zero garbage is created. Read on to see how this was done and how to use iterators with NativeArray<T>
.
We’ve seen how NativeArray works, but what if we want more kinds of native collections? Unity 2018.1 only has that one, but you can make your own! Today’s article shows exactly how to do that.
There are many permutations of loops we can write, but what do they compile to? We should know the consequences of using an array versus a List<T>
, for
versus foreach
, caching Length
, and other factors. So today’s article dives into the C++ code that IL2CPP outputs when we write these various types of loops to examine the differences. We’ll even go further and look at the ARM assembly that the C++ compiles to and really find out how much overhead our choices are costing us.
Every time I see for (var i = 0; i < array.Length; ++i)
I wonder if accessing that Length
property is slow. Should I cache it? It's comforting to know that for (int i = 0, len = array.Length; i < len; ++i)
is only dealing with local variables except on the first loop. Local variables must be faster, right? Likewise, I wonder the same thing about List<T>.Count
. I finally got around to running a test to see if caching these length properties makes any performance difference. The answers might surprise you!
Today we’ll wrap up the iterator series by finishing up porting C++’s <algorithm> header. We end up with a library of functions for common LINQ-style algorithms but without any of the garbage creation that slows our games down. Read on for the source and examples!
We’re nearing the end of the series to build a no-garbage replacement for System.Linq
. Today we tackle functions that work on already-sorted ranges and functions that work on ranges that are in heap order. These include common set operations like “union” and “intersection”. Read on to see how to use them and for the updated library that you can use to eliminate your garbage creation!
This week we continue with iterators to get the functionality of IEnumerable
without the nasty garbage creation. This week the little iterator library gets support for sorting and binary searching. Read on for the details!