ASP.NET Core 6 Performance Improvements

There are quite a few performance improvements in ASP.NET Core. In today’s ASP.NET tutorial, we will take a look at the majority of those updates, learn how they affect .NET developers, and preview some ASP.NET example code.

ASP.NET 6 Core Updates

The following updates are specific to ASP.NET Core 6, unless otherwise noted. We will continue to provide updates here as they become available.

Read: .Net Maui 11 Preview: Features

Native Byte Array Interop

When performing Interop with JavaScript, developers had to, in the past, send and receive the byte arrays in Blazor as Base64 encoded for JSON serialization. This increased not only the transfer size, but the CPU load as well, resulting in performance issues.

Blazor now includes optimized support for byte arrays without the need of using Base64 decoding and encoding. Programmers can now work with the byte array transparently with the use of JavaScript’s Uint8Array. A small example follows:

HTML
<script>
  window.receive = (bytes) => {
    let decoder = new TextDecoder();
    let decodedstring = decoder.decode(bytes);
    return decodedstring;
  };
</script>
razor
@page "/bytearray_ex"
@inject IJSRuntime JS
<h1>Byte Array Example</h1>
<p>
    <button @onclick="Send">Send Array</button>
</p>
<p>
    @result
</p>
@code {
    private string? result;
    private async Task Send()
    {
        var bytes = new byte[] { 0x48, 0x61, 0x6e, 0x6e, 0x65, 0x74, 0x50, 0x69,
            0x6e, 0x67, 0x7a};
        result = await JS.InvokeAsync<string>("receive", bytes);
    }
}

Entity Framework Core

EF Core made some huge improvements in 6.0. These improvements came from improving object pooling, checking if telemetry is enabled, and adding an option to opt out of thread safety checks when you know your app uses DbContext safely. Microsoft claims that query execution is now 31% faster.

.NET Framework

Span<T>

Span<T> performance includes the following:

  • Removal of temporary string allocation in String.Substring and uses Span instead.
  • Introduction of a new Span-based API for enumerating an allocation-free query string when the query string contains encoded characters.
  • Parsing logic in ContentDispositionHeaderValue, in order to use Span-based APIs so that temporary strings and temporary byte[] arrays can be avoided.

Read: 8 .NET Emerging Technologies for Developers

Idle Connections

Changes to reducing the amount of memory used in ASP.NET Core 6 include the following:

  • Reducing the size of objects used by connections such as System.IO.Pipelines, SocketConnections, and SocketSenders.
  • Pooling of commonly accessed objects so that old instances and save on allocations can be reused.
  • Avoidance of allocating buffers upfront for a read that may or may not complete at a future time, thus avoiding large allocations until the data is available.
  • Moved connection PipeOptions to the connection factory so that only one allocation for the entire lifetime of the server and reuse the same options for every connection.
  • Header values in WebSocket requests are replaced with interned strings that can allow strings allocated during header parsing to be garbage collected.

About ASP.NET Core 6

.NET Core 6 was released in early November of 2021. It featured updates to a wide array of features and functionalities for not only ASP.NET and the .NET Framework, but also to programming languages like C# 10 and F# 6. New APIs were also introduced, including APIs for JSON processing, mathematical functions, and HTTP/3 to name but a few.

Visual Basic also saw some improvements in developer experience for Visual Studio and, specifically, the Windows Forms project. JavaScript users were also affected, as Blazor components are now renderable from JS apps. The changes in .NET 6 – which are too many to detail here – are all covered as part of a Long-term Support (LTS) release, which promises support for the next three years on all of the major operating systems, including macOS, Windows, and Linux. You can learn more about the major changes to .NET 6 by visiting Microsoft’s .NET 6 announcement.

Additionally, you can get a more detailed breakdown of ASP.NET Core 6 performance enhancements by visiting the Microsoft DevBlogs.

Read more .NET programming tutorials, news, and tool reviews.

Hannes DuPreez
Hannes DuPreez
Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

More by Author

Must Read