Iterating Multiple Lists In Order

Suppose you have two sorted lists, one holding A elements and another holding B. Both types have a common field and you want to iterate over both lists according to that common field. For example, if you have [1, 3, 5] and [2, 4, 6] then you want to get the A with 1 then the B with 2 then the A with 3 and so on. How do you do this at all? How do you do it efficiently? Read on for my answers to the puzzle!

Read the rest of this article »

No Comments

An Interruptible YieldInstruction

Tags: , , ,

Coroutines are a fundamental building block of Unity scripting. In 5.3, we got a new class to make them more powerful: CustomYieldInstruction. Today we’ll look at it and see if we can make an arbitrarily-interruptible YieldInstruction so our coroutines can abort the things they yield. Read on to see how and to compare against the old 5.2 way!

Read the rest of this article »

No Comments

LINQ Extensions: Part 1

Tags: , ,

One of C#’s most unique features is its SQL-style LINQ syntax. It’s a powerful and expressive way to treat data structures like a database and perform all kinds of actions on them. LINQ can also be used without the SQL-style syntax via various extension methods of IEnumerable<T> defined in the System.Linq namespace. Due to C#’s extension method feature, we’re free to add on our own LINQ-style functions to extend its power. Today’s article introduces some extension methods to do just that. Read on for the source code and power up your LINQ!

Read the rest of this article »

No Comments

If-Else is Really Expensive

Tags: , , ,

Sometimes a tiny amount of code costs a huge amount of performance. This is especially true of built-in language features, which many programmers assume to be extremely cheap if not free. Today we’ll look at if and see just how much performance it can cost your app. Read on to see!

Read the rest of this article »

5 Comments

Better CPU Caching with Structs

Tags: , , , ,

While little utilized, C#’s struct type can come in really handy sometimes. Today’s article shows how to use it to get a lot more mileage out of modern CPUs’ caches to really boost your app’s performance. Read on for some quick tips!

Read the rest of this article »

2 Comments

FastList: A Solution to List’s GC Problems?

Tags: ,

As we know, foreach loops create garbage when used with a List<T>. This happens the first time you iterate over one and it happens every time thereafter. A comment on that article shared a link to a class called FastList that was written expressly to solve the GC issue. Does it? How does its performance compare to plain old List? Today’s article puts it to the test to find out!

Read the rest of this article »

5 Comments

Updater: An Easy Way to Get Updates Without Inheriting MonoBehaviour

Tags: , , ,

At first glance an Updater class seems unnecessary in Unity. All you have to do is inherit from MonoBehaviour and add an Update function. But what if you don’t want to inherit from MonoBehaviour? Enter Updater, an easy way to still get an update event and cut your dependency on MonoBehaviour. This works great for code in DLLs, “pure code” projects, or just projects that don’t want to put everything into MonoBehaviours. Read on for the source code and how to use it!

Read the rest of this article »

1 Comment

How Often Should You Force Garbage Collection?

Tags: , ,

One typical piece of advice for dealing with the slowness of Unity’s garbage collector is to periodically force a garbage collection, such as every 30 frames. Even Unity advises this. The idea is that you’ll spread out the garbage collection work across many frames rather than having a big spike that causes your frame rate to stutter. But the question remains- what’s the best rate to force the GC? Today’s article tries out various intervals to see which is best. Read on for the results!

Read the rest of this article »

No Comments

Error Handling Performance

Tags: , , ,

Having just concluded the series on handling errors without using exceptions, now’s a good time to check up on an assertion I made in the first part: exceptions are slow. A good question to ask is “compared to what?” So let’s put them up against good old error codes and the new Either type I presented in the series. Which is fastest when there is no error? Which is fastest when there is an error? Read on to find out!

Read the rest of this article »

No Comments

Handling Errors Without Exceptions: Part 2

Tags: , , , ,

Last week’s article introduced the Either class as an alternative to exceptions that makes it easy for functions to declare their error results in addition to their success results and for callers of those functions to handle both results. Today we’ll go further by linking together multiple functions to handle all the error cases almost transparently. Read on to learn how to make the most out of Either!

Read the rest of this article »

No Comments