In this post I describe a simple way to remove some byte[] allocations, no matter which version of .NET you're targeting, including .NET Framework. This will likely already be familiar to you if you write performance sensitive code with modern .NET, but I recently realised that this can be applied to older runtimes as well, like .NET Framework. This post looks at the changes to your C# code to reduce the allocations, how the compiler implements the change behind the scenes, and some of the caveats and sharp edges to watch out for. Span<T> and ReadOnlySpan<T> are a performance mainstay for .NET ReadOnlySpan<T> and Span<T> were introduced into .NET a long time ago now, but they have had a significant impact on the code you can (and arguably should) write, particularly when it comes to performance sensitive code. These provide a "window" or "view" over existing data, without creating copies of that data. The classic example is when you're manipulating string objects; instead of using…
No comments yet. Log in to reply on the Fediverse. Comments will appear here.