Returning Awaited methods vs Tasks

I have come across this question several times, I thought it might worth a little investigation. Which of the following method runs faster TestAsync or TestDirect, given the Sum method?

public async Task<int> TestAsync(int a, int b) => await Sum(a, b);

public Task<int> TestDirect(int a, int b) => Sum(a, b);

public Task<int> Sum(int a, int b) => Task.FromResult(a + b);

At first look there is not much difference, but if we look at the performance of them, one executes faster.

At second look we could assume TestDirect must be faster as the compiler 'probably' does not convert it to a state machine. Or is the compiler smart enough to optimize away the state machine in case of TestAsync?

Find out more


C# Functional style - Part 3

In this post I will take the existing methods to a higher abstraction by handling them as pure C# Func.

First we need to introduce a couple of extension methods, which will help to Curry and Partially apply some of the arguments on the existing methods. In C# this is a slightly more difficult, because we would need to create overrides for each different signature based on the arity of the method.

Creating an extension method for Currying:

public static Func<TIn, Func<TOut>> C<TIn, TOut>(this Func<TIn, TOut> f) => x => () => f(x);

public static Func<TIn1, Func<TIn2, TOut>> C<TIn1, TIn2, TOut>(this Func<TIn1, TIn2, TOut> f) => x => y => f(x, y);

// Further overloads

Find out more


Micro benchmarking: finding the max value

I have seen a Pluralsight course recently on performance by Sasha Goldstein. At one point, he mentioned how he had used processor pipelining to find a maximum number of an array of integers even faster the than the regular for/while loop. This got me thinking, can we get the presented solution even faster? (the catch: he also presented some other features of the clr/framework which will help with this)

So how can get even faster (in certain situations) finding a maximum integer in an unordered array?

In this investigation, I am going to use micro-benchmarking, which is certainly a way of measuring one or a couple of instructions, but I find the results difficult to generalize: things being measured are correct, but small changes to the code or the environment can have significant impact on the results.

In my tests I will use an array of 2MB, which easily fits into the processor cache of my machine. The array is filled with integers [0..10000). It is important to have it the whole array fit in the processor cache, so we can avoid measuring the latency for bringing a chunk of data from memory to the processor.

Find out more


C# Functional style - Part 2

In the previous post, I have looked how I could compose methods together:

int RequestHandler(int x) => LogOutput(Add1(LogInput(x)));

As it is a bit difficult to read (inside out), I would like to see alternative solutions to compose them together while achieving the same results.

For the following methods, it is needed that each method has one input parameter and one result returned. In the following posts, I will use the partial application of arguments and currying to achieve this.

Find out more


Allocation free algorithms

With latest C# we have a handful of new constructs to create more performant applications.

C# Challenge: allocation free algorithms

When talking 'allocation free' we mean heap allocations, while on the stack memory is still going to be allocated similarly as before.

Why is this important?

Find out more