Posts

Showing posts with the label performance

Working with Strings in .NET

Introduction So, by now you should know all there is to about string concatenation  in .NET and the  StringBuilder  class and the  string.Concat  method. I'm not going to cover that here, but, instead, some less known functionality around strings that can (also) bring some performance advantages. Searching When it comes to searching for values ("needles") inside a string ("haystack"), we have a couple options: Using the built-in string methods, such as  string.IndexOf , string.IndexOfAny , string.Contains The ContainsAny extension that works with memory spans Using regular expressions with  Regex , for more advanced pattern matching Since .NET 8, we have another one: the  SearchValues<T>  class. This one is optimised for searching for one or multiple strings (exact values) inside of a string. During its creation, it parses the string and builds a memory model that is suitable for searching. With time, using  SearchValues<T...

Optimising EF Core

Note: this is an update to an old post on my old blog . Introduction We all know O/RMs aren't the fastest tools when it comes to loading data, and Entity Framework Core is surely no exception. After all, they need to do a lot of operations, such as translating LINQ queries to SQL, instantiating the right entities, hydrating them (setting their properties from the database-retrieved values), taking a snapshot for the change-tracking functionality, etc, etc. But there, however, are a few things that we can do to help speed things a bit! Here are a few tips for you to make the best usage of Entity Framework Core, organised intro three topics: Optimising reads Optimising writes Other Optimising Reads Use Compiled Queries One of the things that can take some time is the translation from LINQ to SQL. This needs to happen all the time we execute a query from LINQ, unless, of course, we use a compiled query. Compiled queries are not exactly new in EF, and you can read...

.NET Collections

Introduction Sometimes, the proper choice of a collection can greatly impact the performance of your application. For example, there are collection types that are more appropriate for insertions, others that allow faster lookups, and so on. Plus, you must decide if you want indexed collections or not, and if you want to have generic collections, in order to have compile-time type checking. The .NET BCL comes with several general-purpose collection classes. Here is a list of all the collection classes as of .NET 9 and their intended use. Namespaces Namespace Purpose System.Collections Basic types, non-generic collections and interfaces System.Collections.Generic Generic interfaces and collections System.Collections.ObjectModel Observable and read-only collections System.Collections.Specialized Specialised collections System.Collections.Concurrent Concurrent (thread-safe) collections ( System.Collections.Concurrent Nuget package ) System.Collections.Immutable Immutable collections ( Syst...