Loop Performance: Part 4

Tags: , , , , ,

Today’s article takes a break from the iterator series to investigate an interesting anomaly with the List.ForEach function: it’s surprisingly fast! So fast that it’s actually competitive with regular old for, foreach, and while functions. How can it be so fast when it has to call a delegate that you pass it for every single loop iteration? Read on for to find out!

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 »

No Comments

Even More Ways Structs Create Garbage

Tags: , , ,

Last time we saw that calling a non-default constructor on a generic struct (MyStruct<T>) causes garbage creation. That garbage creation is subtle, but can have big impacts on framerate and memory usage. Today we’ll see two more ways that structs can create garbage and hopefully avoid some pitfalls. Read on to find out how!

Read the rest of this article »

2 Comments

Loop Performance: For vs. Foreach vs. While

Tags: , , ,

Today’s article expands on the previous loop test article to find out which kind of loop is truly fastest. Read on to find out!

Read the rest of this article »

3 Comments

Another Way Structs Create Garbage

Tags: , ,

As Unity programmers, the garbage collector is sadly our enemy. C# structs are often a great tool to avoid allocating objects that need to later be garbage-collected. This isn’t always the case though. Sometimes even a struct can allocate garbage. Today’s article points out one of those ways so you won’t be fooled into thinking you’ve stopped the GC just because you’re using a struct. Read on to learn more!

Read the rest of this article »

2 Comments

Hiding Unity Library Internals

Tags: , , ,

When writing code for a library, there is invariably some of it you want to hide from the users of the library. You want to keep the public API clean, but Unity makes this tough. Today’s article discusses a strategy for laying out your code so that users of the library aren’t burdened by classes, functions, and properties that they don’t need to know about. Read on to see how!

Read the rest of this article »

No Comments

Adding the const Keyword to C#

Tags: ,

Today’s article is not about the const keyword that C# already has. It’s about the const keyword that C++ has and how we can approximate it in C# to make our code safer. It’s a really powerful tool that’s often the default for C++ programmers, but we can take advantage of a similar strategy in C#. Read on to learn how!

Read the rest of this article »

3 Comments

The Global Variables Everybody Uses

Every programmer has heard that global variables are bad practice and should be avoided in favor of other techniques. Yet you’d be surprised how often global and pseudo-global variables are used. Today’s article reveals some of these usages and present some alternative ways to structure your code so it’s easier to read, write, and maintain. Read on to learn how!

Read the rest of this article »

4 Comments