Today I witnessed how design of the programming language can force you to produce less performant software.

On my current project I am working with C# and Unity, programming cool multiplayer game for PC.

My task was to handle push message from remote server and update list of containers with new info on client side. My decision was to create list with structs and then update corresponding objects while receiving message from remote server. At first it seems as easy and straightforward procedure. That was the moment when c# surprised me.

I wrote a bit of pseudo code to demonstrate why:

    public struct PlayerInfo
        public bool isOnline;

    public static void Main(string[] args)
        List<PlayerInfo> playersData;
        playersData = new List<PlayerInfo>();

        for (int i = 0; i < 20; i++)
            var obj = new PlayerInfo();
            obj.isOnline = false;

        for (int i = 0; i < playersData.Count; i++)
            playersData[i].isOnline = !playersData[i].isOnline;

This won't compile, because c# doesn't let you modify members of value type containers while iterating over this container through the list or any other data structure. Though I don't understand the reasoning behind this design decision, it urges programmer to use class in this particular case (which is reference type in c#) and decrease quality of the program, because of two main points:
1. Struct is ideal tool for this problem as official page says.
2. Class container will always work slower in c#:

  • class gets allocated in dynamic memory storage. Access to objects is no compare with struct speed on stack.

  • c# runtime will create additional references to manage created class instance.

By the way, if you also like to really think about your decisions in software design, I recommend you to visit Handmade Network community.