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.


Eagle Eye by aalmada

This is part of a series of articles:

Finding an item

I already mentioned in the first article of this series that IEnumerable:

  • doesn’t allow random access.
  • doesn’t give access to any other collection information.

This implies high costs when trying to find an item. This article focus on the methods First() and Single(), commonly used to search for an item, and how its real cost can be hidden.

First() and Single()

First() and Single()


Summer Rain by aalmada

This is part of a series of articles:

Enumerable.Empty<T>()

Enumerable.Empty<T>() is a static method in the System.Linq namespace and returns the simplest implementation of IEnumerable<T>. I think it’s interesting to see how it really works to better understand enumeration in .NET.

In a previous article I wrote that it returns

an IEnumerable implementation that generates IEnumerator implementations where the method MoveNext() always returns false.

This is how it looks…


Giraffe @ Badoca Safari Park by aalmada

This is part of a series of articles:

Count()

On my previous article, I analysed the usage of IEnumerable strictly based on its contract. It was brought to my attention that LINQ to Objects optimizes some of the described scenarios, possibly breaking some of my assumptions. It’s a good point and I did some more research.

Checking the implementation of the Count() extension method in LINQ to Objects, we…

Antão Almada

Principal Engineer @ Farfetch - Store of the Future 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