The power of Span

Intro to the problem

In one organization, I had to schedule a regular meeting once a week, that fits most the people. Fortunately, the people in this organization mark their holidays in a csv file, so processing the file can help me to figure the day when the least favorable for holidays.For this I would like to have a table which shows the given weekday, and the number of holidays taken on that day.

As a note, we can assume people include public holidays to the time interval in the file, as they usually do so. They also include Saturdays and Sundays too, but those will be filtered out.

Fortunately, we can have two solutions for this problem. On is the classic solution in the 'old' C# way, and one new solution from the Span era of dotnet with a more efficient solution.

Find out more


Ref Returns

C# has recently introduced some new features (version 7.0) one of which it is ref returns. I will use this feature in this post to further improve the performance of the Parser created in my previous post.

To refresh the Parser class looked as follows:

public class Parser 
{
  public T Parse<T>(ReadOnlySpan<char> input, PropertySetter[] setters) where T : struct
  {
    T result = default;
    int separator;
    int propertySetterIndex = 0;
    while((separator = input.IndexOf(',')) != -1)
    {
      var parsedValue = int.Parse(input.Slice(0, separator));
      setters[propertySetterIndex++].SetValue(ref result, parsedValue);
      input = input.Slice(separator + 1);
    }
    return result;
  }
}

To recap it receives an input of comma separated integers that we are parsing into a struct of the user's choice: T. Extending it to other than integer types and fixing the possible ArgumentOutOfRangeException is not a concern of this blog post.

Find out more


Heap Allocation free Parsing: structs

I have been working on a library RapideFix to parse fix messages. One of the challenges is to parse a message into a struct with the least amount of allocations possible. That means to allocate only for the references (if any) that are part of the struct's fields.

For the sake of this post, I will use a very simple struct with 2 integer properties and the parser method will set values to them. However, the code should be dynamic, so the user can replace the struct to any other custom one.

Starting Point

So, let's start with a sample code, which is incorrect after all, but it demonstrates what I want to achieve (if it worked correctly).So what I want to do is that given a string with two integers separated by commas "20,19,", and the parser should set the integer values on a struct, Point:

Find out more


WDS Setup for my Router

This is a rather unusual post. It is not going to deal with software development .net or C#, but it solves a networking issue.

Problem

My landline Internet Service Provider has increased the cost of the service by 30% while given no additional value. As I had unlimited mobile Internet connection already, so I decided to cancel my landline and depend on my mobile connection only. This was a smart move from my point of view, because I spend less-and-less time in the apartment in question with the landline. For this though it meant I need to run my Android mobile phone in hotspot mode, and all other devices would connect to it.

Solution

Find out more


NDC London 2019

After every great conference, I create a list of my favorite talks. This year in January I had an opportunity to attend to NDC the first time in person.

As videos have been uploaded to YouTube, I recommend the following sessions.

C#

Much Ado about Nothing: A C# play in two acts - Act 1- Mads Torgersen, Bill Wagnerhttps://www.youtube.com/watch?v=XGn15woZQ4o

Find out more