When your C# has to be fast.

When Microsoft came up with .NET environment and the snazzy new C# programming language to go with it, one of the design goals was to support code that could be shoved willy-nilly across unsecure networks. Thus the Common Language Runtime (CLR), which among other things creates a runtime environment that’s a little like the Java Virtual Machine (JVM) from Sun.

Code for .NET doesn’t execute directly on the CPU, and it doesn’t talk directly to the operating system. Instead, the CLR runtime–part of that big download when you pull “the .NET Framework” from Microsoft’s download site–treats the code as input and does whatever needs to be done at the CPU and operating system level for you.

Safety first, usually

Why would anyone bother with that? Among other things, the CLR itself is assumed to be “safe.” The CLR won’t do anything to interfere with processes that it’s not supposed to control; it observes security restrictions; and it doesn’t let you access code or data through pointers. So in theory, and most of the time in practice, CLR code won’t mess up your computer and therefore it is safe to run CLR code from untrusted sources.

The downside, again one among many, is that as a programmer you don’t have access to pointers in safe code. They’re automatically considered “unsafe.” Any code with pointers in it is “unsafe.” Any code that uses code with pointers in it… is “unsafe.” Any code that uses code that uses code… blah blah blah.

When it has to be fast

Right now I’m working on a .NET application that needs to pull a few megabytes of data from a legacy device driver and do a lot of math on it. It’s too slow to load all the data into a big byte[] array, and then to access each element one by one for calculations. Instead, I wrote something like this, with the names changed to protect the innocent:

[sourcecode language=”csharp”]
unsafe public class BitContainer
{
private IntPtr data;
public static int Size; // initialized elsewhere
public static int Count // however many shorts can fit into Size
{
get
{
return Size / sizeof(short);
}
}
public BitContainer()
{
data = Marshal.AllocHGlobal(Size);
}
~BitContainer()
{
Marshal.FreeHGlobal(data);
}
public void LoadFromStream(BinaryReader input)
{
ushort* p = (ushort*) data.ToPointer();
for (int i = 0; i < Count; i++)
{
*p = input.ReadUInt16();
p++;
}
}
public void GetItemsOverThreshold(int threshold, int max, ref List<Item> items)
{
log.Write();
items.Clear();
unsafe
{
ushort* p = (ushort *) data.ToPointer();
for (long i = 0; i < Count; i++)
{
if (*p >= threshold)
{
if (items.Count >= max)
{
throw new ApplicationException("Too many items passing! Turn down the gain?");
}
else
{
long x = i % FRAME_WIDTH;
long y = i / FRAME_WIDTH;
items.Add(new Item(x, y));
}
}
}
}
}
}
[/sourcecode]

The alternative to tearing through data with a C-style pointer would have been to pick through the elements of a .NET array of byte or short values. Each array access goes through the CLR for bounds-checking and the like, and when you’re hitting literally millions of input values that’s just too slow. I originally did the BitContainer code above that way, and it took hours.

The takeaway

It’s a pain to test, and it’s sometimes hazardous, and it contaminates your entire application with the unsafe label, but dropping under the CLR and writing fast code with pointers is sometimes the only way to get acceptable runtime speed.