CDB: A JSON Alternative

Tags: , ,

JSON is incredibly bloated, but what do you use instead? Many games have some huge configuration file with lots of data about how the game should be run. Think of the items in a shop or the layout of a saga map and you’ll get the picture. This is often a JSON file that will take forever to parse, hog up a bunch of memory, and create a ton of garbage for the GC to collect. Enter CDB: the Constant Database. Unlike other databases, CDB is a simple, read-only, key-value store that’s been around over 20 years! Today’s article introduces the format and provides a one-file script you can drop into your projects and start gaining the many advantages that CDB has to offer.

Read the rest of this article »

No Comments

The Problems with Object Pools

Tags: , ,

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.

Read the rest of this article »

4 Comments

Loop Performance: Part 5 (New Compiler, IL2CPP)

Now that Unity has a new compiler that makes foreach loops not create garbage with List<T>, it’s time to re-test all the kinds of loops to see if anything’s changed. This article is the first in the series to test on a real Android device using IL2CPP, so these numbers should be much more accurate for most games. Read on for the results!

Read the rest of this article »

No Comments

Do Foreach Loops Still Create Garbage?

Tags: , , , ,

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!

Read the rest of this article »

6 Comments

Which Hash Set is Fastest?

Tags: , , ,

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!

Read the rest of this article »

2 Comments

Closures Without the GC

Tags: , , ,

Closures allow you to save the local variables of a function and access them later in a callback. Think of how lambdas can access the local variables of the function they’re declared in, even though the lambda itself is another function. Unfortunately, creating a lambda like this creates garbage for the GC to collect and you have no control over that process. Today’s article introduces an alternative that allows you to take control over the GC and still use nice, type-safe closures. Read on to learn how!

Read the rest of this article »

No Comments

A Simple Formula Evaluator

Tags: , ,

As you know, making your game data-driven is good idea. So you make all kinds of configuration files with numbers in them: starting health, damage amount, XP reward, etc. But what do you do when those values aren’t just constants? Today’s article presents a little one-file class that you can use to evaluate simple formulas instead of just constants. What if your data for “XP reward” wasn’t just “100” but instead a formula like “10+MMRDifference*50”? That’s a great tool you can hand to game designers to data-drive your game. Read on to learn how!

Read the rest of this article »

3 Comments

Faster Memory Allocation with Memory Pools

Tags: , ,

One of the advantages we get when we use unmanaged memory is a huge increase in flexibility. For example, we can easily allocate a whole array of objects at once instead of one-at-a-time when we new a class instance. We can also create a memory pool with one allocation then divide it up ourselves. It turns out that can really speed up memory allocation and, at the same time, actually reduce memory fragmentation on top of the fragmentation we’re avoiding by not creating any garbage for the GC. Today’s article shows you how memory pools work and provides an implementation of one you can use in your own projects!

Read the rest of this article »

4 Comments

Delegates and Garbage Creation

Tags: , , , ,

Two facts are at odds in Unity programming. First, delegates like Action, Func, and EventHandler are extremely common with or without events. Second, the garbage collector is a huge source of CPU spikes and memory fragmentation in our games. Why are these facts at odds? Because code that uses delegates is almost always written in a way that creates garbage. It’s an extremely easy trap to fall into, but this article will show you how to get out of it!

Read the rest of this article »

No Comments

An Alternative to Events

Tags: , , ,

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!

Read the rest of this article »

4 Comments