Taming Complexity with Responsibility
Imagine, one quiet morning, your boss comes to you and says, “Hey, our web shop is growing and we will be having more than one delivery provider now. Can you implement something that would select the best provider after a client pays for a delivery?”.After some back and forth about the criteria on how a delivery company should be selected - mostly by package size, weight and delivery company area, you set out to write the code. How hard can it be? Just write a few if statements, and that’s it, r ...
Disentangling the Spaghetti Monster
In this blog post, we will explore the practical application of a specific design pattern. To illustrate its usefulness, we will gradually reveal the problem in an “organic” manner, simulating how one might encounter such an issue in their daily programming tasks.
The What and WhyPicture this: you’re working on a music streaming platform, and you already implemented live and offline playback, search functionality, and user ratings. The last piece of the puzzle? Playlist suggestions based on user ...
From Inheritance Hell to Component Heaven, the ECS Pattern
All you need is love and Object-Oriented, right? Right?Object-Oriented programming is one of the most widely used programming paradigms. It’s flexible, powerful, and has proven its worth over the years. However, as with any tool, there are situations where it might not be the best fit. In some cases, using an Object-Oriented approach can result in code that’s hard to maintain and overly complex.
Let’s say we’re developing a game and we want to add magic weapons. It should be trivial to create a ...
NancyFx vs. FeatherHttp
As a long time fan of the lightweight syntax of the awesome NancyFx web framework, I was really curious when I stumbled upon FeatherHttp, a new low-ceremony framework for building web services in .Net Core applications.
Naturally, I was curious not only about the syntax but about the performance as well. So I decided to compare their raw performance, using the awesome wrk2 tool.As a first step, I created two HTTP servers with one endpoint that does the same work in both cases.
NancyFx server c ...
More efficient string concatenation with string.Create()
Strings are immutable in C#, this is a common knowledge. Unless you use pointers in unsafe blocks, that is. Apparently, there is another way of making strings mutable.When I discovered string.Create(), a not-so-new but for some reason overlooked (by me at least!) method that was added since .Net Core 2.2 and Netstandard 2.1, I was really curious how it works (and how well it works too), so I looked at relevant .Net Core source code.
12345678910public static string Create<TState>(int lengt ...
Reasons for C# inlining are (a bit) more complex than you think.
The Twitter sometimes can serve as a place of unexpected insights and very interesting technical questions! For example, the question asked in a tweet here: https://twitter.com/EgorBo/status/1236324907723771904
Apparently, in the following code, C# compiler will inline Test1 and not Test2
12345678910111213141516171819using System;public static class C{ public static void Test1() { Validate(42, 42); } public static void Test2(int a, int b) { ...
Local variables vs properties. No suprises here?
Can the benchmark that compares array iteration vs. pointer based iteration be optimized further? Yep!In a post I wrote earlier about performance comparison between array access with pointers and the usual C#’s way, I saw an interesting comment that suggested a way to squeeze some more performance out of the scenario.Using a local variable instead of a call to a property (which is in fact a method call) made sense, though I wondered, just how much of a performance boost it would provide.So I de ...
Is it faster to access arrays with pointer arithmetics?
After seeing the results of my previous post where I tested performance impact of data locality, one of my collegues theorized that perhaps the huge difference between C++ and C# performance (assuming adjacent memory iteration) is due to boundary checks of C# arrays - he said that if I would use pointers to access arrays, the code will be much faster. I was curious if he was right, so I tested it.
The testI used the following test to try and see how much performance usage of pointers over simple ...
Easy way to configure SOS in LLDB
I don’t have much experience in using LLDB to debug .Net Core, so when I stumbled upon this little gem while looking at dotnet/diagnostics GitHub repository, I was instantly curious to try it: up to this point, in order to load the SOS plugin, I had to manually look up its folder, then specify it inside LLDB (via the load plugin command).This tool, however, should configure LLDB to load SOS plugin automatically!How? First install the tool via .Net Core CLI:
1dotnet tool install -g dotnet-sos
The ...
Sequential memory access is... faster?
In gamedev articles about Entity-Component-System, data locality is often mentioned as a big reason to use such design pattern. The underlying data structures of the ECS are cache friendly, thus allowing much better performance for iterations of large amount of game objects.I knew that cache-friendly usage of memory (sequential memory access, for example) would yield better performance, but I was curious, how much better it would be?
In case you never heard about Entity-Component-System, this a ...