Skip to content
OnMSFT.com
  • Home
  • About
  • Contact
  • Windows
  • Surface
  • Xbox
  • How-To
  • OnPodcast
  • Edge
  • Teams
  • Gaming
Menu
  • Home
  • About
  • Contact
  • Windows
  • Surface
  • Xbox
  • How-To
  • OnPodcast
  • Edge
  • Teams
  • Gaming
  1. Home
  2. Performance Improvements in .NET 6

Performance Improvements in .NET 6

admin admin
December 28, 2022
5 min read

In this article

  • Collections and LINQ
    • Clone() Dictionary
    • Clone() SortedDictionairy
    • DistinctCount()
  • Encoding, Decoding and Cryptography
    • CryptoStream()
  • Blazor WASM
    • HashData()

For the past 5-6 years we’ve seen consistent improvements in performance and functionality across a multitude of products from Microsoft. Whether its SQL Server, Windows Server or even Windows 10, everything seems to be getting better and faster. Well the same has held true for the underlying technology which powers many of these experiences, .NET.

.NET Framework had grown to be a very robust system, but given its long history and evolution, it was somewhat saddled with legacy dependencies and compromises which began to hold back possibilities and even performance potential. One of the main goals of .NET Core was to break away from those limitations and give the framework a fresh start and open up horizons.

At the beginning of the .NET Core journey, it was becoming clear that we were in for a great ride. Out of the gate developers started to notice 2-3x improvements in performance and efficiency. There were obvious tradeoffs at the beginning, mostly due to the resultant feature gap, however that is no longer the case.

On the proverbial eve of .NET 6 being released, there is virtually nothing you can’t do with .NET 6 which would require you to tether yourself to the aging .NET Framework.

In this article we’re going to dive into some of the performance improvements realized by the forthcoming release of .NET. In some cases they might not seem significant when compared to .NET 5, but when compared to even the latest version .NET Framework or .NET Core 3.1 , we start to see just how far Microsoft has come and how these improvements translate to a better experience for developers and end users. I wish I could take credit for all the metrics we are going to discuss in this article, but all the credit goes to Stephen Toub, Partner Software Engineer, on the .NET Team. Stephen did an outstanding job benchmarking dozens of scenarios, however I am going to focus on three which are important to me, and perhaps important to you as well. Alright, let’s get to it!

Collections and LINQ

Clone() Dictionary

Whether you are loading data from a database, files or APIs to store, manipulate or present back to users. Improvements to the underlying processes which power these workloads can have tangible results. As observed below, creating one dictionary from another exhibits shows a substantial improvements.

private IEnumerable<KeyValuePair<string, int>> _dictionary = Enumerable.Range(0, 100).ToDictionary(i => i.ToString(), StringComparer.OrdinalIgnoreCase);

[Benchmark]
public Dictionary<string, int> Clone() => new Dictionary<string, int>(_dictionary);
Method Runtime Mean Ratio
Clone .NET Core 3.1 3.224 us 1.00
Clone .NET 5.0 2.880 us 0.89
Clone .NET 6.0 1.685 us 0.52

Clone() SortedDictionairy

private IDictionary<string, int> _dictionary = new SortedDictionary<string, int>(Enumerable.Range(0, 100).ToDictionary(i => i.ToString(), StringComparer.OrdinalIgnoreCase));

[Benchmark]
public SortedDictionary<string, int> Clone() => new SortedDictionary<string, int>(_dictionary);
Method Runtime Mean Ratio
Clone .NET Framework 4.8 69.546 us 1.00
Clone .NET Core 3.1 54.560 us 0.78
Clone .NET 5.0 53.196 us 0.76
Clone .NET 6.0 2.330 us 0.03

DistinctCount()

One very common and useful scenario is getting a unique value count from a large list which perhaps has some duplicates. Here we can once again see ~2x improvement over .NET 5.

private IEnumerable<string> _data = Enumerable.Range(0, 100_000).Select(i => i.ToString()).ToArray();

[Benchmark]
public int DistinctCount() => _data.Distinct().Count();
Method Runtime Mean Ratio Allocated
DistinctCount .NET 5.0 5.154 ms 1.04 5 MB
DistinctCount .NET 6.0 2.626 ms 0.53 2 MB

Encoding, Decoding and Cryptography

Not many modern applications can get away with not implementing cryptography in one way or another. Encoding and Decoding data is a very integral part of most data driven applications in virtually every industry.

CryptoStream()

Encoding and Decoding is something I deal with very often in my day job, when I migrated from .NET Framework 4.8 to .NET Core 3.1 I did not see much improvement in the performance or efficiency when Base64 encoding images. With the release of .NET 5 the leap was monumental. Not only is the performance incredible, but the efficiency of doing the work is almost too good to believe as you can see below.

private byte[] _data = Enumerable.Range(0, 10_000_000).Select(i => (byte)i).ToArray();
private MemoryStream _destination = new MemoryStream();

[Benchmark]
public async Task Encode()
{
    _destination.Position = 0;
    using (var toBase64 = new ToBase64Transform())
    using (var stream = new CryptoStream(_destination, toBase64, CryptoStreamMode.Write, leaveOpen: true))
    {
        await stream.WriteAsync(_data, 0, _data.Length);
    }
}
Method Runtime Mean Ratio Allocated
Encode .NET Framework 4.8 329.871 ms 1.000 213,976,944 B
Encode .NET Core 3.1 251.986 ms 0.765 213,334,112 B
Encode .NET 5.0 146.058 ms 0.443 974 B
Encode .NET 6.0 1.998 ms 0.006 300 B

Blazor WASM

Any improvements to running .NET in the browser is music to my ears! If you don’t already know what Blazor WASM is, check Microsoft’s Blazor page. For this test, the sample Blazor app’s counter page was modified to SHA-265 hash a string a few thousand times. The results are pretty amazing. One of the biggest improvements come from the ability to Ahead-Of-Time compile Blazor WASM applications.

Now with .NET 6, a Blazor WASM app can be compiled ahead of time entirely to WebAssembly, avoiding the need for JIT’ing or interpreting at run-time. All of these improvements together lead to huge, cross-cutting performance improvements for Blazor WASM apps when targeting .NET 6 instead of .NET 5.

– Stephen Toub, .NET Blog

HashData()

@page "/counter"
@using System.Security.Cryptography
@using System.Diagnostics
@using System.Text

<h1>Hashing</h1>

<p>Time: @_time</p>

<button class="btn btn-primary" @onclick="Hash">Click me</button>

@code {
    private const string Sonnet18 =
@"Shall I compare thee to a summer’s day?
Thou art more lovely and more temperate:
Rough winds do shake the darling buds of May,
And summer’s lease hath all too short a date;
Sometime too hot the eye of heaven shines,
And often is his gold complexion dimm'd;
And every fair from fair sometime declines,
By chance or nature’s changing course untrimm'd;
But thy eternal summer shall not fade,
Nor lose possession of that fair thou ow’st;
Nor shall death brag thou wander’st in his shade,
When in eternal lines to time thou grow’st:
So long as men can breathe or eyes can see,
So long lives this, and this gives life to thee.";

    private TimeSpan _time;

    private void Hash()
    {
        byte[] bytes = Encoding.UTF8.GetBytes(Sonnet18);
        var sw = Stopwatch.StartNew();
        for (int i = 0; i < 2000; i++)
        {
            _ = SHA256.HashData(bytes);
        }
        _time = sw.Elapsed;
    }
}

 

Method Runtime Result Improvement
Hash .NET 5 JIT 0.454 ms
Hash .NET 6 JIT 0.280 ms 38 %
Hash .NET 6 AOT 0.017 ms 96 %

For a ton of other benchmarks and information check out the source article here.

What do you guys think? Are you excited about .NET 6? Have you already started using it? Let me know your thoughts in the comments below.

Related

Share this article:
Previous Article Microsoft mistakenly posts about public preview of Visual Studio Code for the web Next Article .NET 6 is almost ready for prime time

Related Articles

PowerToys 0.75 released; Environment Variables editor is here

October 31, 2023

Former Halo developer Bungie announces layoffs in wake of game delays

October 31, 2023

Siemens and Microsoft partner to bring GenAI to industries worldwide

October 31, 2023

Leave a Comment Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • PowerToys 0.75 released; Environment Variables editor is here
  • Former Halo developer Bungie announces layoffs in wake of game delays
  • Siemens and Microsoft partner to bring GenAI to industries worldwide
  • 43 Microsoft Edge keyboard shortcuts to remember for stylish and speedy web surfing
  • Here is what’s coming to (and leaving) Xbox Game Pass soon

Recent Comments

No comments to show.

Archives

  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • February 2016
  • January 2016
  • December 2015
  • November 2015
  • October 2015
  • September 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • March 2015
  • February 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • September 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • October 2012
  • September 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010

Categories

  • Announcements
  • Deals
  • Developer
  • Editorial
  • Feature
  • Feature stories
  • Hero-post
  • Hotdeals
  • How-to
  • Latest news
  • Microsoft / office 365
  • News
  • Office 365
  • Onpodcast
  • Opinion
  • Our featured post
  • Polls
  • Review
  • Reviews
  • Videos
OnMSFT.com

OnMSFT.com covers Microsoft news, reviews, and how-to guides. Formerly known as WinBeta, we have been your source for Microsoft news since 1998.

Categories

  • Windows
  • Surface
  • Xbox
  • How-To
  • OnPodcast
  • Gaming
  • Edge
  • Teams

Recent Posts

  • PowerToys 0.75 released; Environment Variables editor is here
  • Former Halo developer Bungie announces layoffs in wake of game delays
  • Siemens and Microsoft partner to bring GenAI to industries worldwide
  • 43 Microsoft Edge keyboard shortcuts to remember for stylish and speedy web surfing
  • Here is what’s coming to (and leaving) Xbox Game Pass soon

Quick Links

  • About OnMSFT.com
  • Contact OnMSFT
  • Join Our Team
© 2010–2026 OnMSFT.com LLC. All rights reserved.
About OnMSFT.comContact OnMSFT