Posts Tagged iterator

The Many Types and Dangers of Callbacks

Tags: , , , , ,

Callbacks are a mainstay of the real-time games and apps we build in Unity. We’re constantly writing asynchronous code for every operation from walking a character to a destination to making a web call. It’s really convenient for these functions to “call back” and report their status so we know how the operation is going. Unfortunately there are also a lot of dangers that come along with this. Today we’ll look into the surprisingly large number of ways you can “call back” in C# and some of the ways you can get burned doing so.

Read the rest of this article »

2 Comments

Enumerables Without the Garbage: Part 7

Tags: , , , , , ,

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!

Read the rest of this article »

No Comments

Enumerables Without the Garbage: Part 6

Tags: , , , , , ,

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!

Read the rest of this article »

No Comments

Enumerables Without the Garbage: Part 5

Tags: , , , , , ,

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!

Read the rest of this article »

2 Comments

Enumerables Without the Garbage: Part 4

Tags: , , , , , ,

Back from a brief break, we pick up this week by finishing up the “modifying sequence operations” with some gems like RandomShuffle and go through the “partitions” category with functions like Partition and IsPartitioned. These are all solid algorithms with a lot of potential uses, so read on to see how to use them with iterators and for the source code that implements them!

Read the rest of this article »

No Comments

Enumerables Without the Garbage: Part 3

Tags: , , , , , ,

Continuing the series this week we’ll delve into the iterator functions that modify the sequence. This includes handy tools like Copy, SwapRanges, and Transform. Of course this is all done without creating any garbage! Read on to see how and for the full source code.

Read the rest of this article »

No Comments

Enumerables Without the Garbage: Part 2

Tags: , , , , , ,

Last week’s article introduced the concept of iterators as an alternative to the GC-heavy IEnumerable. Today’s article expands the iterator library to include a bunch of more functions to make it useful. Think of these like the extension functions in System.Linq: Any, IndexOf, etc. These have all been tailored to iterators and none of them will create any garbage whatsoever.

Read the rest of this article »

No Comments

Enumerables Without the Garbage: Part 1

Tags: , , , , ,

In C#, just about everything is an IEnumerable. Since LINQ syntax, foreach loops, and the System.Linq namespace are all designed to work with IEnumerable, you’ve got lots of tools to use. Unfortunately, the core of IEnumerable is the GetEnumerator function which usually creates garbage and eventually causes memory fragmentation and GC framerate spikes. Do we simply stop using all of these nice tools? Normally the answer is “yes”, but today’s article shows you another way.

Read the rest of this article »

4 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 »

1 Comment

Too Many Coroutines: A Queue Solution

Tags: , , ,

Unity’s coroutine support is great. So great that it’s easy to go overboard and end up with too many of them. That could be for any number of reasons. Perhaps the coroutines are using too much memory or have too many files open at once. In any case, you’ll need to find a way to limit how many are running at a single time. Today’s article introduces a solution to the problem that queues coroutines so you never have too many running. Read on to learn about the solution and for the class that implements it.

Read the rest of this article »

19 Comments