Writing a High-Performance C# Application

After months of working mostly in a business analyst/architect role und only occasionally writing code, I was finally given a project that now has me coding quite a bit for a change. The objective was to rewrite an existing Delphi application in C# and add new functionality. As with most of the applications I get to work on, performance was key.

Recommended reading

Before embarking on this project, I bought a copy of Pro .NET Performance: Optimize Your C# Applications by Sasha Goldshtein et alia. This book is a great read and I can recommend it without hesitation. You could probably stop reading this article and just get the book and you would be fine.

I also found Writing High-Performance .NET Code, but since I haven’t read it, can’t comment on its merits.

You may also want to check out my collection of links on the matter Reading up on Concurrent Programming.

Memory

While .NET offers automatic memory management, this by no means implies that you did not have to think about it. In fact, for high-throughput applications, I’ve found garbage collection (GC) to be one of the most crucial determinants of performance. The Goldshtein book discusses memory management at length.

In addition, there is also a wealth of more detailed information on various MSDN blogs, particularly the CLR Garbage Collector blog by Maoni Stephens.

Other, non-GC related memory management topics:

  • Know Thine Implicit Allocations highlights situations where memory allocations are made by the framework that you might be unaware of. The good news is in newer version of the framework steps are taken to reduce these.
  • Choosing between class and struct, as (generally speaking) instances of the latter will be collected with the objects that contain them and not result in new objects that need to be tracked during garbage collection.

I’ve actually become quite a fan of structs. One central type in my application, with tens of thousands of instances that store information in the form of a couple of ints and longs, is now implemented as a struct. Instances are stored in an array and instead of passing references, I pass the elements as reference parameters or refer to them by their index. The code in this part of the application might not be very object-oriented, but that is often the price to pay for excellent performance.

Specialized classes

As a general rule, I recommend using the well-known classes from the .NET framework, e.g. for collections. Every once in a while, however, there are situations, where you have special (performance) requirements that may not be met by standard collections. There is actually a chapter in the Goldshtein book about writing your own collections. Sometimes, however, there are already classes that do what you need, they are just hidden away in a different namespace.

For instance, while everybody knows about System.Collections.Concurrent, there is also System.Collections.Specialized, which contains a couple of interesting types:

Speaking of different implementations of dictionaries, I’ve found this article about Choosing The Right Collection Class extremely helpful.

Finally, LazyInitializer is an excellent alternative to the Lazy<T> class, particularly when you are watching your memory footprint and/or the number of objects you create.

Advertisements

Travel Tech

I just came back from a business trip to Hong Kong and I was kind of embarrassed by how many electronic devices I had to unpack when going through security.

While packing I actually gave some thought to whether  I really needed all of these things. However, after careful analysis I concluded that every single item was indeed serving a purpose and deserved to be brought along.

  • Laptop: It was a business trip, so naturally I had to bring the company laptop, even though it’s bulky and doesn’t really do anything but allow me to VPN into the company network.
  • Surface Pro: The flight from Frankfurt to Hong Kong is a long one, and since I can’t play movies on the locked-down company laptop, I needed my Surface Pro. I also back up my photos to the 128 GB microSDXC card I have permanently installed as a sort-of second hard drive. And when I’m connected to Wi-Fi at the hotel, it automatically syncs with OneDrive, too. Since the Surface Pro is a full-fledged Windows 8.1 laptop, I can even touch up my photos in Photoshop Elements and/or post them to my blog using Windows Live Writer.
  • Mouse and Type Cover: While it’s nice that I can use the Surface Pro in touch-mode, I’m often more efficient with a mouse and keyboard. I once said that I bring the Touch Cover when I want to travel light. However, it has become less and less reliable over time; often not recognizing key strokes, thus forcing me to go back and retype or slow down and type more carefully. Hence, I only use the Type Cover nowadays. It’s not that much heavier, but so much better to type on. If it weren’t for a few missing keys (e.g.  the right-mouse-click) and slightly different keyboard layout, I would be as fast as I am with a regular keyboard.
  • Kindle Paperwhite: You can’t always watch movies and on a 12h flight, eventually the Surface will run out of battery power. So it goes without saying that I also have to bring an eBook reader. I thought about upgrading to the Kindle Voyage a short while ago, but the 190 EUR price tag seemed a bit hefty, given the Voyage doesn’t offer a whole lot of advantages over the original Paperwhite.
  • Nokia Lumia 920: Again, no-brainer to bring a smartphone. Even though I’m kind of paranoid about roaming charges and hence kept my phone in flight-mode through most of the trip. I was tempted to upgrade to the 930 when it came out, since my phone had developed a few glitches over time: dust has gotten into the front-facing camera’s lens, making every picture look extremely foggy. And the proximity sensor stopped working, so at the end of every phone call, I have to hope the other side hangs since my phone constantly thinks I was holding it close to my ear and therefore keeps the screen locked permanently. A colleague has the Lumia 930, so I checked it out. Alas, I couldn’t justify the upgrade, given the minor advances Nokia made with that device. I am looking forward to whatever the flagship device for Windows Phone 10 will be.
  • Sony Alpha 58: Judging by the number of Photography tagged posts on this blog, you might have guessed that I’ve gotten into that a bit. And since I had some time to myself to explore the city, I just had to bring my camera. The camera on the Lumia 920 is pretty good, but it doesn’t even come close to a real DSLR.

Micro-USB For The Win

It is really fortunate, that both my phone and my Kindle can be charged via Micro-USB. Since the Surface Pro charger has a USB output, I only had to bring one additional USB to Micro-USB cable. And I can even use that cable to connect my camera to my Surface for downloading pictures.

Even more gadgets

Back in 2012, I posted a picture of all the gadgets I use regularly. Interestingly enough, none of those listed above appear in that picture even though it was taken only two and a half years ago.