Perfaddict.NET

— A blog for .NET performance addicts.
Performance
C#.NET

Intro: Performance in .NET

⏱️ What is performance?

I'd phrase it like this:

Performance is the ability of a code snippet (or module, application, etc.) to require as few resources as possible to complete a certain task.

The most important word in this definition is RESOURCE. Most people usually forget that performance is not simply CPU usage, or the time it takes to complete a task, it includes memory consumption, and I/O resource usages too. In .NET, memory consumption is even more critical, since while an actual snippet of code may be performant, it might put unnecessary pressure on the GC, causing the entire (or just the other part of) the application to be slower, or it might not cause actual slowness, but would make it less scalable.

❓ Should I always strive for writing the most performant code possible?

The short answer is definitely NO. Even if I'm speaking against myself at this moment.
But you HAVE TO know how and when to pay attention to the performance of your code. Here's 3 situations in which you should aim for performance:

🔥 Hot Path

Obviously. When the code in question is executed multiple magnitudes of order much more than other part of your application, performance becomes quite an important factor when coding.

🌅 Startup

Less obvious. You'd think who cares about startup when you can (and should) handle startup delay at the infrastructural level (swapping when changing code or instances, etc...). Well, you're right, but sometimes you have to swap instances, or adding a new one as soon as possible, when seconds might matter. And it is not that hard to mess up startup code so badly that it takes seconds to complete.

⛓️ Request Pipeline and Other Similar Shared Codes

Yes, it is the same as Hot Path, but it's good to have this in mind separately, because sometimes you forget to identify such code as Hot Path. Examples include but not limited to: Logging, Auth, General error handling, Parsing custom request format, General response formatting, and basically anything else in the request pipeline.

❓ Why shouldn't I always strive for writing the most performant code possible?

It's dead simple. Unfortunately, the more performant your code is, the less readable, and maintainable it will be. It is not that obvious that a performant code is always unreadable,because, fortunately, .NET has several language element that supports writing performant, and at the same time, readable code. These language elements and technics is what I want to show and write about in this blog.