O’Connell Bridge, Dublin, Ireland by aalmada

This is a third post in a series on performance in C#.

Modern CPUs, in a quest for better performance, try to maximize its throughput by executing instructions simultaneously and while waiting for the result of logic tests. They try their best to guess the behavior of our code but, many times, the code has to be adjusted to maximize that potencial. These optimizations are CPU dependent and results may vary.

To the ones that keep quoting Donald Knuth every time performance is mentioned, I’ll start with a quote from that exact same paper:

“Easily obtained improvements are never considered…


Dinokart, Lourinhã, Portugal by aalmada

In my previous post I left out ArraySegment<T>. This is a value type that implements a way to slice arrays but that has now been replaced by Span<T> or Memory<T>.

Span<T> supports other types of contiguous memory collections but, if you’re using just arrays, ArraySegment<T> has a few advantages.

Span<T> cannot be used as a generics type and can only be used as a field type in a ref struct. With any other struct, or class, you’ll have to use a Memory<T> field type. To enumerate a Memory<T>, you have to call its Span property, which creates a new instance…


Snails, Parque Urbano Vale da Montanha, Lisbon, Portugal by aalmada

Implementing the sum of the items in an array is very simple. I think most developers would implement it this way:

There’s actually a simpler alternative in C#:

Another alternative is to use the Sum() operation provided by LINQ. It can be applied to any enumerable, including arrays.

So, how do these fair in terms of performance?


I’ve been writing here many stories about enumeration in .NET. I know it’s hard to remember everything, specially when developing large projects with several other developers. I decided to develop NetFabric.Hyperlinq.Analyzer that peer reviews the code while it’s typed. I actually use it myself to develop NetFabric.Hyperlinq.

The easiest way to start developing an analyzer is to use the template available with Visual Studio. One of its great features is that it also generates the unit testing project.

Notice that it applies the analyzer and the code fixer to source code strings. …


Achieving zero heap memory allocations with LINQ operations.

Empty Flowers by aalmada

Introduction

This is a follow up on my exploration into improving LINQ performance. This is a work in progress. Go to my previous article to find what triggered this.

The source code is available at https://github.com/NetFabric/NetFabric.Hyperlinq

Zero Allocation

I’ve been emphasizing the importance of the use of value types for raw performance but, another advantage is that they are allocated on the stack. This means that, they don’t contribute to increasing the frequency of the garbage collection. The deallocation is deterministic. It’s immediately performed when it gets out of scope.

One of my objectives for…


Improving performance of LINQ’s Select operation.

Tiles by aalmada

Introduction

This is a follow up on my exploration into improving LINQ performance. This is a work in progress. Go to my previous article to find what triggered this.

The source code is available at https://github.com/NetFabric/NetFabric.Hyperlinq

Select Operation

Select() is a projection operation, it creates a new sequence by applying a transformation function to each element of the original sequence.

Note: In other programming languages, it’s usually called map().

Its implementation is very simple. Given a IEnumerable<TSource> and a selector function that transforms a TSource into a TResult, it applies the transformation to each element of the…


Improving performance of LINQ generation operations.

Beach Sand by aalmada

Introduction

This is a follow up on my exploration into improving LINQ performance. This is a work in progress. Go to my previous article to find what triggered this.

The source code is available at https://github.com/NetFabric/NetFabric.Hyperlinq

Generation Operations

LINQ is a library of operations on IEnumerable<T>. These operations can be grouped into categories. One of these categories is the generation operations that includes all the operations that create a new sequence of values.

In this category we can find the following operations:

  • DefaultIfEmpty<TSource>(TSource defaultValue) — Replaces an empty collection with a default valued singleton collection.
  • Empty<TResult>() —…


Improving performance of LINQ operations.

High tension by aalmada

LINQ is a great tool for writing easy to read and easy to maintain data processing code. I’ve been writing articles on how using it incorrectly will seriously affect the performance of the application but, even when used correctly, it usually does not result in the best performant solution.

For this reason, many avoid using it. Some use libraries that try to solve this issue. Some of these libraries try to stay true to the LINQ API, some have very different solutions.

Recently, I’ve started wondering what if LINQ used some of the techniques described…


Warp Speed by aalmada

Enumerable.Empty<T>()

In a previous article I focused on Enumerable.Empty() as it’s the simplest possible implementation of IEnumerable. I used my own version of the code that is easier to understand, compared to the one currently found in LINQ.

A recent merge request in the dotnet/corefx repository changes it to something very close to my version. The difference is that they use a singleton of a reference-type that implements both IEnumerable and IEnumerator. This is only possible because Empty<T> is immutable but very interesting.

Given that, previously all the enumerators in the BCL were value-types for performance reasons, I was curious about…


Plaza de Armas, Seville by aalmada

This is part of a series of articles:

ToList()

I find very frequently the use of a ToList() at the end of every LINQ query. Most of the time this is not necessary and can have a huge impact on performance.

Let’s analyze a small example:

This code writes to the console the even numbers between 0 and 10. You can see in SharpLab.io that is does work.

Antão Almada

Principal Engineer @ Farfetch - Future Retail Lab https://about.me/antao.almada

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store