Sometimes it seems like Unity programming is a minefield. Plenty of innocuous-looking code secretly creates garbage and eventually the GC runs and causes a frame hitch. Today’s article is about some of those less-obvious ways to create garbage.
Posts Tagged performance
It’s extremely common to see somebody ask a question about avoiding the garbage collector only to be answered with “just use a pool” as if that immediately and totally solved the problem. While pools will often keep the garbage collector at bay, they’ll also introduce a whole slew of new problems that you’ve got to deal with instead. Today’s article goes through several of these problems so you’ll be aware of the tradeoffs involved and hopefully avoid some pitfalls.
Over a year ago I wrote an article title Do Foreach Loops Create Garbage using Unity 5.2 and tested foreach
with a variety of collections: List
, Dictionary
, arrays, etc. Since then Unity has a new C# compiler and version 5.6 has been released. Is it safe to use foreach
now? Read on to find out!
Say you need to keep track of things you’ve already done, perhaps to avoid doing them again. What’s the fastest way to do that? HashSet<T>
seems like a natural fit, so you might choose that without a second thought. But is it faster than similar collections like Hashtable
and Dictionary<TKey, TValue>
? Today’s article puts all three to the test to see which one can insert elements, check for containment, and remove elements the quickest. Read on for the surprising results!
C# has built-in events and they work fine in Unity projects. End of story, right? Not so fast! Have you ever wondered why the Unity API doesn’t have any C# events in it? Or why Unity made their own UnityEvent
class for UGUI? Maybe there are some valid reasons to avoid C#’s events. Today’s article discusses an alternative with some serious upsides. Read on to learn more!
By request, today’s article follows up on my Unity Function Performance article from a year and a half ago using Unity 5.0. It adds on GameObject.SendMessage
and virtual
functions to get a more complete picture of how various function calls in Unity perform. Of course it runs these tests using Unity 5.4 to see if there have been any changes in the engine. Read on for the results!
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!
File I/O can be a major performance bottleneck for many apps. It’s all too easy to read files in a way that is massively inefficient. Classes like FileStream
and BinaryReader
make it really easy to write super slow code. Today’s article explores why this happens and what can be done about it. Read on to learn more!
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!