Boxing Day

Sugar, sugar

Suppose you have a list of items you want to iterate over and do something for each item in C#. Also suppose you want to avoid any heap allocations while doing so, therefore fancy list extensions using lambda expressions are out of question. So you write something like this:

Which is syntactic sugar for:

Which, in turn, is syntactic sugar for:

Sweet, sweet syntactic sugar! Profiling confirms that that last piece of code generates zero GC alloc, if list is of type System.Collections.Generic.List<T>. So all is well, right? Wrong! Both the foreach loop and the second version with explicit using block generate 48 bytes of garbage. So why is that?

Boxing

There is a small but important lie in the code transformations above: the var keyword. The only reason why that third snippet generates zero garbage is because List<T> implements a custom enumerator which is a struct. Structs are stack allocated and gets passed by value, meaning they are copied every time they get passed around as a function parameter or a return value. So zero heap allocation.

But the foreach pattern does not know what it gets. It expects an IEnumerator<T> out of the GetEnumerator() call. And even though List<T>.Enumerator implements IEnumerator<T> a boxing conversion happens as soon as you write something like this:

Why? I don’t know. Bug, oversight, or feature? I don’t know. Does it happen for all versions of .NET? I don’t know. However, I do know that it happens in all versions of Unity released to this date, and that is bad enough for me.

Also note that the same behavior is true for the using block which expects an IDisposable. Additionally a using block also emits a null check before disposing of the disposable, which of course doesn’t make any sense for a struct either.

So bottom line neither the foreach statement nor the using block can take advantage of having a struct instead of a class getting passed to them.

Workaround

I tend to use a plain T[] array instead of a List<T> whenever possible. If I have to use a list, I tend to use old school indexer loops to iterate over them:

To iterate over collections like HashSet<T> or Dictionary<K,V> I use the following pattern:

It isn’t quite as succinct as the foreach loop but almost. The for loop syntax with empty increment statement might feel weird at first. The worst part is that the code never disposes of the enumerator. Therefore it’s borderline dangerous or simply wrong. But then again, as far as I know, for these collections there is nothing to dispose of anyway.

Remarks

C# compiles an iterator block using yield statements into an inner class implementing IEnumerator. Jon Skeet writes about iterator blocks in great detail. Note that it generates an inner class, not an inner struct! Therefore there is no further cost for boxing involved but GetEnumerator() itself already allocates memory on the heap.

Why did they choose to implement it like this? I don’t know. But the implementation does adhere to the design guidelines for choosing between class and struct. More specifically the enumerator implementing the iterator block neither is immutable, nor necessarily smaller than 16 bytes, nor logically represents a single value, and most likely it will be used in a pattern requiring boxing anyway.

Funny though that they chose to implement collection enumerators as structs, since those violate at least two or three of the four characteristics as well…

Language wars

Anecdote

Recently I had to brush up on my C++ knowledge for a job interview. I have been coding in C++ for maybe fifteen years now but for the last three years I’ve spend most of my programming time doing C# instead because it pays my bills. So I decided to get a quick overview of the fundamental syntactic, idiomatic, and ideological differences between C++ and C# to restore my memories.

I searched for C++ vs. C#. My bad. I should have known better and I guess I even did but hoped for some helpful results anyway. Instead I was presented lots of opinionated and subjective attempts to answer ill-fated questions about performance and features. I will stop right here because I couldn’t say what happens when you ask these kinds of questions any better than Eric Lippert, Jeff Atwood, and Steve Yegge. In fact you should stop reading my blog now and read theirs instead.

Language matters?

Don’t ask which is better. Ask what is the right tool for the job. In the end you should base the choice of programming language solely on project constraints and productivity considerations. The latter in turn are likely much more dependent on the expertise of your team than anything else. And for the vast majority of all software performance doesn’t even matter much. But then again the vast majority of software are apps, websites, and scripts. Not my line of business. I’m a specialized minority.

I do massively parallel computations and soft real-time applications. Stuff where you can’t afford having an operation take ten milliseconds to complete. Obviously I care a lot for performance. When it comes to languages and libraries less overhead and more control is better for me – as long as productivity doesn’t suffer too much. Because in the end someone has to pay for the time my colleagues and I spend on turning ideas into code.

On a more holistic scale for performance and productivity both C++ and C# are somewhat in the middle. Perhaps with C++ favoring performance and C# favoring productivity whenever the two are mutually exclusive. Which is less often than you might think. If you really need raw performance you should head towards C, assembler, programmable hardware, or even custom chips. Oppositely if productivity is prime look for existing solutions in terms of frameworks, libraries, or even complete software.

C++ vs. C#

Lets compare these programming languages anyway. For reasons of scope I will just ignore all other programming languages. But you shouldn’t! Always be aware of what tools are available. They might be the better fit for the job.

I will also not consider platform dependence at all. If you have a very specific target platform in mind or need to support a wide variety of platforms chances are high that C# is not an option. This is a good example of project constraints mentioned above. Make sure to get those right before going into details.

Because my post turned out much longer than envisioned I’m going to break the actual comparison up into multiple parts and link them here.

  1. Memory management
  2. Type system
  3. Polymorphism
  4. Syntax