{"id":23254,"date":"2019-05-15T12:17:00","date_gmt":"2019-05-15T19:17:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/dotnet\/?p=23254"},"modified":"2025-10-29T11:06:47","modified_gmt":"2025-10-29T18:06:47","slug":"performance-improvements-in-net-core-3-0","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/performance-improvements-in-net-core-3-0\/","title":{"rendered":"Performance Improvements in .NET Core 3.0"},"content":{"rendered":"<p>Back when we were getting ready to ship .NET Core 2.0, I wrote a\u00a0<a href=\"https:\/\/blogs.msdn.microsoft.com\/dotnet\/2017\/06\/07\/performance-improvements-in-net-core\/\">blog post<\/a>\u00a0exploring some of the many performance improvements that had gone into it. I enjoyed putting it together so much and received such a positive response to the post that I did it\u00a0<a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/performance-improvements-in-net-core-2-1\">again for .NET Core 2.1<\/a>, a version for which performance was also a significant focus. With <a href=\"https:\/\/www.microsoft.com\/en-us\/build\">\/\/build<\/a> last week and <a href=\"https:\/\/dotnet.microsoft.com\/download\/dotnet-core\/3.0\">.NET Core 3.0<\/a>&#8216;s release now on the horizon, I&#8217;m thrilled to have an opportunity to do it again. .NET Core 3.0 has a ton to offer, from Windows Forms and WPF, to single-file executables, to async enumerables, to platform intrinsics, to HTTP\/2, to fast JSON reading and writing, to assembly unloadability, to enhanced cryptography, and on and on and on&#8230; there is a wealth of new functionality to get excited about. For me, however, performance is the primary feature that makes me excited to go to work in the morning, and there&#8217;s a staggering amount of performance goodness in .NET Core 3.0. In this post, we&#8217;ll take a tour through some of the many improvements, big and small, that have gone into the .NET Core runtime and core libraries in order to make your apps and services leaner and faster.<\/p>\n<h3>Setup<\/h3>\n<p><a href=\"http:\/\/github.com\/dotnet\/benchmarkdotnet\">Benchmark.NET<\/a>\u00a0has become the preeminent tool for doing benchmarking of .NET libraries, and so as I did in my 2.1 post, I&#8217;ll use Benchmark.NET to demonstrate the improvements. Throughout the post, I&#8217;ll include the individual snippets of benchmarks that highlight the particular improvement being discussed. To be able to execute those benchmarks, you can use the following setup: 1. Ensure you have\u00a0<a href=\"https:\/\/dotnet.microsoft.com\/download\/dotnet-core\/3.0\" rel=\"nofollow\">.NET Core 3.0<\/a>\u00a0installed, as well as\u00a0<a href=\"https:\/\/dotnet.microsoft.com\/download\/dotnet-core\/2.1\" rel=\"nofollow\">.NET Core 2.1<\/a>\u00a0for comparison purposes. 2. Create a directory named\u00a0<code>BlogPostBenchmarks<\/code>. 3. In that directory, run\u00a0<code>dotnet new console<\/code>. 4. Replace the contents of BlogPostBenchmarks.csproj with the following:<\/p>\n<pre class=\"tab-convert:true lang:xhtml decode:true \" title=\"BlogPostBenchmarks.csproj\">&lt;Project Sdk=\"Microsoft.NET.Sdk\"&gt;\n\n  &lt;PropertyGroup&gt;\n    &lt;OutputType&gt;Exe&lt;\/OutputType&gt;\n    &lt;AllowUnsafeBlocks&gt;true&lt;\/AllowUnsafeBlocks&gt;\n    &lt;TargetFrameworks&gt;netcoreapp2.1;netcoreapp3.0&lt;\/TargetFrameworks&gt;\n  &lt;\/PropertyGroup&gt;\n\n  &lt;ItemGroup&gt;\n    &lt;PackageReference Include=\"BenchmarkDotNet\" Version=\"0.11.5\" \/&gt;\n    &lt;PackageReference Include=\"System.Drawing.Common\" Version=\"4.5.0\" \/&gt;\n    &lt;PackageReference Include=\"System.IO.Pipelines\" Version=\"4.5.0\" \/&gt;\n    &lt;PackageReference Include=\"System.Threading.Channels\" Version=\"4.5.0\" \/&gt;\n  &lt;\/ItemGroup&gt;\n\n&lt;\/Project&gt;<\/pre>\n<ol>\n<li>Replace the contents of Program.cs with the following: <\/li>\n<\/ol>\n<pre class=\"wrap:false lang:c# decode:true\">using BenchmarkDotNet.Attributes;\nusing BenchmarkDotNet.Configs;\nusing BenchmarkDotNet.Jobs;\nusing BenchmarkDotNet.Running;\nusing BenchmarkDotNet.Toolchains.CsProj;\nusing Microsoft.Win32.SafeHandles;\nusing System;\nusing System.Buffers;\nusing System.Collections;\nusing System.Collections.Concurrent;\nusing System.Collections.Generic;\nusing System.Collections.Immutable;\nusing System.Diagnostics;\nusing System.Drawing;\nusing System.Drawing.Drawing2D;\nusing System.Globalization;\nusing System.IO;\nusing System.IO.Compression;\nusing System.IO.Pipelines;\nusing System.Linq;\nusing System.Net;\nusing System.Net.Http;\nusing System.Net.NetworkInformation;\nusing System.Net.Security;\nusing System.Net.Sockets;\nusing System.Runtime.CompilerServices;\nusing System.Runtime.InteropServices;\nusing System.Security.Authentication;\nusing System.Security.Cryptography.X509Certificates;\nusing System.Text;\nusing System.Text.RegularExpressions;\nusing System.Threading;\nusing System.Threading.Channels;\nusing System.Threading.Tasks;\nusing System.Xml;\n\n[MemoryDiagnoser]\npublic class Program\n{\n    static void Main(string[] args) =&gt; BenchmarkSwitcher.FromTypes(new[] { typeof(Program) }).Run(args);\n\n    \/\/ ... paste benchmark code here\n}<\/pre>\n<p>To execute a particular benchmark, unless otherwise noted, copy and paste the relevant code to replace the\u00a0<\/p>\n<p><code>\/\/ ...<\/code>above, and execute\u00a0<code>dotnet run -c Release -f netcoreapp2.1 --runtimes netcoreapp2.1 netcoreapp3.0 --filter \"*Program*\"<\/code>. This will compile and run the tests in release builds, on both .NET Core 2.1 and .NET Core 3.0, and print out the results for comparison in a table.<\/p>\n<h3>Caveats A few caveats before we get started:<\/h3>\n<ol>\n<li>Any discussion involving microbenchmark results deserves a caveat that measurements can and do vary from machine to machine. I&#8217;ve tried to pick stable examples to share (and have run these tests on multiple machines in multiple configurations to help validate that), but don&#8217;t be too surprised if your numbers differ from the ones I&#8217;ve shown; hopefully, however, the magnitude of the improvements demonstrated carries through. All of the shown results are from a nightly Preview 6 build for .NET Core 3.0. Here&#8217;s my configuration, as summarized by Benchmark.NET, on my Windows configuration and on my Linux configuration: <\/li>\n<\/ol>\n<pre class=\"wrap:false lang:default decode:true\">BenchmarkDotNet=v0.11.5, OS=Windows 10.0.17763.437 (1809\/October2018Update\/Redstone5)\nIntel Core i7-7660U CPU 2.50GHz (Kaby Lake), 1 CPU, 4 logical and 2 physical cores\n.NET Core SDK=3.0.100-preview6-011854\n  [Host]     : .NET Core 2.1.9 (CoreCLR 4.6.27414.06, CoreFX 4.6.27415.01), 64bit RyuJIT\n  Job-RODBZD : .NET Core 2.1.9 (CoreCLR 4.6.27414.06, CoreFX 4.6.27415.01), 64bit RyuJIT\n  Job-TVOWAH : .NET Core 3.0.0-preview6-27712-03 (CoreCLR 3.0.19.26071, CoreFX 4.700.19.26005), 64bit RyuJIT\n\nBenchmarkDotNet=v0.11.5, OS=ubuntu 18.04\nIntel Xeon CPU E5-2673 v4 2.30GHz, 1 CPU, 4 logical and 2 physical cores\n.NET Core SDK=3.0.100-preview6-011877\n  [Host]     : .NET Core 2.1.10 (CoreCLR 4.6.27514.02, CoreFX 4.6.27514.02), 64bit RyuJIT\n  Job-SSHMNT : .NET Core 2.1.10 (CoreCLR 4.6.27514.02, CoreFX 4.6.27514.02), 64bit RyuJIT\n  Job-CHXNFO : .NET Core 3.0.0-preview6-27713-12 (CoreCLR 3.0.19.26071, CoreFX 4.700.19.26307), 64bit RyuJIT<\/pre>\n<ol>\n<li>Unless otherwise mentioned, benchmarks were executed on Windows. In many cases, performance is equivalent between Windows and Unix, but in others, there can be non-trivial discrepancies between them, in particular in places where .NET relies on OS functionality, and the OS itself has different performance characteristics.<\/li>\n<li>I mentioned posts on .NET Core 2.0 and .NET Core 2.1, but I didn&#8217;t mention .NET Core 2.2. .NET Core 2.2 was primarily focused on ASP.NET, and while there were terrific performance improvements at the ASP.NET layer in 2.2, the release was primarily focused on servicing for the runtime and core libraries, with most improvements post-2.1 skipping 2.2 and going into 3.0. With that out of the way, let&#8217;s have some fun. <\/li>\n<\/ol>\n<h3>Span and Friends<\/h3>\n<p>One of the more notable features introduced in .NET Core 2.1 was\u00a0<code>Span&lt;T&gt;<\/code>, along with its friends\u00a0<code>ReadOnlySpan&lt;T&gt;<\/code>,\u00a0<code>Memory&lt;T&gt;<\/code>, and\u00a0<code>ReadOnlyMemory&lt;T&gt;<\/code>. The introduction of these new types came with hundreds of new methods for interacting with them, some on new types and some with overloaded functionality on existing types, as well as optimizations in the just-in-time compiler (JIT) for making working with them very efficient. The release also included some internal usage of\u00a0<code>Span&lt;T&gt;<\/code>\u00a0to make existing operations leaner and faster while still enjoying maintainable and safe code. In .NET Core 3.0, much additional work has gone into further improving all such aspects of these types: making the runtime better at generating code for them, increasing the use of them internally to help improve many other operations, and improving the various library utilities that interact with them to make consumption of these operations faster. To work with a span, one first needs to get a span, and several PRs have made doing so faster. In particular, passing around a\u00a0<code>Memory&lt;T&gt;<\/code>\u00a0and then getting a\u00a0<code>Span&lt;T&gt;<\/code>\u00a0from it is a very common way of creating a span; this is, for example, how the various\u00a0<code>Stream.WriteAsync<\/code>\u00a0and\u00a0<code>ReadAsync<\/code>\u00a0methods work, accepting a\u00a0<code>{ReadOnly}Memory&lt;T&gt;<\/code>\u00a0(so that it can be stored on the heap) and then accessing its\u00a0<code>Span<\/code>\u00a0property once the actual bytes need to be read or written. PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/20771\">dotnet\/coreclr#20771<\/a>\u00a0improved this by removing an argument validation branch (both for\u00a0<code>{ReadOnly}Memory&lt;T&gt;.Span<\/code>\u00a0and for\u00a0<code>{ReadOnly}Span&lt;T&gt;.Slice<\/code>), and while removing a branch is a small thing, in span-heavy code (such as when doing formatting and parsing), small things done over and over and over again add up. More impactful, PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/20386\">dotnet\/coreclr#20386<\/a>\u00a0plays tricks at the runtime level to safely eliminate some of the runtime checked casting and bit masking logic that had been used to enable\u00a0<code>{ReadOnly}Memory&lt;T&gt;<\/code>\u00a0to wrap various types, like\u00a0<code>string<\/code>,\u00a0<code>T[]<\/code>, and\u00a0<code>MemoryManager&lt;T&gt;<\/code>, providing a seamless veneer over all of them. The net result of these PRs is a nice speed-up when fishing a\u00a0<code>Span&lt;T&gt;<\/code>\u00a0out of a\u00a0<code>Memory&lt;T&gt;<\/code>, which in turn improves all other operations that do so.<\/p>\n<pre class=\"lang:c# decode:true\">private ReadOnlyMemory&lt;byte&gt; _mem = new byte[1];\n\n[Benchmark]\npublic ReadOnlySpan&lt;byte&gt; GetSpan() =&gt; _mem.Span;<\/pre>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr style=\"height: 27px;\">\n<th style=\"width: 66px; height: 27px;\">\n        Method\n      <\/th>\n<th style=\"width: 106px; height: 27px;\">\n        Toolchain\n      <\/th>\n<th style=\"width: 64.6667px; height: 27px;\" align=\"right\">\n        Mean\n      <\/th>\n<th style=\"width: 73.3333px; height: 27px;\" align=\"right\">\n        Error\n      <\/th>\n<th style=\"width: 73.3333px; height: 27px;\" align=\"right\">\n        StdDev\n      <\/th>\n<th style=\"width: 41.3333px; height: 27px;\" align=\"right\">\n        Ratio\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"height: 27px;\">\n<td style=\"width: 66px; height: 27px;\">\n        GetSpan\n      <\/td>\n<td style=\"width: 106px; height: 27px;\">\n        netcoreapp2.1\n      <\/td>\n<td style=\"width: 64.6667px; height: 27px;\" align=\"right\">\n        3.873 ns\n      <\/td>\n<td style=\"width: 73.3333px; height: 27px;\" align=\"right\">\n        0.0927 ns\n      <\/td>\n<td style=\"width: 73.3333px; height: 27px;\" align=\"right\">\n        0.0822 ns\n      <\/td>\n<td style=\"width: 41.3333px; height: 27px;\" align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr style=\"height: 27px;\">\n<td style=\"width: 66px; height: 27px;\">\n        GetSpan\n      <\/td>\n<td style=\"width: 106px; height: 27px;\">\n        netcoreapp3.0\n      <\/td>\n<td style=\"width: 64.6667px; height: 27px;\" align=\"right\">\n        1.843 ns\n      <\/td>\n<td style=\"width: 73.3333px; height: 27px;\" align=\"right\">\n        0.0401 ns\n      <\/td>\n<td style=\"width: 73.3333px; height: 27px;\" align=\"right\">\n        0.0375 ns\n      <\/td>\n<td style=\"width: 41.3333px; height: 27px;\" align=\"right\">\n        0.48\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 Of course, once you get a span, you want to use it, and there are a myriad of ways to use one, many of which have also been optimized further in .NET Core 3.0. For example, just as with arrays, to pass the data from a span to native code via a P\/Invoke, the data needs to be pinned (unless it&#8217;s already immovable, such as if the span were created to wrap some natively allocated memory not on the GC heap or if it were created for some data on the stack). To pin a span, the easiest way is to simply rely on the C# language&#8217;s support added in C# 7.3 that supports a pattern-based way to use any type with the\u00a0<\/p>\n<p><code>fixed<\/code>\u00a0keyword. All a type need do is expose a\u00a0<code>GetPinnableReference<\/code>\u00a0method (or extension method) that returns a\u00a0<code>ref T<\/code>\u00a0to the data stored in that instance, and that type can be used with\u00a0<code>fixed<\/code>.\u00a0<code>{ReadOnly}Span&lt;T&gt;<\/code>\u00a0does exactly this. However, even though\u00a0<code>{ReadOnly}Span&lt;T&gt;.GetPinnableReference<\/code>\u00a0generally gets inlined, a call it makes internally to\u00a0<code>Unsafe.AsRef<\/code>\u00a0was getting blocked from inlining; PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/18274\">dotnet\/coreclr#18274<\/a>\u00a0fixed this, enabling the whole operation to be inlined. Further, the aforementioned code was actually tweaked in PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/20428\">dotnet\/coreclr#20428<\/a>\u00a0to eliminate one branch on the hot path. Both of these combine to result in a measurable boost when pinning a span:<\/p>\n<pre class=\"lang:c# decode:true\">private readonly byte[] _bytes = new byte[10_000];\n\n[Benchmark(OperationsPerInvoke = 10_000)]\npublic unsafe int PinSpan()\n{\n    Span&lt;byte&gt; s = _bytes;\n    int total = 0;\n\n    for (int i = 0; i &lt; s.Length; i++)\n        fixed (byte* p = s) \/\/ equivalent to `fixed (byte* p = &s.GetPinnableReference())`\n            total += *p;\n\n    return total;\n}<\/pre>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th style=\"width: 63.3333px;\">\n        Method\n      <\/th>\n<th style=\"width: 105.333px;\">\n        Toolchain\n      <\/th>\n<th style=\"width: 73.3333px;\" align=\"right\">\n        Mean\n      <\/th>\n<th style=\"width: 73.3333px;\" align=\"right\">\n        Error\n      <\/th>\n<th style=\"width: 73.3333px;\" align=\"right\">\n        StdDev\n      <\/th>\n<th style=\"width: 40.6667px;\" align=\"right\">\n        Ratio\n      <\/th>\n<th style=\"width: 62.6667px;\" align=\"right\">\n        RatioSD\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"width: 63.3333px;\">\n        PinSpan\n      <\/td>\n<td style=\"width: 105.333px;\">\n        netcoreapp2.1\n      <\/td>\n<td style=\"width: 73.3333px;\" align=\"right\">\n        0.7930 ns\n      <\/td>\n<td style=\"width: 73.3333px;\" align=\"right\">\n        0.0177 ns\n      <\/td>\n<td style=\"width: 73.3333px;\" align=\"right\">\n        0.0189 ns\n      <\/td>\n<td style=\"width: 40.6667px;\" align=\"right\">\n        1.00\n      <\/td>\n<td style=\"width: 62.6667px;\" align=\"right\">\n        0.00\n      <\/td>\n<\/tr>\n<tr>\n<td style=\"width: 63.3333px;\">\n        PinSpan\n      <\/td>\n<td style=\"width: 105.333px;\">\n        netcoreapp3.0\n      <\/td>\n<td style=\"width: 73.3333px;\" align=\"right\">\n        0.6496 ns\n      <\/td>\n<td style=\"width: 73.3333px;\" align=\"right\">\n        0.0109 ns\n      <\/td>\n<td style=\"width: 73.3333px;\" align=\"right\">\n        0.0102 ns\n      <\/td>\n<td style=\"width: 40.6667px;\" align=\"right\">\n        0.82\n      <\/td>\n<td style=\"width: 62.6667px;\" align=\"right\">\n        0.03\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 It&#8217;s worth noting, as well, that if you&#8217;re interested in these kinds of micro-optimizations, you might also want to avoid using the default pinning at all, at least on super hot paths. The\u00a0<\/p>\n<p><code>{ReadOnly}Span&lt;T&gt;.GetPinnableReference<\/code>\u00a0method was designed to behave just like pinning of arrays and strings, where null or empty inputs result in a null pointer. This behavior requires an additional check to be performed to see whether the length of the span is zero:<\/p>\n<pre class=\"wrap:false lang:c# decode:true\">\/\/ https:\/\/github.com\/dotnet\/coreclr\/blob\/52aff202cd382c233d903d432da06deffaa21868\/src\/System.Private.CoreLib\/shared\/System\/Span.Fast.cs#L168-L174\n\n[EditorBrowsable(EditorBrowsableState.Never)]\npublic unsafe ref T GetPinnableReference()\n{\n    \/\/ Ensure that the native code has just one forward branch that is predicted-not-taken.\n    ref T ret = ref Unsafe.AsRef&lt;T&gt;(null);\n    if (_length != 0) ret = ref _pointer.Value;\n    return ref ret;\n}<\/pre>\n<p>If in your code by construction you know that the span will not be empty, you can choose to instead use\u00a0<\/p>\n<p><code>MemoryMarshal.GetReference<\/code>, which performs the same operation but without the length check:<\/p>\n<pre class=\"wrap:false lang:c# decode:true\">\/\/ https:\/\/github.com\/dotnet\/coreclr\/blob\/52aff202cd382c233d903d432da06deffaa21868\/src\/System.Private.CoreLib\/shared\/System\/Runtime\/InteropServices\/MemoryMarshal.Fast.cs#L79\n\npublic static ref T GetReference&lt;T&gt;(Span&lt;T&gt; span) =&gt; ref span._pointer.Value;<\/pre>\n<p>Again, while a single check adds minor overhead, when executed over and over and over, that can add up:<\/p>\n<pre class=\"lang:c# decode:true \">private readonly byte[] _bytes = new byte[10_000];\n\n[Benchmark(OperationsPerInvoke = 10_000, Baseline = true)]\npublic unsafe int PinSpan()\n{\n    Span&lt;byte&gt; s = _bytes;\n    int total = 0;\n\n    for (int i = 0; i &lt; s.Length; i++)\n        fixed (byte* p = s) \/\/ equivalent to `fixed (byte* p = &s.GetPinnableReference())`\n            total += *p;\n\n    return total;\n}\n\n[Benchmark(OperationsPerInvoke = 10_000)]\npublic unsafe int PinSpanExplicit()\n{\n    Span&lt;byte&gt; s = _bytes;\n    int total = 0;\n\n    for (int i = 0; i &lt; s.Length; i++)\n        fixed (byte* p = &MemoryMarshal.GetReference(s))\n            total += *p;\n\n    return total;\n}<\/pre>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        RatioSD\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        PinSpan\n      <\/td>\n<td align=\"right\">\n        0.6524 ns\n      <\/td>\n<td align=\"right\">\n        0.0129 ns\n      <\/td>\n<td align=\"right\">\n        0.0159 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        PinSpanExplicit\n      <\/td>\n<td align=\"right\">\n        0.5200 ns\n      <\/td>\n<td align=\"right\">\n        0.0111 ns\n      <\/td>\n<td align=\"right\">\n        0.0140 ns\n      <\/td>\n<td align=\"right\">\n        0.80\n      <\/td>\n<td align=\"right\">\n        0.03\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 Of course, there are many other (and generally preferred) ways to operate over a span&#8217;s data than to use\u00a0<\/p>\n<p><code>fixed<\/code>. For example, it&#8217;s a bit surprising that until\u00a0<code>Span&lt;T&gt;<\/code>\u00a0came along, .NET didn&#8217;t have a built-in equivalent of\u00a0<code>memcmp<\/code>, but nevertheless,\u00a0<code>Span&lt;T&gt;<\/code>&#8216;s\u00a0<code>SequenceEqual<\/code>\u00a0and\u00a0<code>SequenceCompareTo<\/code>\u00a0methods have become go-to methods for comparing in-memory data in .NET. In .NET Core 2.1, both\u00a0<code>SequenceEqual<\/code>\u00a0and\u00a0<code>SequenceCompareTo<\/code>\u00a0were optimized to utilize\u00a0<code>System.Numerics.Vector<\/code>\u00a0for vectorization, but the nature of\u00a0<code>SequenceEqual<\/code>\u00a0made it more amenable to best take advantage. In PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/22127\">dotnet\/coreclr#22127<\/a>, @benaadams updated\u00a0<code>SequenceCompareTo<\/code>\u00a0to take advantage of the new hardware instrinsics APIs available in .NET Core 3.0 to specifically target AVX2 and SSE2, resulting in significant improvements when comparing both small and large spans. (For more information on hardware intrinsics in .NET Core 3.0, see\u00a0<a href=\"https:\/\/github.com\/dotnet\/designs\/blob\/master\/accepted\/platform-intrinsics.md\">platform-intrinsics.md<\/a>\u00a0and\u00a0<a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/using-net-hardware-intrinsics-api-to-accelerate-machine-learning-scenarios\/\" rel=\"nofollow\">using-net-hardware-intrinsics-api-to-accelerate-machine-learning-scenarios<\/a>.)<\/p>\n<pre class=\"lang:c# decode:true\">private byte[] _orig, _same, _differFirst, _differLast;\n\n[Params(16, 256)]\npublic int Length { get; set; }\n\n[GlobalSetup]\npublic void Setup()\n{\n    _orig = Enumerable.Range(0, Length).Select(i =&gt; (byte)i).ToArray();\n    _same = (byte[])_orig.Clone();\n\n    _differFirst = (byte[])_orig.Clone();\n    _differFirst[0] = (byte)(_orig[0] + 1);\n\n    _differLast = (byte[])_orig.Clone();\n    _differLast[_differLast.Length - 1] = (byte)(_orig[_orig.Length - 1] + 1);\n}\n\n[Benchmark]\npublic int CompareSame() =&gt; _orig.AsSpan().SequenceCompareTo(_same);\n\n[Benchmark]\npublic int CompareDifferFirst() =&gt; _orig.AsSpan().SequenceCompareTo(_differFirst);\n\n[Benchmark]\npublic int CompareDifferLast() =&gt; _orig.AsSpan().SequenceCompareTo(_differLast);<\/pre>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th>\n        Length\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        CompareSame\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td>\n        16\n      <\/td>\n<td align=\"right\">\n        16.955 ns\n      <\/td>\n<td align=\"right\">\n        0.2009 ns\n      <\/td>\n<td align=\"right\">\n        0.1781 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        CompareSame\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td>\n        16\n      <\/td>\n<td align=\"right\">\n        4.757 ns\n      <\/td>\n<td align=\"right\">\n        0.0938 ns\n      <\/td>\n<td align=\"right\">\n        0.0732 ns\n      <\/td>\n<td align=\"right\">\n        0.28\n      <\/td>\n<\/tr>\n<tr>\n<td>\n      <\/td>\n<td>\n      <\/td>\n<td>\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        CompareDifferFirst\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td>\n        16\n      <\/td>\n<td align=\"right\">\n        11.874 ns\n      <\/td>\n<td align=\"right\">\n        0.1240 ns\n      <\/td>\n<td align=\"right\">\n        0.1100 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        CompareDifferFirst\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td>\n        16\n      <\/td>\n<td align=\"right\">\n        5.174 ns\n      <\/td>\n<td align=\"right\">\n        0.0543 ns\n      <\/td>\n<td align=\"right\">\n        0.0508 ns\n      <\/td>\n<td align=\"right\">\n        0.44\n      <\/td>\n<\/tr>\n<tr>\n<td>\n      <\/td>\n<td>\n      <\/td>\n<td>\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        CompareDifferLast\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td>\n        16\n      <\/td>\n<td align=\"right\">\n        16.644 ns\n      <\/td>\n<td align=\"right\">\n        0.2146 ns\n      <\/td>\n<td align=\"right\">\n        0.2007 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        CompareDifferLast\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td>\n        16\n      <\/td>\n<td align=\"right\">\n        5.373 ns\n      <\/td>\n<td align=\"right\">\n        0.0479 ns\n      <\/td>\n<td align=\"right\">\n        0.0448 ns\n      <\/td>\n<td align=\"right\">\n        0.32\n      <\/td>\n<\/tr>\n<tr>\n<td>\n      <\/td>\n<td>\n      <\/td>\n<td>\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        CompareSame\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td>\n        256\n      <\/td>\n<td align=\"right\">\n        43.740 ns\n      <\/td>\n<td align=\"right\">\n        0.8226 ns\n      <\/td>\n<td align=\"right\">\n        0.7292 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        CompareSame\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td>\n        256\n      <\/td>\n<td align=\"right\">\n        11.055 ns\n      <\/td>\n<td align=\"right\">\n        0.1625 ns\n      <\/td>\n<td align=\"right\">\n        0.1441 ns\n      <\/td>\n<td align=\"right\">\n        0.25\n      <\/td>\n<\/tr>\n<tr>\n<td>\n      <\/td>\n<td>\n      <\/td>\n<td>\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        CompareDifferFirst\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td>\n        256\n      <\/td>\n<td align=\"right\">\n        12.144 ns\n      <\/td>\n<td align=\"right\">\n        0.0849 ns\n      <\/td>\n<td align=\"right\">\n        0.0752 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        CompareDifferFirst\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td>\n        256\n      <\/td>\n<td align=\"right\">\n        6.663 ns\n      <\/td>\n<td align=\"right\">\n        0.1044 ns\n      <\/td>\n<td align=\"right\">\n        0.0977 ns\n      <\/td>\n<td align=\"right\">\n        0.55\n      <\/td>\n<\/tr>\n<tr>\n<td>\n      <\/td>\n<td>\n      <\/td>\n<td>\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        CompareDifferLast\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td>\n        256\n      <\/td>\n<td align=\"right\">\n        39.697 ns\n      <\/td>\n<td align=\"right\">\n        0.9291 ns\n      <\/td>\n<td align=\"right\">\n        2.6054 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        CompareDifferLast\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td>\n        256\n      <\/td>\n<td align=\"right\">\n        11.242 ns\n      <\/td>\n<td align=\"right\">\n        0.2218 ns\n      <\/td>\n<td align=\"right\">\n        0.1732 ns\n      <\/td>\n<td align=\"right\">\n        0.32\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 As background, &#8220;vectorization&#8221; is an approach to parallelization that performs multiple operations as part of individual instructions on a single core. Some optimizing compilers can perform automatic vectorization, whereby the compiler analyzes loops to determine whether it can generate functionally equivalent code that would utilize such instructions to run faster. The .NET JIT compiler does not currently perform auto-vectorization, but it is possible to manually vectorize loops, and the options for doing so have significantly improved in .NET Core 3.0. Just as a simple example of what vectorization can look like, imagine having an array of bytes and wanting to search it for the first non-zero byte, returning the position of that byte. The simple solution is to just iterate through all of the bytes:<\/p>\n<pre class=\"lang:c# decode:true\">private byte[] _buffer = new byte[10_000].Concat(new byte[] { 42 }).ToArray();\n\n[Benchmark(Baseline = true)]\npublic int LoopBytes()\n{\n    byte[] buffer = _buffer;\n    for (int i = 0; i &lt; buffer.Length; i++)\n    {\n        if (buffer[i] != 0)\n            return i;\n    }\n    return -1;\n}<\/pre>\n<p>That of course works functionally, and for very small arrays it&#8217;s fine. But for larger arrays, we end up doing significantly more work than is actually necessary. Consider instead in a 64-bit process re-interpreting the array of bytes as an array of longs, which\u00a0<\/p>\n<p><code>Span&lt;T&gt;<\/code>\u00a0nicely supports. We then effectively compare 8 bytes at a time rather than 1 byte at a time, at the expense of added code complexity: once we find a non-zero long, we then need to look at each byte it contains to determine the position of the first non-zero one (though there are ways to improve that, too). Similarly, the array&#8217;s length may not evenly divide by 8, so we need to be able to handle the overflow.<\/p>\n<pre class=\"lang:c# decode:true\">[Benchmark]\npublic int LoopLongs()\n{\n    byte[] buffer = _buffer;\n    int remainingStart = 0;\n\n    if (IntPtr.Size == sizeof(long))\n    {\n        Span&lt;long&gt; longBuffer = MemoryMarshal.Cast&lt;byte, long&gt;(buffer);\n        remainingStart = longBuffer.Length * sizeof(long);\n\n        for (int i = 0; i &lt; longBuffer.Length; i++)\n        {\n            if (longBuffer[i] != 0)\n            {\n                remainingStart = i * sizeof(long);\n                break;\n            }\n        }\n    }\n\n    for (int i = remainingStart; i &lt; buffer.Length; i++)\n    {\n        if (buffer[i] != 0)\n            return i;\n    }\n\n    return -1;\n}<\/pre>\n<p>For longer arrays, this yields really nice wins:<\/p>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr style=\"height: 27px;\">\n<th style=\"height: 27px; width: 82px;\">\n        Method\n      <\/th>\n<th style=\"height: 27px; width: 78px;\" align=\"right\">\n        Mean\n      <\/th>\n<th style=\"height: 27px; width: 82px;\" align=\"right\">\n        Error\n      <\/th>\n<th style=\"height: 27px; width: 82px;\" align=\"right\">\n        StdDev\n      <\/th>\n<th style=\"height: 27px; width: 40.6667px;\" align=\"right\">\n        Ratio\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"height: 27px;\">\n<td style=\"height: 27px; width: 82px;\">\n        LoopBytes\n      <\/td>\n<td style=\"height: 27px; width: 78px;\" align=\"right\">\n        5,462.3 ns\n      <\/td>\n<td style=\"height: 27px; width: 82px;\" align=\"right\">\n        107.093 ns\n      <\/td>\n<td style=\"height: 27px; width: 82px;\" align=\"right\">\n        105.180 ns\n      <\/td>\n<td style=\"height: 27px; width: 40.6667px;\" align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr style=\"height: 27px;\">\n<td style=\"height: 27px; width: 82px;\">\n        LoopLongs\n      <\/td>\n<td style=\"height: 27px; width: 78px;\" align=\"right\">\n        568.6 ns\n      <\/td>\n<td style=\"height: 27px; width: 82px;\" align=\"right\">\n        6.895 ns\n      <\/td>\n<td style=\"height: 27px; width: 82px;\" align=\"right\">\n        5.758 ns\n      <\/td>\n<td style=\"height: 27px; width: 40.6667px;\" align=\"right\">\n        0.10\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 I&#8217;ve glossed over some details here, but it should convey the core idea. .NET includes additional mechanisms for vectorizing as well. In particular, the aforementioned\u00a0<\/p>\n<p><code>System.Numerics.Vector<\/code>\u00a0type allows for a developer to write code using\u00a0<code>Vector<\/code>\u00a0and then have the JIT compiler translate that into the best instructions available on the current platform.<\/p>\n<pre class=\"tab-convert:true lang:c# decode:true \">[Benchmark]\npublic int LoopVectors()\n{\n    byte[] buffer = _buffer;\n    int remainingStart = 0;\n\n    if (Vector.IsHardwareAccelerated)\n    {\n        while (remainingStart &lt;= buffer.Length - Vector&lt;byte&gt;.Count)\n        {\n            var vector = new Vector&lt;byte&gt;(buffer, remainingStart);\n            if (!Vector.EqualsAll(vector, default))\n            {\n                break;\n            }\n            remainingStart += Vector&lt;byte&gt;.Count;\n        }\n    }\n\n    for (int i = remainingStart; i &lt; buffer.Length; i++)\n    {\n        if (buffer[i] != 0)\n            return i;\n    }\n\n    return -1;\n}<\/pre>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        LoopBytes\n      <\/td>\n<td align=\"right\">\n        5,462.3 ns\n      <\/td>\n<td align=\"right\">\n        107.093 ns\n      <\/td>\n<td align=\"right\">\n        105.180 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        LoopLongs\n      <\/td>\n<td align=\"right\">\n        568.6 ns\n      <\/td>\n<td align=\"right\">\n        6.895 ns\n      <\/td>\n<td align=\"right\">\n        5.758 ns\n      <\/td>\n<td align=\"right\">\n        0.10\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        LoopVectors\n      <\/td>\n<td align=\"right\">\n        306.0 ns\n      <\/td>\n<td align=\"right\">\n        4.502 ns\n      <\/td>\n<td align=\"right\">\n        4.211 ns\n      <\/td>\n<td align=\"right\">\n        0.06\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 Further, .NET Core 3.0 includes new hardware intrinsics that allow a properly-motivated developer to eke out the best possible performance on supporting hardware, utilizing extensions like AVX or SSE that can compare well more than 8 bytes at a time. Many of the improvements in .NET Core 3.0 come from utilizing these techniques. Back to examples, copying spans has also improved, thanks to PRs\u00a0<\/p>\n<p><a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/18006\">dotnet\/coreclr#18006<\/a>\u00a0from @benaadams and\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/17889\">dotnet\/coreclr#17889<\/a>, in particular for relatively small spans&#8230;<\/p>\n<pre class=\"lang:c# decode:true\">private byte[] _from = new byte[] { 1, 2, 3, 4 };\nprivate byte[] _to = new byte[4];\n\n[Benchmark]\npublic void CopySpan() =&gt; _from.AsSpan().CopyTo(_to);<\/pre>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        CopySpan\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        10.913 ns\n      <\/td>\n<td align=\"right\">\n        0.1960 ns\n      <\/td>\n<td align=\"right\">\n        0.1737 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        CopySpan\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        3.568 ns\n      <\/td>\n<td align=\"right\">\n        0.0528 ns\n      <\/td>\n<td align=\"right\">\n        0.0494 ns\n      <\/td>\n<td align=\"right\">\n        0.33\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 Searching is one of the most commonly performed operations in any program, and searches with spans are generally performed with\u00a0<\/p>\n<p><code>IndexOf<\/code>\u00a0and its variants (e.g.\u00a0<code>IndexOfAny<\/code>\u00a0and\u00a0<code>Contains<\/code>) In PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/20738\">dotnet\/coreclr#20738<\/a>, @benaadams again utilized vectorization, this time to improve the performance of\u00a0<code>IndexOfAny<\/code>\u00a0when operating over bytes, a particularly common case in many networking-related scenarios (e.g. parsing bytes off the wire as part of an HTTP stack). You can see the effects of this in the following microbenchmark:<\/p>\n<pre class=\"wrap:false lang:c# decode:true\">private byte[] _arr = Encoding.UTF8.GetBytes(\"This is a test to see improvements to IndexOfAny.  How'd they work?\");\n[Benchmark] public int IndexOf() =&gt; new Span&lt;byte&gt;(_arr).IndexOfAny((byte)'.', (byte)'?');<\/pre>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        IndexOf\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        12.828 ns\n      <\/td>\n<td align=\"right\">\n        0.1805 ns\n      <\/td>\n<td align=\"right\">\n        0.1600 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        IndexOf\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        4.504 ns\n      <\/td>\n<td align=\"right\">\n        0.0968 ns\n      <\/td>\n<td align=\"right\">\n        0.0858 ns\n      <\/td>\n<td align=\"right\">\n        0.35\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 I love these kinds of improvements, because they&#8217;re low-enough in the stack that they end up having multiplicative effects across so much code. The above change only affected\u00a0<\/p>\n<p><code>byte<\/code>, but subsequent PRs were submitted to cover\u00a0<code>char<\/code>\u00a0as well, and then PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/20855\">dotnet\/coreclr#20855<\/a>\u00a0made a nice change that brought these same changes to other primitives of the same sizes. For example, we can recast the previous benchmark to use sbyte instead of byte, and as of that PR, a similar improvement applies:<\/p>\n<pre class=\"wrap:false lang:c# decode:true\">private sbyte[] _arr = Encoding.UTF8.GetBytes(\"This is a test to see improvements to IndexOfAny.  How'd they work?\").Select(b =&gt; (sbyte)b).ToArray();\n\n[Benchmark]\npublic int IndexOf() =&gt; new Span&lt;sbyte&gt;(_arr).IndexOfAny((sbyte)'.', (sbyte)'?');<\/pre>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        IndexOf\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        24.636 ns\n      <\/td>\n<td align=\"right\">\n        0.2292 ns\n      <\/td>\n<td align=\"right\">\n        0.2144 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        IndexOf\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        9.795 ns\n      <\/td>\n<td align=\"right\">\n        0.1419 ns\n      <\/td>\n<td align=\"right\">\n        0.1258 ns\n      <\/td>\n<td align=\"right\">\n        0.40\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 As another example, consider PR\u00a0<\/p>\n<p><a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/20275\">dotnet\/coreclr#20275<\/a>. That change similarly utilized vectorization to improve the performance of To{Upper\/Lower}{Invariant}.<\/p>\n<pre class=\"lang:c# decode:true\">private string _src = \"This is a source string that needs to be capitalized.\";\nprivate char[] _dst = new char[1024];\n[Benchmark] public int ToUpperInvariant() =&gt; _src.AsSpan().ToUpperInvariant(_dst);<\/pre>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        ToUpperInvariant\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        64.36 ns\n      <\/td>\n<td align=\"right\">\n        0.8099 ns\n      <\/td>\n<td align=\"right\">\n        0.6763 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        ToUpperInvariant\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        26.48 ns\n      <\/td>\n<td align=\"right\">\n        0.2411 ns\n      <\/td>\n<td align=\"right\">\n        0.2137 ns\n      <\/td>\n<td align=\"right\">\n        0.41\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 PR\u00a0<\/p>\n<p><a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/19959\">dotnet\/coreclr#19959<\/a>\u00a0optimizes the Trim{Start\/End} helpers on\u00a0<code>ReadOnlySpan&lt;char&gt;<\/code>, another very commonly-applied method, with equally exciting results (it&#8217;s hard to see with the white space in the results, but the results in the table go in order of the arguments in the Params attribute):<\/p>\n<pre class=\"lang:default decode:true\">[Params(\"\", \" abcdefg \", \"abcdefg\")]\npublic string Data;\n\n[Benchmark]\npublic ReadOnlySpan&lt;char&gt; Trim() =&gt; Data.AsSpan().Trim();<\/pre>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th>\n        Data\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        Trim\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td>\n      <\/td>\n<td align=\"right\">\n        12.999 ns\n      <\/td>\n<td align=\"right\">\n        0.1913 ns\n      <\/td>\n<td align=\"right\">\n        0.1789 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        Trim\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td>\n      <\/td>\n<td align=\"right\">\n        3.078 ns\n      <\/td>\n<td align=\"right\">\n        0.0349 ns\n      <\/td>\n<td align=\"right\">\n        0.0326 ns\n      <\/td>\n<td align=\"right\">\n        0.24\n      <\/td>\n<\/tr>\n<tr>\n<td>\n      <\/td>\n<td>\n      <\/td>\n<td>\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        Trim\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td>\n        abcdefg\n      <\/td>\n<td align=\"right\">\n        17.618 ns\n      <\/td>\n<td align=\"right\">\n        0.3534 ns\n      <\/td>\n<td align=\"right\">\n        0.2951 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        Trim\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td>\n        abcdefg\n      <\/td>\n<td align=\"right\">\n        7.927 ns\n      <\/td>\n<td align=\"right\">\n        0.0934 ns\n      <\/td>\n<td align=\"right\">\n        0.0828 ns\n      <\/td>\n<td align=\"right\">\n        0.45\n      <\/td>\n<\/tr>\n<tr>\n<td>\n      <\/td>\n<td>\n      <\/td>\n<td>\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        Trim\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td>\n        abcdefg\n      <\/td>\n<td align=\"right\">\n        15.522 ns\n      <\/td>\n<td align=\"right\">\n        0.2200 ns\n      <\/td>\n<td align=\"right\">\n        0.1951 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        Trim\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td>\n        abcdefg\n      <\/td>\n<td align=\"right\">\n        5.227 ns\n      <\/td>\n<td align=\"right\">\n        0.0750 ns\n      <\/td>\n<td align=\"right\">\n        0.0665 ns\n      <\/td>\n<td align=\"right\">\n        0.34\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 Sometimes optimizations are just about being smarter about code management. PR\u00a0<\/p>\n<p><a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/17890\">dotnet\/coreclr#17890<\/a>\u00a0removed an unnecessary layer of functions that were on many globalization-related code paths, and just removing those extra unnecessary method invocations results in measurable speed-ups when working with small spans, e.g.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"wrap:false lang:default decode:true\">[Benchmark]\npublic bool EndsWith() =&gt; \"Hello world\".AsSpan().EndsWith(\"world\", StringComparison.OrdinalIgnoreCase);<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        EndsWith\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        37.80 ns\n      <\/td>\n<td align=\"right\">\n        0.3290 ns\n      <\/td>\n<td align=\"right\">\n        0.2917 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        EndsWith\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        12.26 ns\n      <\/td>\n<td align=\"right\">\n        0.1479 ns\n      <\/td>\n<td align=\"right\">\n        0.1384 ns\n      <\/td>\n<td align=\"right\">\n        0.32\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 Of course, one of the great things about span is that it is a reusable building-block that enables many higher-level operations. That includes operations on both arrays and strings&#8230;<\/p>\n<h3>Arrays and Strings<\/h3>\n<p>As a theme that&#8217;s emerged within .NET Core, wherever possible, new performance-focused functionality should not only be exposed for public use but also be used internally; after all, given the depth and breadth of functionality within .NET Core, if some performance-focused feature doesn&#8217;t meet the needs of .NET Core itself, there&#8217;s a reasonable chance it also won&#8217;t meet the public need. As such, internal usage of new features is a key benchmark as to whether the design is adequate, and in the process of evaluating such criteria, many additional code paths benefit, and these improvements have a multiplicative effect. This isn&#8217;t just about new APIs. Many of the language features introduced in C# 7.2, 7.3, and 8.0 are influenced by the needs of .NET Core itself and have been used to improve things that we couldn&#8217;t reasonably improve before (other than dropping down to unsafe code, which we try to avoid when possible). For example, PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/17891\">dotnet\/coreclr#17891<\/a>\u00a0speeds up Array.Reverse by taking advantage of the C# 7.2 ref locals feature and the 7.3 ref local reassignment feature. Using the new feature allows for the code to be expressed in a way that lets the JIT generate better code for the inner loop, and in turn results in a measurable speed-up:<\/p>\n<pre class=\"lang:default decode:true\">private int[] _arr = Enumerable.Range(0, 256).ToArray();\n\n[Benchmark]\npublic void Reverse() =&gt; Array.Reverse(_arr);<\/pre>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        RatioSD\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        Reverse\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        105.06 ns\n      <\/td>\n<td align=\"right\">\n        2.488 ns\n      <\/td>\n<td align=\"right\">\n        7.337 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        Reverse\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        74.12 ns\n      <\/td>\n<td align=\"right\">\n        1.494 ns\n      <\/td>\n<td align=\"right\">\n        2.536 ns\n      <\/td>\n<td align=\"right\">\n        0.66\n      <\/td>\n<td align=\"right\">\n        0.02\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 Another example for arrays, the\u00a0<\/p>\n<p><code>Clear<\/code>\u00a0method improved in PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/24302\">dotnet\/coreclr#24302<\/a>, which works around an alignment issue that results in the underlying memset used to implement the operation being up to 2x slower. The change manually clears up to a few bytes one by one, such that the pointer we then hand off to memset is properly aligned. If you got &#8220;lucky&#8221; previously and the array happened to be aligned, performance was fine, but if it wasn&#8217;t aligned, there was a non-trivial performance hit incurred. This benchmark simulates the unlucky case:<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">[GlobalSetup]\npublic void Setup()\n{\n    while (true)\n    {\n        var buffer = new byte[8192];\n        GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);\n        if (((long)handle.AddrOfPinnedObject()) % 32 != 0)\n        {\n            _handle = handle;\n            _buffer = buffer;\n            return;\n        }\n        handle.Free();\n    }\n}\n\n[GlobalCleanup]\npublic void Cleanup() =&gt; _handle.Free();\n\nprivate GCHandle _handle;\nprivate byte[] _buffer;\n\n[Benchmark] public void Clear() =&gt; Array.Clear(_buffer, 0, _buffer.Length);<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        Clear\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        121.59 ns\n      <\/td>\n<td align=\"right\">\n        0.8349 ns\n      <\/td>\n<td align=\"right\">\n        0.6519 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        Clear\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        87.91 ns\n      <\/td>\n<td align=\"right\">\n        1.7768 ns\n      <\/td>\n<td align=\"right\">\n        1.6620 ns\n      <\/td>\n<td align=\"right\">\n        0.73\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 That said, many of the improvements are in fact based on new APIs. Span is a great example of this. It was introduced in .NET Core 2.1, and the initial push was to get it to be usable and expose sufficient surface area to allow it to be used meaningfully. But at the same time, we started utilizing it internally in order to both vet the design and benefit from the improvements it enables. Some of this was done in .NET Core 2.1, but the effort continues in .NET Core 3.0. Arrays and strings are both prime candidates for such optimizations. For example, many of the same vectorization optimizations applied to spans are similarly applied to arrays. PR\u00a0<\/p>\n<p><a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/21116\">dotnet\/coreclr#21116<\/a>\u00a0from @benaadams optimized\u00a0<code>Array.{Last}IndexOf<\/code>\u00a0for both\u00a0<code>byte<\/code>s and\u00a0<code>char<\/code>s, utilizing the same internal helpers that were written to enable spans, and to similar effect:<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">private char[] _arr = \"This is a test to see improvements to IndexOf.  How'd they work?\".ToCharArray();\n\n[Benchmark]\npublic int IndexOf() =&gt; Array.IndexOf(_arr, '.');<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        RatioSD\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        IndexOf\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        34.976 ns\n      <\/td>\n<td align=\"right\">\n        0.6352 ns\n      <\/td>\n<td align=\"right\">\n        0.5631 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        IndexOf\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        9.471 ns\n      <\/td>\n<td align=\"right\">\n        0.6638 ns\n      <\/td>\n<td align=\"right\">\n        1.1091 ns\n      <\/td>\n<td align=\"right\">\n        0.29\n      <\/td>\n<td align=\"right\">\n        0.04\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 And as with spans, thanks to PR\u00a0<\/p>\n<p><a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/24293\">dotnet\/coreclr#24293<\/a>\u00a0from @dschinde, these\u00a0<code>IndexOf<\/code>optimizations also now apply to other primitives of the same size.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">private short[] _arr = \"This is a test to see improvements to IndexOf.  How'd they work?\".Select(c =&gt; (short)c).ToArray();\n\n[Benchmark]\npublic int IndexOf() =&gt; Array.IndexOf(_arr, (short)'.');<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        IndexOf\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        34.181 ns\n      <\/td>\n<td align=\"right\">\n        0.6626 ns\n      <\/td>\n<td align=\"right\">\n        0.6508 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        IndexOf\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        9.600 ns\n      <\/td>\n<td align=\"right\">\n        0.1913 ns\n      <\/td>\n<td align=\"right\">\n        0.1598 ns\n      <\/td>\n<td align=\"right\">\n        0.28\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 Vectorization optimizations have been applied to strings, too. You can see the effect of PR\u00a0<\/p>\n<p><a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/21076\">dotnet\/coreclr#21076<\/a>\u00a0from @benaadams in this microbenchmark:<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"wrap:false lang:default decode:true\">[Benchmark]\npublic int IndexOf() =&gt; \"Let's see how fast we can find the period towards the end of this string.  Pretty fast?\".IndexOf('.', StringComparison.Ordinal);<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<th align=\"right\">\n        Allocated\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        IndexOf\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        75.14 ns\n      <\/td>\n<td align=\"right\">\n        1.5285 ns\n      <\/td>\n<td align=\"right\">\n        1.6355 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.0151\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        32 B\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        IndexOf\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        11.70 ns\n      <\/td>\n<td align=\"right\">\n        0.2382 ns\n      <\/td>\n<td align=\"right\">\n        0.2111 ns\n      <\/td>\n<td align=\"right\">\n        0.16\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 Also note in the above that the .NET Core 2.1 operation allocates (due to converting the search character into a string), whereas the .NET Core 3.0 implementation does not. That&#8217;s thanks to PR\u00a0<\/p>\n<p><a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/19788\">dotnet\/coreclr#19788<\/a> from @benaadams. There are of course pieces of functionality that are more unique to strings (albeit also applicable to new functionality exposed on spans), such as hash code computation with various string comparison methods. For example, PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/20309\/\">dotnet\/coreclr#20309\/<\/a>\u00a0improved the performance of\u00a0<code>String.GetHashCode<\/code>\u00a0when performing\u00a0<code>OrdinalIgnoreCase<\/code>\u00a0operations, which along with\u00a0<code>Ordinal<\/code>\u00a0(the default) represent the two most common modes.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">[Benchmark]\npublic int GetHashCodeIgnoreCase() =&gt; \"Some string\".GetHashCode(StringComparison.OrdinalIgnoreCase);<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        GetHashCodeIgnoreCase\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        47.70 ns\n      <\/td>\n<td align=\"right\">\n        0.5751 ns\n      <\/td>\n<td align=\"right\">\n        0.5380 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        GetHashCodeIgnoreCase\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        14.28 ns\n      <\/td>\n<td align=\"right\">\n        0.1462 ns\n      <\/td>\n<td align=\"right\">\n        0.1296 ns\n      <\/td>\n<td align=\"right\">\n        0.30\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0<\/p>\n<p><code>OrdinalsIgnoreCase<\/code>\u00a0has been improved for other uses as well. For example, PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/20734\">dotnet\/coreclr#20734<\/a>\u00a0improved\u00a0<code>String.Equals<\/code>\u00a0when using\u00a0<code>StringComparer.OrdinalIgnoreCase<\/code>by both vectorizing (checking two chars at a time instead of one) and removing branches from an inner loop:<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">[Benchmark]\npublic bool EqualsIC() =&gt; \"Some string\".Equals(\"sOME sTrinG\", StringComparison.OrdinalIgnoreCase);<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        EqualsIC\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        24.036 ns\n      <\/td>\n<td align=\"right\">\n        0.3819 ns\n      <\/td>\n<td align=\"right\">\n        0.3572 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        EqualsIC\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        9.165 ns\n      <\/td>\n<td align=\"right\">\n        0.0589 ns\n      <\/td>\n<td align=\"right\">\n        0.0551 ns\n      <\/td>\n<td align=\"right\">\n        0.38\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 The previous cases are examples of functionality in\u00a0<\/p>\n<p><code>String<\/code>&#8216;s implementation, but there are lots of ancillary string-related functionality that have seen improvements as well. For example, various operations on\u00a0<code>Char<\/code>\u00a0have been improved, such as\u00a0<code>Char.GetUnicodeCategory<\/code>\u00a0via PRs\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/20983\">dotnet\/coreclr#20983<\/a>\u00a0and\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/20864\">dotnet\/coreclr#20864<\/a>:<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">[Params('.', 'a', '\\x05D0')]\npublic char Char { get; set; }\n\n[Benchmark]\npublic UnicodeCategory GetCategory() =&gt; char.GetUnicodeCategory(Char);<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th>\n        Char\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        RatioSD\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        GetCategory\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td>\n        .\n      <\/td>\n<td align=\"right\">\n        1.8001 ns\n      <\/td>\n<td align=\"right\">\n        0.0160 ns\n      <\/td>\n<td align=\"right\">\n        0.0142 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        GetCategory\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td>\n        .\n      <\/td>\n<td align=\"right\">\n        0.4925 ns\n      <\/td>\n<td align=\"right\">\n        0.0141 ns\n      <\/td>\n<td align=\"right\">\n        0.0132 ns\n      <\/td>\n<td align=\"right\">\n        0.27\n      <\/td>\n<td align=\"right\">\n        0.01\n      <\/td>\n<\/tr>\n<tr>\n<td>\n      <\/td>\n<td>\n      <\/td>\n<td>\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        GetCategory\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td>\n        a\n      <\/td>\n<td align=\"right\">\n        1.7925 ns\n      <\/td>\n<td align=\"right\">\n        0.0144 ns\n      <\/td>\n<td align=\"right\">\n        0.0127 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        GetCategory\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td>\n        a\n      <\/td>\n<td align=\"right\">\n        0.4957 ns\n      <\/td>\n<td align=\"right\">\n        0.0117 ns\n      <\/td>\n<td align=\"right\">\n        0.0091 ns\n      <\/td>\n<td align=\"right\">\n        0.28\n      <\/td>\n<td align=\"right\">\n        0.01\n      <\/td>\n<\/tr>\n<tr>\n<td>\n      <\/td>\n<td>\n      <\/td>\n<td>\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        GetCategory\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td>\n        ?\n      <\/td>\n<td align=\"right\">\n        3.7836 ns\n      <\/td>\n<td align=\"right\">\n        0.0493 ns\n      <\/td>\n<td align=\"right\">\n        0.0461 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        GetCategory\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td>\n        ?\n      <\/td>\n<td align=\"right\">\n        2.7531 ns\n      <\/td>\n<td align=\"right\">\n        0.0757 ns\n      <\/td>\n<td align=\"right\">\n        0.0633 ns\n      <\/td>\n<td align=\"right\">\n        0.73\n      <\/td>\n<td align=\"right\">\n        0.02\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 Those PRs also highlight another case of benefiting from a language improvement. As of C# 7.3, the C# compiler is able to optimize properties of the form:<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">static ReadOnlySpan&lt;byte&gt; s_byteData =&gt; new byte[] { \u2026 \/* constant bytes *\/ }<\/pre>\n<p>  <span style=\"font-size: 1rem;\">Rather than emitting this exactly as written, which would allocate a new byte array on each call, the compiler takes advantage of the facts that a) the bytes backing the array are all constant and b) it&#8217;s being returned as a read-only span, which means the consumer is unable to mutate the data using safe code. As such, with PR\u00a0<\/span><a style=\"background-color: #f7f7f9; font-size: 1rem;\" href=\"https:\/\/github.com\/dotnet\/roslyn\/pull\/24621\">dotnet\/roslyn#24621<\/a><span style=\"font-size: 1rem;\">, the C# compiler instead emits this by writing the bytes as a binary blob in metadata, and the property then simply creates a span that points directly to that data, making it very fast to access the data, more so even than if this property returned a static byte[].<\/span>\n<\/div>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"wrap:false lang:default decode:true\">\/\/ Run with: dotnet run -c Release -f netcoreapp2.1 --filter *Program* --runtimes netcoreapp3.0\n\nprivate static byte[] ArrayProp { get; } = new byte[] { 1, 2, 3 };\n\n[Benchmark(Baseline = true)]\npublic ReadOnlySpan&lt;byte&gt; GetArrayProp() =&gt; ArrayProp;\n\nprivate static ReadOnlySpan&lt;byte&gt; SpanProp =&gt; new byte[] { 1, 2, 3 };\n\n[Benchmark]\npublic ReadOnlySpan&lt;byte&gt; GetSpanProp() =&gt; SpanProp;<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Median\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        GetArrayProp\n      <\/td>\n<td align=\"right\">\n        1.3362 ns\n      <\/td>\n<td align=\"right\">\n        0.0498 ns\n      <\/td>\n<td align=\"right\">\n        0.0416 ns\n      <\/td>\n<td align=\"right\">\n        1.3366 ns\n      <\/td>\n<td align=\"right\">\n        1.000\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        GetSpanProp\n      <\/td>\n<td align=\"right\">\n        0.0125 ns\n      <\/td>\n<td align=\"right\">\n        0.0132 ns\n      <\/td>\n<td align=\"right\">\n        0.0110 ns\n      <\/td>\n<td align=\"right\">\n        0.0080 ns\n      <\/td>\n<td align=\"right\">\n        0.009\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 Another string-related area that&#8217;s gotten some attention is\u00a0<\/p>\n<p><code>StringBuilder<\/code>\u00a0(not necessarily improvements to\u00a0<code>StringBuilder<\/code>\u00a0itself, although it has received some of those, for example a new overload in PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/20773\">dotnet\/coreclr#20773<\/a>\u00a0from @Wraith2 that helps avoid accidentally boxing and creating a string from a\u00a0<code>ReadOnlyMemory&lt;char&gt;<\/code>\u00a0appended to the builder). Rather, in many situations\u00a0<code>StringBuilder<\/code>s have been used for convenience but added cost, and with just a little work (and in some cases the new\u00a0<code>String.Create<\/code>\u00a0method introduced in .NET Core 2.1), we can eliminate that overhead, in both CPU usage and allocation. Here a few examples\u2026 * PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/33598\">dotnet\/corefx#33598<\/a>\u00a0removed a\u00a0<code>StringBuilder<\/code>\u00a0used in marshaling from\u00a0<code>Dns.GetHostEntry<\/code>:<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">[Benchmark]\npublic IPHostEntry GetHostEntry() =&gt; Dns.GetHostEntry(\"34.206.253.53\");<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Median\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        RatioSD\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<th align=\"right\">\n        Allocated\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        GetHostEntry\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        532.7 us\n      <\/td>\n<td align=\"right\">\n        16.59 us\n      <\/td>\n<td align=\"right\">\n        46.79 us\n      <\/td>\n<td align=\"right\">\n        526.8 us\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<td align=\"right\">\n        1.9531\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        4888 B\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        GetHostEntry\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        527.7 us\n      <\/td>\n<td align=\"right\">\n        12.85 us\n      <\/td>\n<td align=\"right\">\n        37.06 us\n      <\/td>\n<td align=\"right\">\n        542.8 us\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.11\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        616 B\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0<\/p>\n<ul>\n<li>PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/21122\">dotnet\/coreclr#21122<\/a>\u00a0removed a\u00a0<code>StringBuilder<\/code>\u00a0used in Hebrew number formatting:<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">private static CultureInfo CreateCulture()\n{\n    var c = new CultureInfo(\"he-IL\");\n    c.DateTimeFormat.Calendar = new HebrewCalendar();\n    return c;\n}\n\nprivate CultureInfo _hebrewIsrael = CreateCulture();\n\n[Benchmark]\npublic string FormatHebrew() =&gt; new DateTime(2018, 11, 20).ToString(_hebrewIsrael);<\/pre>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        RatioSD\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<th align=\"right\">\n        Allocated\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        FormatHebrew\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        626.0 ns\n      <\/td>\n<td align=\"right\">\n        7.917 ns\n      <\/td>\n<td align=\"right\">\n        7.405 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<td align=\"right\">\n        0.2890\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        608 B\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        FormatHebrew\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        570.6 ns\n      <\/td>\n<td align=\"right\">\n        10.504 ns\n      <\/td>\n<td align=\"right\">\n        9.825 ns\n      <\/td>\n<td align=\"right\">\n        0.91\n      <\/td>\n<td align=\"right\">\n        0.02\n      <\/td>\n<td align=\"right\">\n        0.1554\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        328 B\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0<\/p>\n<ul>\n<li>PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/33592\">dotnet\/corefx#33592<\/a>\u00a0removed a\u00a0<code>StringBuilder<\/code>\u00a0used in\u00a0<code>PhysicalAddress<\/code>\u00a0formatting:<\/li>\n<\/ul>\n<pre class=\"wrap:false lang:default decode:true\">private readonly PhysicalAddress _short = new PhysicalAddress(new byte[1] { 42 });\nprivate readonly PhysicalAddress _long = new PhysicalAddress(Enumerable.Range(0, 256).Select(i =&gt; (byte)i).ToArray());\n\n[Benchmark]\npublic void PAShort() =&gt; _short.ToString();\n\n[Benchmark]\npublic void PALong() =&gt; _long.ToString();<\/pre>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        RatioSD\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<th align=\"right\">\n        Allocated\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        PAShort\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        33.68 ns\n      <\/td>\n<td align=\"right\">\n        1.0378 ns\n      <\/td>\n<td align=\"right\">\n        2.9271 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<td align=\"right\">\n        0.0648\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        136 B\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        PAShort\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        17.12 ns\n      <\/td>\n<td align=\"right\">\n        0.4240 ns\n      <\/td>\n<td align=\"right\">\n        0.7313 ns\n      <\/td>\n<td align=\"right\">\n        0.55\n      <\/td>\n<td align=\"right\">\n        0.04\n      <\/td>\n<td align=\"right\">\n        0.0153\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        32 B\n      <\/td>\n<\/tr>\n<tr>\n<td>\n      <\/td>\n<td>\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        PALong\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        2,761.80 ns\n      <\/td>\n<td align=\"right\">\n        50.1515 ns\n      <\/td>\n<td align=\"right\">\n        46.9117 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<td align=\"right\">\n        1.1940\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        2512 B\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        PALong\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        787.78 ns\n      <\/td>\n<td align=\"right\">\n        27.4673 ns\n      <\/td>\n<td align=\"right\">\n        80.1234 ns\n      <\/td>\n<td align=\"right\">\n        0.31\n      <\/td>\n<td align=\"right\">\n        0.01\n      <\/td>\n<td align=\"right\">\n        0.5007\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        1048 B\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0<\/p>\n<ul>\n<li>PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/29605\">dotnet\/corefx#29605<\/a>\u00a0removed\u00a0<code>StringBuilder<\/code>s from various properties of\u00a0<code>X509Certificate<\/code>:<\/li>\n<\/ul>\n<pre class=\"wrap:false lang:default decode:true\">private X509Certificate2 _cert = GetCert();\n\nprivate static X509Certificate2 GetCert()\n{\n    using (var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))\n    {\n        client.Connect(\"microsoft.com\", 443);\n        using (var ssl = new SslStream(new NetworkStream(client)))\n        {\n            ssl.AuthenticateAsClient(\"microsoft.com\", null, SslProtocols.None, false);\n            return new X509Certificate2(ssl.RemoteCertificate);\n        }\n    }\n}\n\n[Benchmark]\npublic string CertProp() =&gt; _cert.Thumbprint;<\/pre>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Median\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        RatioSD\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<th align=\"right\">\n        Allocated\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        CertProp\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        209.30 ns\n      <\/td>\n<td align=\"right\">\n        4.464 ns\n      <\/td>\n<td align=\"right\">\n        10.435 ns\n      <\/td>\n<td align=\"right\">\n        204.35 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<td align=\"right\">\n        0.1256\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        264 B\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        CertProp\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        95.82 ns\n      <\/td>\n<td align=\"right\">\n        1.822 ns\n      <\/td>\n<td align=\"right\">\n        1.704 ns\n      <\/td>\n<td align=\"right\">\n        96.43 ns\n      <\/td>\n<td align=\"right\">\n        0.45\n      <\/td>\n<td align=\"right\">\n        0.02\n      <\/td>\n<td align=\"right\">\n        0.0497\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        104 B\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 and so on. These PRs demonstrate that good gains can be had simply by making small tweaks that make existing code paths cheaper, and that expands well beyond\u00a0<\/p>\n<p><code>StringBuilder<\/code>. There are lots of places within .NET Core, for example, where\u00a0<code>String.Substring<\/code>\u00a0is used, and many of those cases can be replaced with use of\u00a0<code>AsSpan<\/code>\u00a0and\u00a0<code>Slice<\/code>, for example as was done in PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/29402\">dotnet\/corefx#29402<\/a>\u00a0by @juliushardt, or PRs\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/17916\">dotnet\/coreclr#17916<\/a>\u00a0and\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/29539\">dotnet\/corefx#29539<\/a>, or as was done in PRs\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/29227\">dotnet\/corefx#29227<\/a>\u00a0and\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/29721\">dotnet\/corefx#29721<\/a>\u00a0to remove string allocations from FileSystemWatcher, delaying the creation of such strings until only when it was known they were absolutely necessary.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"wrap:false lang:default decode:true\">[Benchmark]\npublic void HtmlDecode() =&gt; WebUtility.HtmlDecode(\"&#x6C34;&#x6C34;&#x6C34;&#x6C34;&#x6C34;&#x6C34;&#x6C34;\");<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<th align=\"right\">\n        Allocated\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        HtmlDecode\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        638.2 ns\n      <\/td>\n<td align=\"right\">\n        8.474 ns\n      <\/td>\n<td align=\"right\">\n        7.077 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.1516\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        320 B\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        HtmlDecode\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        153.7 ns\n      <\/td>\n<td align=\"right\">\n        2.776 ns\n      <\/td>\n<td align=\"right\">\n        2.461 ns\n      <\/td>\n<td align=\"right\">\n        0.24\n      <\/td>\n<td align=\"right\">\n        0.0191\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        40 B\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 Another example of using new APIs to improve existing functionality is with\u00a0<\/p>\n<p><code>String.Concat<\/code>. .NET Core 3.0 has several new\u00a0<code>String.Concat<\/code>\u00a0overloads, ones that accept\u00a0<code>ReadOnlySpan&lt;char&gt;<\/code>\u00a0instead of\u00a0<code>string<\/code>. These make it easy to avoid allocations\/copies of substrings in cases where concatenating pieces of other strings: instead of using\u00a0<code>String.Concat<\/code>\u00a0with\u00a0<code>String.Substring<\/code>, it&#8217;s used instead with\u00a0<code>String.AsSpan(...)<\/code>\u00a0or\u00a0<code>Slice<\/code>. In fact, the PRs\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/21766\">dotnet\/coreclr#21766<\/a>\u00a0and\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/34451\">dotnet\/corefx#34451<\/a>\u00a0that implemented, exposed, and added tests for these new overloads also added tens of call sites to the new overloads across .NET Core. Here&#8217;s an example of the impact one of those has, improving the performance of accessing\u00a0<code>Uri.DnsSafeHost<\/code>:<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">[Benchmark]\npublic string DnsSafeHost() =&gt; new Uri(\"http:\/\/[fe80::3]%1\").DnsSafeHost;<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        RatioSD\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<th align=\"right\">\n        Allocated\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        DnsSafeHost\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        733.7 ns\n      <\/td>\n<td align=\"right\">\n        14.448 ns\n      <\/td>\n<td align=\"right\">\n        17.20 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<td align=\"right\">\n        0.2012\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        424 B\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        DnsSafeHost\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        450.1 ns\n      <\/td>\n<td align=\"right\">\n        9.013 ns\n      <\/td>\n<td align=\"right\">\n        18.41 ns\n      <\/td>\n<td align=\"right\">\n        0.63\n      <\/td>\n<td align=\"right\">\n        0.02\n      <\/td>\n<td align=\"right\">\n        0.1059\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        224 B\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 Another example, using\u00a0<\/p>\n<p><code>Path.ChangeExtension<\/code>\u00a0to change from one non-null extension to another:<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">[Benchmark]\npublic string ChangeExtension() =&gt; Path.ChangeExtension(\"filename.txt\", \".dat\");<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<th align=\"right\">\n        Allocated\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        ChangeExtension\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        30.57 ns\n      <\/td>\n<td align=\"right\">\n        0.7124 ns\n      <\/td>\n<td align=\"right\">\n        0.6664 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.0495\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        104 B\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        ChangeExtension\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        24.54 ns\n      <\/td>\n<td align=\"right\">\n        0.3398 ns\n      <\/td>\n<td align=\"right\">\n        0.2838 ns\n      <\/td>\n<td align=\"right\">\n        0.80\n      <\/td>\n<td align=\"right\">\n        0.0229\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        48 B\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 Finally, a very closely related area is that of encoding. A bunch of improvements were made in .NET Core 3.0 around\u00a0<\/p>\n<p><code>Encoding<\/code>, both in general and for specific encodings, such as PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/18263\">dotnet\/coreclr#18263<\/a>\u00a0that allowed an existing corner-case optimization to be applied for\u00a0<code>Encoding.Unicode.GetString<\/code>\u00a0in many more cases, or\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/18487\">dotnet\/coreclr#18487<\/a>\u00a0that removed a bunch of unnecessary virtual indirections from various encoding implementations, or PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/20768\">dotnet\/coreclr#20768<\/a>\u00a0that improved the performance of\u00a0<code>Encoding.Preamble<\/code>\u00a0by taking advantage of the same metadata-blob span support discussed earlier, or PRs\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/21948\">dotnet\/coreclr#21948<\/a>\u00a0and\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/23098\">dotnet\/coreclr#23098<\/a>\u00a0that overhauled and streamlined the implementions of\u00a0<code>UTF8Encoding<\/code>\u00a0and\u00a0<code>AsciiEncoding<\/code>.<\/p>\n<pre class=\"wrap:false lang:default decode:true\">private byte[] _data = Encoding.ASCII.GetBytes(\"This is a test of ASCII encoding. It's faster now.\");\n\n[Benchmark]\npublic string ASCII() =&gt; Encoding.ASCII.GetString(_data);<\/pre>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<th align=\"right\">\n        Allocated\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        ASCII\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        66.92 ns\n      <\/td>\n<td align=\"right\">\n        0.8942 ns\n      <\/td>\n<td align=\"right\">\n        0.8364 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.0609\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        128 B\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        ASCII\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        28.04 ns\n      <\/td>\n<td align=\"right\">\n        0.6325 ns\n      <\/td>\n<td align=\"right\">\n        0.9467 ns\n      <\/td>\n<td align=\"right\">\n        0.42\n      <\/td>\n<td align=\"right\">\n        0.0612\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        128 B\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 These examples all served to highlight improvements made in and around strings. That&#8217;s all well and good, but where the improvements related to strings really start to shine is when looking at improvements around formatting and parsing.<\/p>\n<h3>Parsing\/Formatting<\/h3>\n<p>Parsing and formatting are the lifeblood of any modern web app or service: take data off the wire, parse it, manipulate it, format it back out. As such, in .NET Core 2.1 along with bringing up\u00a0<code>Span&lt;T&gt;<\/code>, we invested in the formatting and parsing of primitives, from\u00a0<code>Int32<\/code>\u00a0to\u00a0<code>DateTime<\/code>. Many of those changes can be read about in my previous blog posts, but one of the key factors in enabling those performance improvements was in moving a lot of native code to managed. That may be counter-intuitive, in that it&#8217;s &#8220;common knowledge&#8221; that C code is faster than C# code. However, in addition to the gap between them narrowing, having (mostly) safe C# code has made the code base easier to experiment in, so whereas we may have been skittish about tweaking the native implementations, the community-at-large has dived head first into optimizing these implementations wherever possible. That effort continues in full force in .NET Core 3.0, with some very nice rewards reaped. Let&#8217;s start with core integer primitives. PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/18897\">dotnet\/coreclr#18897<\/a>\u00a0added a variety of special paths for the parsing of\u00a0<code>Integer<\/code>-style signed values (e.g.\u00a0<code>Int32<\/code>\u00a0and\u00a0<code>Int64<\/code>), PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/18930\">dotnet\/coreclr#18930<\/a> added similar support for unsigned (e.g.\u00a0<code>UInt32<\/code>\u00a0and\u00a0<code>UInt64<\/code>), and PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/18952\">dotnet\/coreclr#18952<\/a>\u00a0did a similar pass for hex. On top of those, PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/21365\">dotnet\/coreclr#21365<\/a>\u00a0layered in additional optimizations, for example utilizing those changes for primitives like\u00a0<code>byte<\/code>, skipping unnecessary layers of functions, streamlining some calls to improve inlining, and further reducing branching. The net impact here are some significant improvements to the performance of parsing integer primitive types in this release.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">[Benchmark]\npublic int ParseInt32Dec() =&gt; int.Parse(\"12345678\");\n\n[Benchmark]\npublic int ParseInt32Hex() =&gt; int.Parse(\"BC614E\", NumberStyles.HexNumber);<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        ParseInt32Dec\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        77.30 ns\n      <\/td>\n<td align=\"right\">\n        0.8710 ns\n      <\/td>\n<td align=\"right\">\n        0.8147 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        ParseInt32Dec\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        16.08 ns\n      <\/td>\n<td align=\"right\">\n        0.2168 ns\n      <\/td>\n<td align=\"right\">\n        0.2028 ns\n      <\/td>\n<td align=\"right\">\n        0.21\n      <\/td>\n<\/tr>\n<tr>\n<td>\n      <\/td>\n<td>\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        ParseInt32Hex\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        69.01 ns\n      <\/td>\n<td align=\"right\">\n        1.0024 ns\n      <\/td>\n<td align=\"right\">\n        0.9377 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        ParseInt32Hex\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        17.39 ns\n      <\/td>\n<td align=\"right\">\n        0.1123 ns\n      <\/td>\n<td align=\"right\">\n        0.0995 ns\n      <\/td>\n<td align=\"right\">\n        0.25\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 Formatting of such types was also improved, even though it had already been improved significantly between .NET Core 2.0 and .NET Core 2.1. PR\u00a0<\/p>\n<p><a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/19551\">dotnet\/coreclr#19551<\/a>\u00a0tweaked the structure of the code to avoid needing to access the current culture number formatting data if it wouldn&#8217;t be needed (e.g. when formatting a value as hex, there&#8217;s no customization based on current culture), and PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/18935\">dotnet\/coreclr#18935<\/a>\u00a0improved decimal formatting performance, in large part by optimizing how data is passed around (or not passed at all).<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">[Benchmark]\npublic string DecimalToString() =&gt; 12345.6789m.ToString();<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<th align=\"right\">\n        Allocated\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        DecimalToString\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        88.79 ns\n      <\/td>\n<td align=\"right\">\n        1.4034 ns\n      <\/td>\n<td align=\"right\">\n        1.3127 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.0228\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        48 B\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        DecimalToString\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        76.62 ns\n      <\/td>\n<td align=\"right\">\n        0.5957 ns\n      <\/td>\n<td align=\"right\">\n        0.5572 ns\n      <\/td>\n<td align=\"right\">\n        0.86\n      <\/td>\n<td align=\"right\">\n        0.0228\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        48 B\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 In fact,\u00a0<\/p>\n<p><code>System.Decimal<\/code>\u00a0itself has been overhauled in .NET Core 3.0, as of PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/18948\">dotnet\/coreclr#18948<\/a> now with an entirely managed implementation, and with additional performance work in PRs like\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/20305\">dotnet\/coreclr#20305<\/a>.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">private decimal _a = 67891.2345m;\nprivate decimal _b = 12345.6789m;\n\n[Benchmark]\npublic decimal Add() =&gt; _a + _b;\n\n[Benchmark]\npublic decimal Subtract() =&gt; _a - _b;\n\n[Benchmark]\npublic decimal Multiply() =&gt; _a * _b;\n\n[Benchmark]\npublic decimal Divide() =&gt; _a \/ _b;\n\n[Benchmark]\npublic decimal Mod() =&gt; _a % _b;\n\n[Benchmark]\npublic decimal Floor() =&gt; decimal.Floor(_a);\n\n[Benchmark]\npublic decimal Round() =&gt; decimal.Round(_a);<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Median\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        RatioSD\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        Add\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        12.021 ns\n      <\/td>\n<td align=\"right\">\n        0.6813 ns\n      <\/td>\n<td align=\"right\">\n        2.0088 ns\n      <\/td>\n<td align=\"right\">\n        11.507 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        Add\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        8.300 ns\n      <\/td>\n<td align=\"right\">\n        0.0553 ns\n      <\/td>\n<td align=\"right\">\n        0.0518 ns\n      <\/td>\n<td align=\"right\">\n        8.312 ns\n      <\/td>\n<td align=\"right\">\n        0.87\n      <\/td>\n<td align=\"right\">\n        0.04\n      <\/td>\n<\/tr>\n<tr>\n<td>\n      <\/td>\n<td>\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        Subtract\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        13.026 ns\n      <\/td>\n<td align=\"right\">\n        0.2599 ns\n      <\/td>\n<td align=\"right\">\n        0.2431 ns\n      <\/td>\n<td align=\"right\">\n        13.046 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        Subtract\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        8.613 ns\n      <\/td>\n<td align=\"right\">\n        0.2024 ns\n      <\/td>\n<td align=\"right\">\n        0.2770 ns\n      <\/td>\n<td align=\"right\">\n        8.488 ns\n      <\/td>\n<td align=\"right\">\n        0.66\n      <\/td>\n<td align=\"right\">\n        0.03\n      <\/td>\n<\/tr>\n<tr>\n<td>\n      <\/td>\n<td>\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        Multiply\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        19.215 ns\n      <\/td>\n<td align=\"right\">\n        0.2813 ns\n      <\/td>\n<td align=\"right\">\n        0.2631 ns\n      <\/td>\n<td align=\"right\">\n        19.229 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        Multiply\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        7.182 ns\n      <\/td>\n<td align=\"right\">\n        0.1795 ns\n      <\/td>\n<td align=\"right\">\n        0.2457 ns\n      <\/td>\n<td align=\"right\">\n        7.131 ns\n      <\/td>\n<td align=\"right\">\n        0.38\n      <\/td>\n<td align=\"right\">\n        0.01\n      <\/td>\n<\/tr>\n<tr>\n<td>\n      <\/td>\n<td>\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        Divide\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        196.827 ns\n      <\/td>\n<td align=\"right\">\n        4.3572 ns\n      <\/td>\n<td align=\"right\">\n        4.6621 ns\n      <\/td>\n<td align=\"right\">\n        194.721 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        Divide\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        75.456 ns\n      <\/td>\n<td align=\"right\">\n        1.5301 ns\n      <\/td>\n<td align=\"right\">\n        1.7007 ns\n      <\/td>\n<td align=\"right\">\n        75.089 ns\n      <\/td>\n<td align=\"right\">\n        0.38\n      <\/td>\n<td align=\"right\">\n        0.01\n      <\/td>\n<\/tr>\n<tr>\n<td>\n      <\/td>\n<td>\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        Mod\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        464.968 ns\n      <\/td>\n<td align=\"right\">\n        7.0295 ns\n      <\/td>\n<td align=\"right\">\n        6.5754 ns\n      <\/td>\n<td align=\"right\">\n        466.825 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        Mod\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        13.756 ns\n      <\/td>\n<td align=\"right\">\n        0.2476 ns\n      <\/td>\n<td align=\"right\">\n        0.2316 ns\n      <\/td>\n<td align=\"right\">\n        13.729 ns\n      <\/td>\n<td align=\"right\">\n        0.03\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n      <\/td>\n<td>\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        Floor\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        33.593 ns\n      <\/td>\n<td align=\"right\">\n        0.8348 ns\n      <\/td>\n<td align=\"right\">\n        2.2710 ns\n      <\/td>\n<td align=\"right\">\n        32.734 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        Floor\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        12.109 ns\n      <\/td>\n<td align=\"right\">\n        0.1325 ns\n      <\/td>\n<td align=\"right\">\n        0.1239 ns\n      <\/td>\n<td align=\"right\">\n        12.085 ns\n      <\/td>\n<td align=\"right\">\n        0.33\n      <\/td>\n<td align=\"right\">\n        0.02\n      <\/td>\n<\/tr>\n<tr>\n<td>\n      <\/td>\n<td>\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        Round\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        32.181 ns\n      <\/td>\n<td align=\"right\">\n        0.5660 ns\n      <\/td>\n<td align=\"right\">\n        0.5294 ns\n      <\/td>\n<td align=\"right\">\n        32.018 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        Round\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        12.798 ns\n      <\/td>\n<td align=\"right\">\n        0.1572 ns\n      <\/td>\n<td align=\"right\">\n        0.1394 ns\n      <\/td>\n<td align=\"right\">\n        12.808 ns\n      <\/td>\n<td align=\"right\">\n        0.40\n      <\/td>\n<td align=\"right\">\n        0.01\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 Back to formatting and parsing, there are even some new formatting special-cases that might look silly at first, but that represent optimizations targeting real-world cases. In some sizeable web applications, we found that a large number of strings on the managed heap were simple integral values like &#8220;0&#8221; and &#8220;1&#8221;. And since the fastest code is code you don&#8217;t need to execute at all, why bother allocating and formatting these small numbers over and over when we can instead just cache and reuse the results (effectively our own string interning pool)? That&#8217;s what PR\u00a0<\/p>\n<p><a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/18383\">dotnet\/coreclr#18383<\/a>\u00a0does, creating a small, specialized cache of the strings for &#8220;0&#8221; through &#8220;9&#8221;, and any time we now find ourselves formatting a single-digit integer primitive, we instead just grab the relevant string from this cache.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">private int _digit = 4;\n\n[Benchmark]\npublic string SingleDigitToString() =&gt; _digit.ToString();<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<th align=\"right\">\n        Allocated\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        SingleDigitToString\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        17.72 ns\n      <\/td>\n<td align=\"right\">\n        0.3273 ns\n      <\/td>\n<td align=\"right\">\n        0.3061 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.0152\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        32 B\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        SingleDigitToString\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        11.57 ns\n      <\/td>\n<td align=\"right\">\n        0.1750 ns\n      <\/td>\n<td align=\"right\">\n        0.1551 ns\n      <\/td>\n<td align=\"right\">\n        0.65\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 Enums have also seen sizable parsing and formatting improvements in .NET Core 3.0. PR\u00a0<\/p>\n<p><a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/21214\">dotnet\/coreclr#21214<\/a>\u00a0improved the handling of\u00a0<code>Enum.Parse<\/code>\u00a0and\u00a0<code>Enum.TryParse<\/code>, for both the generic and non-generic variants. PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/21254\">dotnet\/coreclr#21254<\/a>\u00a0improved the performance of\u00a0<code>ToString<\/code>\u00a0when dealing with\u00a0<code>[Flags]<\/code>\u00a0enums, and PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/21284\">dotnet\/coreclr#21284<\/a>\u00a0further improved other ToString cases. The net effect of these changes is a sizeable improvement in\u00a0<code>Enum<\/code>-related performance:<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">[Benchmark]\npublic DayOfWeek EnumParse() =&gt; Enum.Parse&lt;DayOfWeek&gt;(\"Thursday\");\n\n[Benchmark]\npublic string EnumToString() =&gt; NumberStyles.Integer.ToString();<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<th align=\"right\">\n        Allocated\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        EnumParse\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        154.42 ns\n      <\/td>\n<td align=\"right\">\n        1.6917 ns\n      <\/td>\n<td align=\"right\">\n        1.5824 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.0114\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        24 B\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        EnumParse\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        62.92 ns\n      <\/td>\n<td align=\"right\">\n        1.2239 ns\n      <\/td>\n<td align=\"right\">\n        1.1448 ns\n      <\/td>\n<td align=\"right\">\n        0.41\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<\/tr>\n<tr>\n<td>\n      <\/td>\n<td>\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        EnumToString\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        85.81 ns\n      <\/td>\n<td align=\"right\">\n        1.6458 ns\n      <\/td>\n<td align=\"right\">\n        1.3743 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.0305\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        64 B\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        EnumToString\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        27.89 ns\n      <\/td>\n<td align=\"right\">\n        0.6076 ns\n      <\/td>\n<td align=\"right\">\n        0.7901 ns\n      <\/td>\n<td align=\"right\">\n        0.32\n      <\/td>\n<td align=\"right\">\n        0.0114\n      <\/td>\n<td align=\"right\">\n        0.0001\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        24 B\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 In .NET Core 2.1,\u00a0<\/p>\n<p><code>DateTime.TryFormat<\/code>\u00a0and\u00a0<code>ToString<\/code>\u00a0were optimized for the commonly-used &#8220;o&#8221; and &#8220;r&#8221; formats; in .NET Core 3.0, the parsing equivalents get a similar treatment. PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/18800\">dotnet\/coreclr#18800<\/a>\u00a0significantly improves the performance of parsing\u00a0<code>DateTime{Offset}<\/code>s formatted with the Roundtrip &#8220;o&#8221; format, and PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/18771\">dotnet\/coreclr#18771<\/a>\u00a0does the same for the RFC1123 &#8220;r&#8221; format. For any serialization formats heavy in\u00a0<code>DateTime<\/code>s, these improvements can make a substantial impact:<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">private string _r = DateTime.Now.ToString(\"r\");\nprivate string _o = DateTime.Now.ToString(\"o\");\n\n[Benchmark]\npublic DateTime ParseR() =&gt; DateTime.ParseExact(_r, \"r\", null);\n\n[Benchmark]\npublic DateTime ParseO() =&gt; DateTime.ParseExact(_o, \"o\", null);<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Median\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<th align=\"right\">\n        Allocated\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        ParseR\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        2,254.6 ns\n      <\/td>\n<td align=\"right\">\n        44.340 ns\n      <\/td>\n<td align=\"right\">\n        45.534 ns\n      <\/td>\n<td align=\"right\">\n        2,263.2 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.0420\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        96 B\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        ParseR\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        113.7 ns\n      <\/td>\n<td align=\"right\">\n        3.440 ns\n      <\/td>\n<td align=\"right\">\n        9.926 ns\n      <\/td>\n<td align=\"right\">\n        112.6 ns\n      <\/td>\n<td align=\"right\">\n        0.06\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<\/tr>\n<tr>\n<td>\n      <\/td>\n<td>\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<td align=\"right\">\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        ParseO\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        1,337.1 ns\n      <\/td>\n<td align=\"right\">\n        26.542 ns\n      <\/td>\n<td align=\"right\">\n        68.987 ns\n      <\/td>\n<td align=\"right\">\n        1,363.8 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.0744\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        160 B\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        ParseO\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        354.9 ns\n      <\/td>\n<td align=\"right\">\n        4.801 ns\n      <\/td>\n<td align=\"right\">\n        3.748 ns\n      <\/td>\n<td align=\"right\">\n        354.9 ns\n      <\/td>\n<td align=\"right\">\n        0.30\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 Tying back to the\u00a0<\/p>\n<p><code>StringBuilder<\/code>\u00a0discussion from earlier, default\u00a0<code>DateTime<\/code>\u00a0formatting was also improved by PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/22111\">dotnet\/coreclr#22111<\/a>, tweaking how\u00a0<code>DateTime<\/code>\u00a0internally interacts with a\u00a0<code>StringBuilder<\/code>\u00a0that&#8217;s used to build up the resulting state.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">private DateTime _now = DateTime.Now;\n\n[Benchmark]\npublic string DateTimeToString() =&gt; _now.ToString();<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        RatioSD\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<th align=\"right\">\n        Allocated\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        DateTimeToString\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        337.8 ns\n      <\/td>\n<td align=\"right\">\n        6.560 ns\n      <\/td>\n<td align=\"right\">\n        5.815 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<td align=\"right\">\n        0.0834\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        176 B\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        DateTimeToString\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        269.4 ns\n      <\/td>\n<td align=\"right\">\n        5.274 ns\n      <\/td>\n<td align=\"right\">\n        5.416 ns\n      <\/td>\n<td align=\"right\">\n        0.80\n      <\/td>\n<td align=\"right\">\n        0.02\n      <\/td>\n<td align=\"right\">\n        0.0300\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        64 B\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0<\/p>\n<p><code>TimeSpan<\/code>\u00a0formatting was also significantly improved, via PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/18990\">dotnet\/coreclr#18990<\/a>:<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">private TimeSpan _ts = new TimeSpan(3, 10, 2, 34, 567);\n\n[Benchmark]\npublic string TimeSpanToString() =&gt; _ts.ToString();<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<th align=\"right\">\n        Allocated\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        TimeSpanToString\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        151.11 ns\n      <\/td>\n<td align=\"right\">\n        2.0037 ns\n      <\/td>\n<td align=\"right\">\n        1.874 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.0303\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        64 B\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        TimeSpanToString\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        34.73 ns\n      <\/td>\n<td align=\"right\">\n        0.7680 ns\n      <\/td>\n<td align=\"right\">\n        1.304 ns\n      <\/td>\n<td align=\"right\">\n        0.23\n      <\/td>\n<td align=\"right\">\n        0.0305\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        64 B\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0<\/p>\n<p><code>Guid<\/code>\u00a0parsing also got in the perf-optimization game, with PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/20183\">dotnet\/coreclr#20183<\/a>\u00a0improved parsing performance of\u00a0<code>Guid<\/code>, primarily by avoiding overhead in helper routines, as well as by avoiding some searches used to determine which parsing routines to employ.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">private string _guid = Guid.NewGuid().ToString(\"D\");\n\n[Benchmark]\npublic Guid ParseGuid() =&gt; Guid.ParseExact(_guid, \"D\");<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Median\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        ParseGuid\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        287.5 ns\n      <\/td>\n<td align=\"right\">\n        11.606 ns\n      <\/td>\n<td align=\"right\">\n        28.688 ns\n      <\/td>\n<td align=\"right\">\n        277.2 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        ParseGuid\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        111.7 ns\n      <\/td>\n<td align=\"right\">\n        2.199 ns\n      <\/td>\n<td align=\"right\">\n        2.057 ns\n      <\/td>\n<td align=\"right\">\n        112.4 ns\n      <\/td>\n<td align=\"right\">\n        0.33\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 Related, PR\u00a0<\/p>\n<p><a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/21336\">dotnet\/coreclr#21336<\/a>\u00a0again takes advantage of vectorization to improve\u00a0<code>Guid<\/code>&#8216;s construction and formatting to and from byte arrays and spans:<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">private Guid _guid = Guid.NewGuid();\nprivate byte[] _buffer = new byte[16];\n\n[Benchmark]\npublic void GuidToFromBytes()\n{\n    _guid.TryWriteBytes(_buffer);\n    _guid = new Guid(_buffer);\n}<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        GuidToFromBytes\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        16.623 ns\n      <\/td>\n<td align=\"right\">\n        0.2917 ns\n      <\/td>\n<td align=\"right\">\n        0.2586 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        GuidToFromBytes\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        5.701 ns\n      <\/td>\n<td align=\"right\">\n        0.1047 ns\n      <\/td>\n<td align=\"right\">\n        0.0980 ns\n      <\/td>\n<td align=\"right\">\n        0.34\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0<\/p>\n<h3>Regular Expressions<\/h3>\n<p>Often related to parsing is the area of regular expressions. A bit of work was done on\u00a0<code>System.Text.RegularExpressions<\/code>\u00a0in .NET Core 3.0. PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/30474\">dotnet\/corefx#30474<\/a>\u00a0replaced some usage of an internal\u00a0<code>StringBuilder<\/code>\u00a0cache with a\u00a0<code>ref struct<\/code>-based builder that takes advantage of stack-allocated space and pooled buffers. And PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/30632\">dotnet\/corefx#30632<\/a>\u00a0continued the effort by taking further advantage of spans. But the biggest improvement came in PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/32899\">dotnet\/corefx#32899<\/a>\u00a0from @Alois-xx, which tweaks the code generated for a\u00a0<code>RegexOptions.Compiled<\/code>\u00a0<code>Regex<\/code>\u00a0to avoid gratuitous thread-local accesses to look up the current culture. This is particularly impactful when also using\u00a0<code>RegexOptions.IgnoreCase<\/code>. To see the impact, I found a complicated\u00a0<code>Regex<\/code>\u00a0that used both\u00a0<code>Compiled<\/code>\u00a0and\u00a0<code>IgnoreCase<\/code>, and put it into a benchmark:<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"wrap:false lang:default decode:true\">\/\/ Pattern and options copied from https:\/\/github.com\/microsoft\/referencesource\/blob\/aaca53b025f41ab638466b1efe569df314f689ea\/System.ComponentModel.DataAnnotations\/DataAnnotations\/EmailAddressAttribute.cs#L54-L55\nprivate Regex _regex = new Regex(\n    @\"^((([a-z]|\\d|[!#\\$%&'\\*\\+\\-\\\/=\\?\\^_`{\\|}~]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])+(\\.([a-z]|\\d|[!#\\$%&'\\*\\+\\-\\\/=\\?\\^_`{\\|}~]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])+)*)|((\\x22)((((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(([\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]|\\x21|[\\x23-\\x5b]|[\\x5d-\\x7e]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(\\\\([\\x01-\\x09\\x0b\\x0c\\x0d-\\x7f]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF]))))*(((\\x20|\\x09)*(\\x0d\\x0a))?(\\x20|\\x09)+)?(\\x22)))@((([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])*([a-z]|\\d|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])))\\.)+(([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])|(([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])([a-z]|\\d|-|\\.|_|~|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])*([a-z]|[\\u00A0-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF])))\\.?$\",\n    RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);\n\n[Benchmark]\npublic bool RegexCompiled() =&gt; _regex.IsMatch(\"someAddress@someCompany.com\");<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        RatioSD\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        RegexCompiled\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        1.946 us\n      <\/td>\n<td align=\"right\">\n        0.0406 us\n      <\/td>\n<td align=\"right\">\n        0.0883 us\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        RegexCompiled\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        1.209 us\n      <\/td>\n<td align=\"right\">\n        0.0432 us\n      <\/td>\n<td align=\"right\">\n        0.1254 us\n      <\/td>\n<td align=\"right\">\n        0.64\n      <\/td>\n<td align=\"right\">\n        0.08\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0<\/p>\n<h3>Threading<\/h3>\n<p>Threading is one of those things that&#8217;s ever-present and yet most apps and libraries don&#8217;t need to explicitly interact with most of the time. That makes it an area ripe for runtime performance improvements to drive down overhead as much as possible, so that user code just gets faster. Previous releases of .NET Core saw a lot of investment in this area, and .NET Core 3.0 continues the trend. This is another area where new APIs have been exposed and then also used in .NET Core itself for further gain. For example, historically the only work item types that could be queued to the\u00a0<code>ThreadPool<\/code>\u00a0were ones implemented in the runtime, namely those created by\u00a0<code>ThreadPool.QueueUserWorkItem<\/code>\u00a0and friends, by\u00a0<code>Task<\/code>, by\u00a0<code>Timer<\/code>, and other such core types. But in .NET Core 3.0, the\u00a0<code>ThreadPool<\/code>\u00a0has an\u00a0<code>UnsafeQueueUserWorkItem<\/code>\u00a0overload that accepts the newly public\u00a0<code>IThreadPoolWorkItem<\/code>\u00a0interface. This interface is very simple, with a single method that just\u00a0<code>Execute<\/code>s work, and that means that any object that implements this interface can be queued directly to the thread pool. This is advanced; most code is just fine using the existing work item types. But this additional option affords a lot of flexibility, in particular in being able to implement the interface on a reusable object that can be queued over and over again to the pool. This is now used in a bunch of additional places in .NET Core 3.0. One such place is in\u00a0<code>System.Threading.Channels<\/code>. The\u00a0<code>Channels<\/code>\u00a0library introduced in .NET Core 2.1 already had a fairly low allocation profile, but there were still times it would allocate. For example, one of the options when creating a channel is whether continuations created by the library should run synchronously or asynchronously as part of a task completing (e.g. when a\u00a0<code>TryWrite<\/code>\u00a0call on a channel wakes up a corresponding\u00a0<code>ReadAsync<\/code>, whether the continuation from that\u00a0<code>ReadAsync<\/code>\u00a0invoked synchronously or queued by the\u00a0<code>TryWrite<\/code>\u00a0call). The default is that continuations are never invoked synchronously, but that also then requires allocating an object as part of queueing the continuation to the thread pool. With PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/33080\">dotnet\/corefx#33080<\/a>, the reusable\u00a0<code>IValueTaskSource<\/code>\u00a0implementation that already backs the\u00a0<code>ValueTask<\/code>s returned from\u00a0<code>ReadAsync<\/code>\u00a0calls also implements\u00a0<code>IThreadPoolWorkItem<\/code>\u00a0and can thus itself be queued, avoiding that allocation. This can have a measurable impact on throughput.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"wrap:false lang:default decode:true\">\/\/ Run with: dotnet run -c Release -f netcoreapp2.1 --filter *Program*\n\nprivate sealed class Config : ManualConfig \/\/ also add [Config(typeof(Config))] to the Program class\n{\n    public Config()\n    {\n        Add(Job.MediumRun.With(CsProjCoreToolchain.NetCoreApp21).WithNuGet(\"System.Threading.Channels\", \"4.5.0\").WithId(\"4.5.0\"));\n        Add(Job.MediumRun.With(CsProjCoreToolchain.NetCoreApp30).WithNuGet(\"System.Threading.Channels\", \"4.6.0-preview5.19224.8\").WithId(\"4.6.0-preview5.19224.8\"));\n    }\n}\n\nprivate Channel&lt;int&gt; _channel1 = Channel.CreateUnbounded&lt;int&gt;();\nprivate Channel&lt;int&gt; _channel2 = Channel.CreateUnbounded&lt;int&gt;();\n\n[GlobalSetup]\npublic void Setup()\n{\n    Task.Run(async () =&gt;\n    {\n        var reader = _channel1.Reader;\n        var writer = _channel2.Writer;\n        while (true)\n        {\n            writer.TryWrite(await reader.ReadAsync());\n        }\n    });\n}\n\n[Benchmark]\npublic async Task PingPong()\n{\n    var writer = _channel1.Writer;\n    var reader = _channel2.Reader;\n    for (int i = 0; i &lt; 10_000; i++)\n    {\n        writer.TryWrite(i);\n        await reader.ReadAsync();\n    }\n}<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Job\n      <\/th>\n<th>\n        NuGetReferences\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        PingPong\n      <\/td>\n<td>\n        4.5.0\n      <\/td>\n<td>\n        System.Threading.Channels 4.5.0\n      <\/td>\n<td>\n        .NET Core 2.1\n      <\/td>\n<td align=\"right\">\n        22.44 ms\n      <\/td>\n<td align=\"right\">\n        0.3246 ms\n      <\/td>\n<td align=\"right\">\n        0.4757 ms\n      <\/td>\n<td align=\"right\">\n        593.7500\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        PingPong\n      <\/td>\n<td>\n        4.6.0-preview5.19224.8\n      <\/td>\n<td>\n        System.Threading.Channels 4.6.0-preview5.19224.8\n      <\/td>\n<td>\n        .NET Core 3.0\n      <\/td>\n<td align=\"right\">\n        16.81 ms\n      <\/td>\n<td align=\"right\">\n        0.4246 ms\n      <\/td>\n<td align=\"right\">\n        0.6356 ms\n      <\/td>\n<td align=\"right\">\n        31.2500\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0<\/p>\n<p><code>IThreadPoolWorkItem<\/code>\u00a0is now also utilized in other places, like in\u00a0<code>ConcurrentExclusiveSchedulerPair<\/code>\u00a0(a little known but useful type that provides an exclusive scheduler that limits execution to only one task at a time, a concurrent scheduler that limits a user-defined number of tasks to run at a time, and that coordinate with each other so that no concurrent tasks may run while an exclusive task is running, ala a reader-writer lock), which now implements\u00a0<code>IThreadPoolWorkItem<\/code>\u00a0on an internally reusable work item object such that it also can avoid allocations when queueing its own processors. It&#8217;s also used in ASP.NET Core, and is one of the reasons key ASP.NET benchmarks are ammortized to 0 allocations per request. But by far the most impactful new implementer is in the async\/await infrastructure. In .NET Core 2.1, the runtime&#8217;s support for async\/await was overhauled, drastically reducing the overheads involved in async methods. Previously when an async method awaited for the first time an awaitable that wasn&#8217;t yet complete, the struct-based state machine for the async method would be boxed (literally a runtime box) to the heap. With .NET Core 2.1, we changed that to instead use a generic object that stores the struct as a field on it. This has a myriad of benefits, but one of these benefits is that it now enables us to implement additional interfaces on that object, such as implementing\u00a0<code>IThreadPoolWorkItem<\/code>. PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/20159\">dotnet\/coreclr#20159<\/a>\u00a0does exactly that, and it enables another large swath of scenarios to have further reduced allocations, in particular situations where\u00a0<code>TaskCreationOptions.RunContinuationsAsynchronously<\/code>\u00a0was used with a\u00a0<code>TaskCompletionSource&lt;T&gt;<\/code>. This can be seen in a benchmark like the following.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"wrap:false lang:default decode:true\">\/\/ Run with: dotnet run -c Release -f netcoreapp2.1 --filter *Program*\n\nprivate sealed class Config : ManualConfig \/\/ also add [Config(typeof(Config))] to the Program class\n{\n    public Config()\n    {\n        Add(Job.MediumRun.With(CsProjCoreToolchain.NetCoreApp21).WithNuGet(\"System.Threading.Channels\", \"4.5.0\").WithId(\"4.5.0\"));\n        Add(Job.MediumRun.With(CsProjCoreToolchain.NetCoreApp30).WithNuGet(\"System.Threading.Channels\", \"4.6.0-preview5.19224.8\").WithId(\"4.6.0-preview5.19224.8\"));\n    }\n}\n\nprivate Channel&lt;TaskCompletionSource&lt;bool&gt;&gt; _channel = Channel.CreateUnbounded&lt;TaskCompletionSource&lt;bool&gt;&gt;();\n\n[GlobalSetup]\npublic void Setup()\n{\n    Task.Run(async () =&gt;\n    {\n        var reader = _channel.Reader;\n        while (true) (await reader.ReadAsync()).TrySetResult(true);\n    });\n}\n\n[Benchmark]\npublic async Task AsyncAllocs()\n{\n    var writer = _channel.Writer;\n    for (int i = 0; i &lt; 1_000_000; i++)\n    {\n        var tcs = new TaskCompletionSource&lt;bool&gt;(TaskCreationOptions.RunContinuationsAsynchronously);\n        writer.TryWrite(tcs);\n        await tcs.Task;\n    }\n}<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Job\n      <\/th>\n<th>\n        NuGetReferences\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        AsyncAllocs\n      <\/td>\n<td>\n        4.5.0\n      <\/td>\n<td>\n        System.Threading.Channels 4.5.0\n      <\/td>\n<td>\n        .NET Core 2.1\n      <\/td>\n<td align=\"right\">\n        2.396 s\n      <\/td>\n<td align=\"right\">\n        0.0486 s\n      <\/td>\n<td align=\"right\">\n        0.0728 s\n      <\/td>\n<td align=\"right\">\n        96000.0000\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        AsyncAllocs\n      <\/td>\n<td>\n        4.6.0-preview5.19224.8\n      <\/td>\n<td>\n        System.Threading.Channels 4.6.0-preview5.19224.8\n      <\/td>\n<td>\n        .NET Core 3.0\n      <\/td>\n<td align=\"right\">\n        1.512 s\n      <\/td>\n<td align=\"right\">\n        0.0256 s\n      <\/td>\n<td align=\"right\">\n        0.0359 s\n      <\/td>\n<td align=\"right\">\n        49000.0000\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 That change allowed subsequent optimizations, such as PR\u00a0<\/p>\n<p><a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/20186\">dotnet\/coreclr#20186<\/a>\u00a0using it to make\u00a0<code>await Task.Yield();<\/code>\u00a0allocation-free:<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">[Benchmark]\npublic async Task Yield()\n{\n    for (int i = 0; i &lt; 1_000_000; i++)\n    {\n        await Task.Yield();\n    }\n}<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        RatioSD\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        Yield\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        581.3 ms\n      <\/td>\n<td align=\"right\">\n        11.615 ms\n      <\/td>\n<td align=\"right\">\n        30.39 ms\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<td align=\"right\">\n        19000.0000\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        Yield\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        464.4 ms\n      <\/td>\n<td align=\"right\">\n        9.087 ms\n      <\/td>\n<td align=\"right\">\n        10.46 ms\n      <\/td>\n<td align=\"right\">\n        0.81\n      <\/td>\n<td align=\"right\">\n        0.06\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 It&#8217;s even utilized further in\u00a0<\/p>\n<p><code>Task<\/code>\u00a0itself. There&#8217;s an interesting race condition that has to be handled in awaitables: what happens if the awaited operation completes after the call to\u00a0<code>IsCompleted<\/code>\u00a0but before the call to\u00a0<code>OnCompleted<\/code>? As a reminder, the code:<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">await something;<\/pre>\n<\/div>\n<p>compiles down to code along the lines of:<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">var $awaiter = something.GetAwaiter();\nif (!$awaiter.IsCompleted)\n{\n    _state = 42;\n    AwaitOnCompleted(ref $awaiter);\n    return;\n}\nLabel42:\n$awaiter.GetResult();<\/pre>\n<\/div>\n<p>Once we go down the path of\u00a0<\/p>\n<p><code>IsCompleted<\/code>\u00a0having returned\u00a0<code>false<\/code>, we&#8217;re going to call\u00a0<code>AwaitOnCompleted<\/code>\u00a0and return. If the operation has completed by the time we call\u00a0<code>AwaitOnCompleted<\/code>, we don&#8217;t want to synchronously invoke the continuation that re-enters this state machine, as we&#8217;ll be doing so further down the stack, and if that happened repeatedly, we&#8217;d &#8220;stack dive&#8221; and could end up overflowing the stack. Instead, we&#8217;re forced to queue the continuation. This case isn&#8217;t the common case, but it happens more often than you might expect, as it simply requires an operation that completes asynchronously very quickly (various networking operations often fall into this category). As of PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/22373\">dotnet\/coreclr#22373<\/a>, the runtime now takes advantage of the async state machine box object implementing\u00a0<code>IThreadPoolWorkItem<\/code>\u00a0to avoid the allocations in this case as well! In addition to\u00a0<code>IThreadPoolWorkItem<\/code>\u00a0being used with async\/await to allow the async implementation to queue work items to the thread pool in a more allocation-friendly manner just as any other code can, changes were also made that give the\u00a0<code>ThreadPool<\/code>\u00a01st-hand knowledge of the state machine box in order to help it optimize additional cases. PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/21159\">dotnet\/coreclr#21159<\/a>\u00a0from @benaadams teaches the\u00a0<code>ThreadPool<\/code>\u00a0to re-route some\u00a0<code>UnsafeQueueUserWorkItem(Action&lt;object&gt;, object, bool)<\/code>\u00a0calls to instead use\u00a0<code>UnsafeQueueUserWorkItem(IAsyncStateMachineBox, bool)<\/code>\u00a0under the covers, so that higher-level libraries can get these allocation benefits without having to be aware of the box machinery. Another async-related area that&#8217;s seen measurable improvements are\u00a0<code>Timer<\/code>s. In .NET Core 2.1, some important improvements were made to\u00a0<code>System.Threading.Timers<\/code>\u00a0to help improve throughput and minimize contention for a common case where timers aren&#8217;t firing, but instead are quickly created and destroyed. And while those changes help a bit with the case when timers do actually fire, they didn&#8217;t help with the majority costs and sources of contention in that case, which is that potentially a lot of work (proportional to the number of timers registered) was done while holding locks. .NET Core 3.0 makes some big improvements here. PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/20302\">dotnet\/coreclr#20302<\/a>\u00a0partitions the internal list of registered timers into two lists: one with timers that will soon fire and one with timers that won&#8217;t fire for a while. In most workloads that have a lot of registered timers, the majority of timers fall into the latter bucket at any given point in time, and this partitioning scheme enables the runtime to only consider the small bucket when firing timers most of the time. In doing so, it can significantly reduce the costs involved in firing timers, and as a result, also significantly reduce contention on the lock held while manipulating those lists. One customer who tried out these changes after having experienced issues due to tons of active timers had this to say about the impact:<\/p>\n<blockquote>\n<\/blockquote>\n<p style=\"padding-left: 30px;\">\n  > <span style=\"font-size: 1rem;\">&#8220;We got the change in production yesterday and the results are amazing, with 99% reduction in lock contention. We have also measured 4-5% CPU gains, and more importantly 0.15% improvement in reliability for our service (which is huge!).&#8221;<\/span> >\n<\/p>\n<p>The nature of the scenario makes it a little difficult to see the impact in a Benchmark.NET benchmark, so we&#8217;ll do something a little different. Rather than measuring the thing that was actually changed, we&#8217;ll measure something else that&#8217;s indirectly impacted. In particular, these changes didn&#8217;t directly impact the performance of creating and destroying timers; in fact, one of the goals was to avoid doing so (in particular to avoid harming that important path). But by reducing the costs of firing timers, we reduce how long locks are held, which then also reduces the contention that the creating\/destroying of timers faces. So, our benchmark creates a bunch of timers, ranging in when and how often they fire, and then we time how long it takes to create and destroy a bunch of additional timers.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">private Timer[] _timers;\n\n[GlobalSetup]\npublic void Setup()\n{\n    _timers = new Timer[1_000_000];\n    for (int i = 0; i &lt; _timers.Length; i++)\n    {\n        _timers[i] = new Timer(_ =&gt; { }, null, i, i);\n    }\n    Thread.Sleep(1000);\n}\n\n[Benchmark]\npublic void CreateDestroy()\n{\n    for (int i = 0; i &lt; 1_000; i++)\n    {\n        new Timer(_ =&gt; { }, 0, 100, 100).Dispose();\n    }\n}<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Median\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        RatioSD\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        CreateDestroy\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        289.1 us\n      <\/td>\n<td align=\"right\">\n        7.131 us\n      <\/td>\n<td align=\"right\">\n        20.687 us\n      <\/td>\n<td align=\"right\">\n        282.8 us\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<td align=\"right\">\n        80.0781\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        CreateDestroy\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        199.5 us\n      <\/td>\n<td align=\"right\">\n        3.983 us\n      <\/td>\n<td align=\"right\">\n        5.584 us\n      <\/td>\n<td align=\"right\">\n        199.2 us\n      <\/td>\n<td align=\"right\">\n        0.71\n      <\/td>\n<td align=\"right\">\n        0.04\n      <\/td>\n<td align=\"right\">\n        80.3223\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0<\/p>\n<p><code>Timer<\/code>\u00a0improvements have also taken other forms. For example, PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/22233\">dotnet\/coreclr#22233<\/a>\u00a0from @benaadams shrinks the allocation involved in\u00a0<code>Task.Delay<\/code>\u00a0when used without a\u00a0<code>CancellationToken<\/code>\u00a0by 24 bytes, and PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/20509\">dotnet\/coreclr#20509<\/a>\u00a0reduces the timer-related allocations involved in creating timed\u00a0<code>CancellationTokenSource<\/code>s, which also has a nice effect on throughput:<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">[Benchmark]\npublic void CTSTimer()\n{\n    using (var cts = new CancellationTokenSource())\n        cts.CancelAfter(1_000_000);\n}<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Median\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        RatioSD\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<th align=\"right\">\n        Allocated\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        CTSTimer\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        231.3 ns\n      <\/td>\n<td align=\"right\">\n        6.293 ns\n      <\/td>\n<td align=\"right\">\n        16.018 ns\n      <\/td>\n<td align=\"right\">\n        224.8 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<td align=\"right\">\n        0.0987\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        208 B\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        CTSTimer\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        115.3 ns\n      <\/td>\n<td align=\"right\">\n        1.769 ns\n      <\/td>\n<td align=\"right\">\n        1.655 ns\n      <\/td>\n<td align=\"right\">\n        115.0 ns\n      <\/td>\n<td align=\"right\">\n        0.46\n      <\/td>\n<td align=\"right\">\n        0.04\n      <\/td>\n<td align=\"right\">\n        0.0764\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        160 B\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 There are other even lower-level improvements that have gone into the release. For example, PR\u00a0<\/p>\n<p><a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/21328\">dotnet\/coreclr#21328<\/a>\u00a0from @benaadams improved\u00a0<code>Thread.CurrentThread<\/code>\u00a0by changing the implementation to store the relevant\u00a0<code>Thread<\/code>\u00a0in a\u00a0<code>[ThreadStatic]<\/code>\u00a0field rather than forcing\u00a0<code>CurrentThread<\/code>\u00a0to make an\u00a0<code>InternalCall<\/code>\u00a0into the native portions of the runtime.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">[Benchmark]\npublic Thread CurrentThread() =&gt; Thread.CurrentThread;<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        RatioSD\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        CurrentThread\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        6.101 ns\n      <\/td>\n<td align=\"right\">\n        0.2587 ns\n      <\/td>\n<td align=\"right\">\n        0.7547 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        CurrentThread\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        2.822 ns\n      <\/td>\n<td align=\"right\">\n        0.0439 ns\n      <\/td>\n<td align=\"right\">\n        0.0389 ns\n      <\/td>\n<td align=\"right\">\n        0.45\n      <\/td>\n<td align=\"right\">\n        0.04\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 As other examples, PR\u00a0<\/p>\n<p><a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/23747\">dotnet\/coreclr#23747<\/a>\u00a0taught the runtime to better respect Docker &#8211;cpu limits, PRs\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/21722\">dotnet\/coreclr#21722<\/a>\u00a0and\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/21586\">dotnet\/coreclr#21586<\/a>\u00a0improved spinning behavior when contention was encountered across a variety of synchronization sites, PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/22686\">dotnet\/coreclr#22686<\/a>\u00a0improved performance of\u00a0<code>SemaphoreSlim<\/code>\u00a0when consumers of an instance were mixing both synchronous\u00a0<code>Wait<\/code>s and asynchronous\u00a0<code>WaitAsync<\/code>s, and PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/18098\">dotnet\/coreclr#18098<\/a>\u00a0from @Quogu special-cased\u00a0<code>CancellationTokenSource<\/code>\u00a0created with a timeout of 0 to avoid\u00a0<code>Timer<\/code>-related costs. \u00a0<\/p>\n<h3>Collections<\/h3>\n<p>Moving on from threading, let&#8217;s explore some of the performance improvements that have gone into collections. Collections are so commonly used in pretty much every program that they&#8217;ve received a lot of performance-focused attention in previous .NET Core releases. Even so, there continues to be areas for improvement. Here are some example such improvements in .NET Core 3.0.<\/p>\n<p><code>ConcurrentDictionary&lt;TKey, TValue&gt;<\/code>\u00a0has an\u00a0<code>IsEmpty<\/code>\u00a0property that states whether the dictionary is empty at that moment-in-time. In previous releases, it took all of the dictionary&#8217;s locks in order to get a proper moment-in-time answer. But as it turns out, those locks only need to be held if we think the collection might be empty: if we see anything in any of the dictionary&#8217;s internals buckets, the locks aren&#8217;t needed, as we&#8217;d stop looking at additional buckets anyway the moment we found one bucket to contain anything. Thus, PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/30098\">dotnet\/corefx#30098<\/a>\u00a0from @drewnoakes added a fast path that first checks each bucket without the locks, in order to optimize for the common case where the dictionary isn&#8217;t empty (the impact on the case where the dictionary is empty is minimal).<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">private ConcurrentDictionary&lt;int, int&gt; _cd;\n\n[GlobalSetup]\npublic void Setup()\n{\n    _cd = new ConcurrentDictionary&lt;int, int&gt;();\n    _cd.TryAdd(1, 1);\n}\n\n[Benchmark] public bool IsEmpty() =&gt; _cd.IsEmpty;<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        IsEmpty\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        73.675 ns\n      <\/td>\n<td align=\"right\">\n        0.3934 ns\n      <\/td>\n<td align=\"right\">\n        0.3285 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        IsEmpty\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        3.160 ns\n      <\/td>\n<td align=\"right\">\n        0.0402 ns\n      <\/td>\n<td align=\"right\">\n        0.0356 ns\n      <\/td>\n<td align=\"right\">\n        0.04\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0<\/p>\n<p><code>ConcurrentDictionary<\/code>\u00a0wasn&#8217;t the only concurrent collection to get some attention. An improvement came to\u00a0<code>ConcurrentQueue&lt;T&gt;<\/code>\u00a0in\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/18035\">dotnet\/coreclr#18035<\/a>, and it&#8217;s an interesting example in how performance optimization often is a trade-off between scenarios. In .NET Core 2.0, we overhauled the\u00a0<code>ConcurrentQueue<\/code>\u00a0implementation in a way that significantly improved throughput while also significantly reducing memory allocations, turning the\u00a0<code>ConcurrentQueue<\/code>\u00a0into a linked list of circular arrays. However, the change involved a concession: because of the producer\/consumer nature of the arrays, if any operation needed to observe data in-place in a segment (rather than dequeueing it), the segment that was observed would be &#8220;frozen&#8221; for any further enqueues&#8230; this was to avoid problems where, for example, one thread was enumerating the contents of the segment while another thread was enqueueing and dequeueing. When there were multiple segments in the queue, accessing\u00a0<code>Count<\/code>\u00a0ended up being treated as an observation, but that meant that simply accessing the\u00a0<code>ConcurrentQueue<\/code>&#8216;s\u00a0<code>Count<\/code>\u00a0would render all of the multiple segments in the queue dead for further enqueues. The theory at the time was that such a trade-off was fine, because no one should be accessing the\u00a0<code>Count<\/code>\u00a0of the queue frequently enough for this to matter. That theory was wrong, and several customers reported significant slowdowns in their workloads because they were accessing the\u00a0<code>Count<\/code>\u00a0on every enqueue or dequeue. While the right solution is in general to avoid doing that, we wanted to fix this, and as it turns out, the fix was relatively straightforward, such that we could have our performance cake and eat it, too. The results are very obvious in the following benchmark.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">private ConcurrentQueue&lt;int&gt; _cq;\n\n[GlobalSetup]\npublic void Setup()\n{\n    _cq = new ConcurrentQueue&lt;int&gt;();\n    for (int i = 0; i &lt; 100; i++)\n    {\n        _cq.Enqueue(i);\n    }\n}\n\n[Benchmark]\npublic void EnqueueCountDequeue()\n{\n    _cq.Enqueue(42);\n    _ = _cq.Count;\n    _cq.TryDequeue(out _);\n}<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<th align=\"right\">\n        Allocated\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        EnqueueCountDequeue\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        708.48 ns\n      <\/td>\n<td align=\"right\">\n        23.8638 ns\n      <\/td>\n<td align=\"right\">\n        21.1546 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.1669\n      <\/td>\n<td align=\"right\">\n        0.0830\n      <\/td>\n<td align=\"right\">\n        0.0010\n      <\/td>\n<td align=\"right\">\n        704 B\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        EnqueueCountDequeue\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        22.79 ns\n      <\/td>\n<td align=\"right\">\n        0.4471 ns\n      <\/td>\n<td align=\"right\">\n        0.4182 ns\n      <\/td>\n<td align=\"right\">\n        0.03\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0<\/p>\n<p><code>ImmutableDictionary&lt;TKey, TValue&gt;<\/code>\u00a0also got some attention. A customer reported that they&#8217;d compared\u00a0<code>ImmutableDictionary&lt;TKey, TValue&gt;<\/code>\u00a0and\u00a0<code>Dictionary&lt;TKey, TValue&gt;<\/code>\u00a0and found the former to be measurably slower for lookups. This is to be expected, as the types use very different data structures, with\u00a0<code>ImmutableDictionary<\/code>\u00a0optimized for being able to inexpensively create a copy of the dictionary with a mutation, something that&#8217;s quite expensive to do with\u00a0<code>Dictionary<\/code>; the trade-off is that it ends up being slower for lookups. Still, it caused us to take a look at the costs involved in\u00a0<code>ImmutableDictionary<\/code>\u00a0lookups, and PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/35759\">dotnet\/corefx#35759<\/a>\u00a0includes several tweaks to improve it, changing a recursive call to be non-recursive and inlinable and avoiding some unnecessary struct wrapping. While this doesn&#8217;t make\u00a0<code>ImmutableDictionary<\/code>\u00a0and\u00a0<code>Dictionary<\/code>\u00a0lookups equivalent, it does improve\u00a0<code>ImmutableDictionary<\/code>\u00a0measurably, especially when it contains just a few elements.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">private ImmutableDictionary&lt;int, int&gt; _hundredInts;\n\n[GlobalSetup]\npublic void Setup()\n{\n    _hundredInts = ImmutableDictionary.Create&lt;int, int&gt;();\n    for (int i = 0; i &lt; 100; i++)\n    {\n        _hundredInts = _hundredInts.Add(i, i);\n    }\n}\n\n[Benchmark]\npublic int Lookup()\n{\n    int count = 0;\n    {\n        for (int i = 0; i &lt; 100; i++)\n        {\n            for (int j = 0; j &lt; 100; j++)\n            {\n                if (_hundredInts.TryGetValue(j, out _))\n                {\n                    count++;\n                }\n            }\n        }\n    }\n    return count;\n}<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Median\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        RatioSD\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        Lookup\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        303.9 us\n      <\/td>\n<td align=\"right\">\n        7.271 us\n      <\/td>\n<td align=\"right\">\n        15.016 us\n      <\/td>\n<td align=\"right\">\n        297.8 us\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        Lookup\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        174.5 us\n      <\/td>\n<td align=\"right\">\n        3.360 us\n      <\/td>\n<td align=\"right\">\n        2.806 us\n      <\/td>\n<td align=\"right\">\n        174.5 us\n      <\/td>\n<td align=\"right\">\n        0.57\n      <\/td>\n<td align=\"right\">\n        0.03\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 Another collection that&#8217;s seen measurable improvements in .NET Core 3.0 is\u00a0<\/p>\n<p><code>BitArray<\/code>. Lots of operations, including construction, were optimized in PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/33367\">dotnet\/corefx#33367<\/a>.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">private byte[] _bytes = Enumerable.Range(0, 100).Select(i =&gt; (byte)i).ToArray();\n\n[Benchmark]\npublic BitArray BitArrayCtor() =&gt; new BitArray(_bytes);<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Median\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        RatioSD\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        BitArrayCtor\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        82.28 ns\n      <\/td>\n<td align=\"right\">\n        2.601 ns\n      <\/td>\n<td align=\"right\">\n        7.546 ns\n      <\/td>\n<td align=\"right\">\n        77.89 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        BitArrayCtor\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        46.87 ns\n      <\/td>\n<td align=\"right\">\n        2.738 ns\n      <\/td>\n<td align=\"right\">\n        8.030 ns\n      <\/td>\n<td align=\"right\">\n        44.63 ns\n      <\/td>\n<td align=\"right\">\n        0.57\n      <\/td>\n<td align=\"right\">\n        0.10\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 Core operations like\u00a0<\/p>\n<p><code>Set<\/code>\u00a0and\u00a0<code>Get<\/code>\u00a0were further improved in PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/35364\">dotnet\/corefx#35364<\/a>\u00a0from @omariom by streamlining the relevant methods and making them inlineable<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"wrap:false lang:default decode:true\">private BitArray _ba = new BitArray(Enumerable.Range(0, 1000).Select(i =&gt; i % 2 == 0).ToArray());\n\n[Benchmark]\npublic void GetSet()\n{\n    BitArray ba = _ba;\n    for (int i = 0; i &lt; 1000; i++)\n    {\n        ba.Set(i, !ba.Get(i));\n    }\n}<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        GetSet\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        6.497 us\n      <\/td>\n<td align=\"right\">\n        0.0854 us\n      <\/td>\n<td align=\"right\">\n        0.0713 us\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        GetSet\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        2.049 us\n      <\/td>\n<td align=\"right\">\n        0.0233 us\n      <\/td>\n<td align=\"right\">\n        0.0218 us\n      <\/td>\n<td align=\"right\">\n        0.32\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 while other operations like\u00a0<\/p>\n<p><code>Or<\/code>,\u00a0<code>And<\/code>, and\u00a0<code>Xor<\/code>\u00a0were vectorized in PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/33781\">dotnet\/corefx#33781<\/a>. This benchmark highlights some of the wins.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">private BitArray _ba1 = new BitArray(Enumerable.Range(0, 1000).Select(i =&gt; i % 2 == 0).ToArray());\nprivate BitArray _ba2 = new BitArray(Enumerable.Range(0, 1000).Select(i =&gt; i % 2 == 1).ToArray());\n\n[Benchmark]\npublic void Xor() =&gt; _ba1.Xor(_ba2);<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        Xor\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        28.57 ns\n      <\/td>\n<td align=\"right\">\n        0.4086 ns\n      <\/td>\n<td align=\"right\">\n        0.3822 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        Xor\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        10.92 ns\n      <\/td>\n<td align=\"right\">\n        0.0924 ns\n      <\/td>\n<td align=\"right\">\n        0.0772 ns\n      <\/td>\n<td align=\"right\">\n        0.38\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 Another example:\u00a0<\/p>\n<p><code>SortedSet&lt;T&gt;<\/code>. PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/30921\">dotnet\/corefx#30921<\/a>\u00a0from @acerbusace tweaks how\u00a0<code>GetViewBetween<\/code>\u00a0changes how counts of the overall set and subset are managed, resulting in a nice performance boost.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">private SortedSet&lt;int&gt; _set = new SortedSet&lt;int&gt;(Enumerable.Range(0, 1000));\n\n[Benchmark]\npublic int EnumerateViewBetween()\n{\n    int count = 0;\n    foreach (int item in _set.GetViewBetween(100, 200)) count++;\n    return count;\n}<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<th align=\"right\">\n        Allocated\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        EnumerateViewBetween\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        5.117 us\n      <\/td>\n<td align=\"right\">\n        0.0590 us\n      <\/td>\n<td align=\"right\">\n        0.0552 us\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.2518\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        544 B\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        EnumerateViewBetween\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        2.510 us\n      <\/td>\n<td align=\"right\">\n        0.0307 us\n      <\/td>\n<td align=\"right\">\n        0.0287 us\n      <\/td>\n<td align=\"right\">\n        0.49\n      <\/td>\n<td align=\"right\">\n        0.1373\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        288 B\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 Comparers have also seen some nice improvements in .NET Core 3.0. For example, PR\u00a0<\/p>\n<p><a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/21604\">dotnet\/coreclr#21604<\/a>\u00a0overhauled how comparers for enums are implemented in the runtime, borrowing the approach used in CoreRT. It&#8217;s often the case that performance optimizations involve adding code; this is one of those fortuitous cases where the better approach is not only faster, it&#8217;s also simpler and smaller.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">private enum ExampleEnum : byte { A, B }\n\n[Benchmark]\npublic void CompareEnums()\n{\n    var comparer = Comparer&lt;ExampleEnum&gt;.Default;\n    for (int i = 0; i &lt; 100_000_000; i++)\n    {\n        comparer.Compare(ExampleEnum.A, ExampleEnum.B);\n    }\n}<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        RatioSD\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        CompareEnums\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        239.5 ms\n      <\/td>\n<td align=\"right\">\n        10.130 ms\n      <\/td>\n<td align=\"right\">\n        10.403 ms\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        CompareEnums\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        131.7 ms\n      <\/td>\n<td align=\"right\">\n        2.479 ms\n      <\/td>\n<td align=\"right\">\n        2.319 ms\n      <\/td>\n<td align=\"right\">\n        0.55\n      <\/td>\n<td align=\"right\">\n        0.03\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0<\/p>\n<h3>Networking<\/h3>\n<p>From the Kestrel web server running on\u00a0<code>System.Net.Sockets<\/code>\u00a0and\u00a0<code>System.Net.Security<\/code>\u00a0to applications accessing web services via\u00a0<code>HttpClient<\/code>,\u00a0<code>System.Net<\/code>\u00a0now more than ever is critical path for many applications. It received a lot of attention in .NET Core 2.1, and continues to in .NET Core 3.0. Let&#8217;s start with\u00a0<code>HttpClient<\/code>. One improvement made in PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/32820\">dotnet\/corefx#32820<\/a>\u00a0was around how buffering is handled, and in particular better respecting larger buffer size requests made as part of copying the response data when a content length was provided by the server. On a fast connection and with a large response body (such as the 10MB in this example), this can make a sizeable difference in throughput due to reduced syscalls to transfer data.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"wrap:false lang:default decode:true\">private HttpClient _client = new HttpClient();\nprivate Socket _listener;\nprivate Uri _uri;\n\n[GlobalSetup]\npublic void Setup()\n{\n    _listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);\n    _listener.Bind(new IPEndPoint(IPAddress.Loopback, 0));\n    _listener.Listen(int.MaxValue);\n    var ep = (IPEndPoint)_listener.LocalEndPoint;\n    _uri = new Uri($\"http:\/\/{ep.Address}:{ep.Port}\");\n\n    Task.Run(async () =&gt;\n    {\n        while (true)\n        {\n            Socket s = await _listener.AcceptAsync();\n            var ignored = Task.Run(async () =&gt;\n            {\n                ReadOnlyMemory&lt;byte&gt; headers = Encoding.ASCII.GetBytes(\"HTTP\/1.1 200 OK\\r\\nContent-Length: 10485760\\r\\n\\r\\n\");\n                ReadOnlyMemory&lt;byte&gt; data = new byte[10*1024*1024]; \/\/ 10485760\n\n                using (var serverStream = new NetworkStream(s, true))\n                using (var reader = new StreamReader(serverStream))\n                {\n                    while (true)\n                    {\n                        while (!string.IsNullOrEmpty(await reader.ReadLineAsync())) ;\n                        await s.SendAsync(headers, SocketFlags.None);\n                        await s.SendAsync(data, SocketFlags.None);\n                    }\n                }\n            });\n        }\n    });\n}\n\n[Benchmark]\npublic async Task HttpDownload()\n{\n    using (HttpResponseMessage r = await _client.GetAsync(_uri, HttpCompletionOption.ResponseHeadersRead))\n    using (Stream s = await r.Content.ReadAsStreamAsync())\n    {\n        await s.CopyToAsync(Stream.Null);\n    }\n}<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr style=\"height: 27px;\">\n<th style=\"height: 27px; width: 103.333px;\">\n        Method\n      <\/th>\n<th style=\"height: 27px; width: 105.333px;\">\n        Toolchain\n      <\/th>\n<th style=\"height: 27px; width: 68.6667px;\" align=\"right\">\n        Mean\n      <\/th>\n<th style=\"height: 27px; width: 78px;\" align=\"right\">\n        Error\n      <\/th>\n<th style=\"height: 27px; width: 78px;\" align=\"right\">\n        StdDev\n      <\/th>\n<th style=\"height: 27px; width: 40.6667px;\" align=\"right\">\n        Ratio\n      <\/th>\n<th style=\"height: 27px; width: 62.6667px;\" align=\"right\">\n        RatioSD\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"height: 27px;\">\n<td style=\"height: 27px; width: 103.333px;\">\n        HttpDownload\n      <\/td>\n<td style=\"height: 27px; width: 105.333px;\">\n        netcoreapp2.1\n      <\/td>\n<td style=\"height: 27px; width: 68.6667px;\" align=\"right\">\n        8.792 ms\n      <\/td>\n<td style=\"height: 27px; width: 78px;\" align=\"right\">\n        0.1833 ms\n      <\/td>\n<td style=\"height: 27px; width: 78px;\" align=\"right\">\n        0.3397 ms\n      <\/td>\n<td style=\"height: 27px; width: 40.6667px;\" align=\"right\">\n        1.00\n      <\/td>\n<td style=\"height: 27px; width: 62.6667px;\" align=\"right\">\n        0.00\n      <\/td>\n<\/tr>\n<tr style=\"height: 27px;\">\n<td style=\"height: 27px; width: 103.333px;\">\n        HttpDownload\n      <\/td>\n<td style=\"height: 27px; width: 105.333px;\">\n        netcoreapp3.0\n      <\/td>\n<td style=\"height: 27px; width: 68.6667px;\" align=\"right\">\n        4.615 ms\n      <\/td>\n<td style=\"height: 27px; width: 78px;\" align=\"right\">\n        0.0356 ms\n      <\/td>\n<td style=\"height: 27px; width: 78px;\" align=\"right\">\n        0.0278 ms\n      <\/td>\n<td style=\"height: 27px; width: 40.6667px;\" align=\"right\">\n        0.52\n      <\/td>\n<td style=\"height: 27px; width: 62.6667px;\" align=\"right\">\n        0.02\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 Now consider\u00a0<\/p>\n<p><code>SslStream<\/code>. Previous releases saw work done to make reads and writes on\u00a0<code>SslStream<\/code>\u00a0much more efficient, but additional work was done in .NET Core 3.0 as part of PRs\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/35091\">dotnet\/corefx#35091<\/a>\u00a0and\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/35209\">dotnet\/corefx#35209<\/a>\u00a0(and\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/35367\">dotnet\/corefx#35367<\/a>\u00a0on Unix) to make initiating the connection more efficient, in particular in terms of allocations.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"wrap:false lang:default decode:true\">private NetworkStream _client;\nprivate NetworkStream _server;\n\nprivate static X509Certificate2 s_cert = GetServerCertificate();\n\nprivate static X509Certificate2 GetServerCertificate()\n{\n    var certCollection = new X509Certificate2Collection();\n    byte[] testCertBytes = Convert.FromBase64String(@\"MIIVBAIBAzCCFMAGCSqGSIb3DQEHAaCCFLEEghStMIIUqTCCCooGCSqGSIb3DQEHAaCCCnsEggp3MIIKczCCCm8GCyqGSIb3DQEMCgECoIIJfjCCCXowHAYKKoZIhvcNAQwBAzAOBAhCAauyUWggWwICB9AEgglYefzzX\/jx0b+BLU\/TkAVj1KBpojf0o6qdTXV42drqIGhX\/k1WwF1ypVYdHeeuDfhH2eXHImwPTw+0bACY0dSiIHKptm0sb\/MskoGI8nlOtHWLi+QBirJ9LSUZcBNOLwoMeYLSFEWWBT69k\/sWrc6\/SpDoVumkfG4pZ02D9bQgs1+k8fpZjZGoZp1jput8CQXPE3JpCsrkdSdiAbWdbNNnYAy4C9Ej\/vdyXJVdBTEsKzPYajAzo6Phj\/oS\/J3hMxxbReMtj2Z0QkoBBVMc70d+DpAK5OY3et872D5bZjvxhjAYh5JoVTCLTLjbtPRn1g7qh2dQsIpfQ5KrdgqdImshHvxgL92ooC1eQVqQffMnZ0\/LchWNb2rMDa89K9CtAefEIF4ve2bOUZUNFqQ6dvd90SgKq6jNfwQf\/1u70WKE86+vChXMMcHFeKso6hTE9+\/zuUPNVmbRefYAtDd7ng996S15FNVdxqyVLlmfcihX1jGhTLi\/\/WuMEaOfXJ9KiwYUyxdUnMp5QJqO8X\/tiwnsuhlFe3NKMXY77jUe8F7I+dv5cjb9iKXAT+q8oYx1LcWu2mj1ER9\/b2omnotp2FIaJDwI40Tts6t4QVH3bUNE9gFIfTMK+WMgKBz\/JAGvC1vbPSdFsWIqwhl7mEYWx83HJp\/+Uqp5f+d8m4phSan2rkHEeDjkUaoifLWHWDmL94SZBrgU6yGVK9dU82kr7jCSUTrnga8qDYsHwpQ22QZtu0aOJGepSwZU7NZNMiyX6QR2hI0CNMjvTK2VusHFB+qnvw+19DzaDT6P0KNPxwBwp07KMQm3HWTRNt9u6gKUmo5FHngoGte+TZdY66dAwCl0Pt+p1v18XlOB2KOQZKLXnhgikjOwYQxFr3oTb2MjsP6YqnSF9EpYpmiNySXiYmrYxVinHmK+5JBqoQCN2C3N24slZkYq+AYUTnNST7Ib2We3bBICOFdVUgtFITRW40T+0XZnIv8G1Kbaq\/1avfWI\/ieKKxyiYp\/ZNXaxc+ycgpsSsAJEuhb83bUkSBpGg9PvFEF0DXm4ah67Ja1SSTmvrCnrOsWZXIpciexMWRGoKrdvd7Yzj9E8hiu+CGTC4T6+7FxVXJrjCg9zU9G2U6g7uxzoyjGj1wqkhxgvl9pPbz6\/KqDRLOHCEwRF4qlWXhsJy4levxGtifFt6n7DWaNSsOUf8Nwpi+d4fd7LQ7B5tW\/y+\/vVZziORueruCWO4LnfPhpJ70g18uyN7KyzrWy29rpE46rfjZGGt0WDZYahObPbw6HjcqSOuzwRoJMxamQb2qsuQnaBS6Bhb5PAnY4SEA045odf\/u9uC7mLom2KGNHHz6HrgEPas2UHoJLuxYvY1pza\/29akuVQZQUvMA5yMFHHGYZLtTKtCGdVGwX0+QS6ovpV93xux4I\/5TrD5U8z9RmTdAx03R3MUhkHF7Zbv5egDNsVar+41YWG4VkV1ZXtsZRKJf0hvKNvrpH0e7fVKBdXljm5PXOSg2VdtkhhOpnKKSMcv6MbGWVi\/svWLnc7Qim4A4MDaz+bFVZmh3oGJ7WHvRQhWIcHUL+YJx+064+4IKXZJ\/2a\/+b2o7C8mJ3GGSBx831ADogg6MRWZx3UY19OZ8YMvpzmZEBRZZnm4KgNpj+SQnf6pGzD2cmnRhzG60LSNPb17iKbdoUAEMkgt2tlMKXpnt1r7qwsIoTt407cAdCEsUH7OU\/AjfFmSkKJZ7vC5HweqZPnhgJgZ6LYHlfiRzUR1xeDg8JG0nb0vb7LUE4nGPy39\/TxIGos7WNwGpG1QVL\/8pKjFdjwREaR8e5CSTlQ7gxHV+G3FFvFGpA1p8cRFzlgE6khDLrSJIUkhkHMA3oFwwAzBNIKVXjToyxCogDqxWya0E1Hw5rVCS\/zOCS1De2XQbXs\/\/g46TW0wTJwvgNbs0xLShf3XB+23meeEsMTCR0+igtMMMsh5K\/vBUGcJA27ru\/KM9qEBcseb\/tqCkhhsdj1dnH0HDmpgFf5DfVrjm+P6ickcF2b+Ojr9t7XHgFszap3COpEPGmeJqNOUTuU53tu\/O774IBgqINMWvvG65yQwsEO06jRrFPRUGb0eH6UM4vC7wbKajnfDuI\/EXSgvuOSZ9wE8DeoeK\/5We4pN7MSWoDl39gI\/LBoNDKFYEYuAw\/bhGp8nOwDKki4a16aYcBGRClpN3ymrdurWsi7TjyFHXfgW8fZe4jXLuKRIk19lmL1gWyD+3bT3mkI2cU2OaY2C0fVHhtiBVaYbxBV8+kjK8q0Q70zf0r+xMHnewk9APFqUjguPguTdpCoH0VAQST9Mmriv\/J12+Y+fL6H+jrtDY2zHPxTF85pA4bBBnLA7Qt9TKCe6uuWu5yBqxOV3w2Oa4Pockv1gJzFbVnwlEUWnIjbWVIyo9vo4LBd03uJHPPIQbUp9kCP\/Zw+Zblo42\/ifyY+a+scwl1q1dZ7Y0L92yJCKm9Qf6Q+1PBK+uU9pcuVTg\/Imqcg5T7jFO5QCi88uwcorgQp+qoeFi0F9tnUecfDl6d0PSgAPnX9XA0ny3bPwSiWOA8+uW73gesxnGTsNrtc1j85tail8N6m6S2tHXwOmM65J4XRZlzzeM4D\/Rzzh13xpRA9kzm9T2cSHsXEYmSW1X7WovrmYhdOh9K3DPwSyG4tD58cvC7X79UbOB+d17ieo7ZCj+NSLVQO1BqTK0QfErdoVHGKfQG8Lc\/ERQRqj132Mhi2\/r5Ca7AWdqD7\/3wgRdQTJSFXt\/akpM44xu5DMTCISEFOLWiseSOBtzT6ssaq2Q35dCkXp5wVbWxkXAD7Gm34FFXXyZrJWAx45Y40wj\/0KDJoEzXCuS4Cyiskx1EtYNNOtfDC5wngywmINFUnnW0NkdKSxmDJvrT6HkRKN8ftik7tP4ZvTaTS28Z0fDmWJ+RjvZW+vtF6mrIzYgGOgdpZwG0ZOSKrXKrY3xpMO16fXyawFfBosLzCty7uA57niPS76UXdbplgPanIGFyceTg1MsNDsd8vszXd4KezN2VMaxvw+93s0Uk\/3Mc+5MAj+UhXPi5UguXMhNo\/CU7erzyxYreOlAI7ZzGhPk+oT9g\/MqWa5RpA2IBUaK\/wgaNaHChfCcDj\/J1qEl6YQQboixxp1IjQxiV9bRQzgwf31Cu2m\/FuHTTkPCdxDK156pyFdhcgTpTNy7RPLDGB3TATBgkqhkiG9w0BCRUxBgQEAQAAADBdBgkrBgEEAYI3EQExUB5OAE0AaQBjAHIAbwBzAG8AZgB0ACAAUwB0AHIAbwBuAGcAIABDAHIAeQBwAHQAbwBnAHIAYQBwAGgAaQBjACAAUAByAG8AdgBpAGQAZQByMGcGCSqGSIb3DQEJFDFaHlgAQwBlAHIAdABSAGUAcQAtADcAOQA4AGUANQA4AGIANQAtAGMAOQA2ADQALQA0ADcAZQA2AC0AYQAzADIAOQAtADAAMQBjAGEAZABmADcANgAyAGEANgA5MIIKFwYJKoZIhvcNAQcGoIIKCDCCCgQCAQAwggn9BgkqhkiG9w0BBwEwHAYKKoZIhvcNAQwBBjAOBAh+t0PMVhyoagICB9CAggnQwKPcfNq8ETOrNesDKNNYJVXnWoZ9Qjgj9RSpj+pUN5I3B67iFpXClvnglKbeNarNCzN4hXD0I+ce+u+Q3iy9AAthG7uyYYNBRjCWcBy25iS8htFUm9VoV9lH8TUnS63Wb\/KZnowew2HVd8QI\/AwQkRn8MJ200IxR\/cFD4GuVO\/Q76aqvmFb1BBHItTerUz7t9izjhL46BLabJKx6Csqixle7EoDOsTCA3H1Vmy2\/Hw3FUtSUER23jnRgpRTA48M6\/nhlnfjsjmegcnVBoyCgGaUadGE5OY42FDDUW7wT9VT6vQEiIfKSZ7fyqtZ6n4+xD2rVySVGQB9+ROm0mywZz9PufsYptZeB7AfNOunOAd2k1F5y3qT0cjCJ+l4eXr9KRd2lHOGZVoGq+e08ylBQU5HB+Tgm6mZaEO2QgzXOAt1ilS0lDii490DsST62+v58l2R45ItbRiorG\/US7+HZHjHUY7EsDUZ+gn3ZZNqh1lAoli5bC1xcjEjNdqq0knyCAUaNMG59UhCWoB6lJpRfVEeQOm+TjgyGw6t3Fx\/6ulNPc1V\/wcascmahH3kgHL146iJi1p2c2yIJtEB+4zrbYv7xH73c8qXVh\/VeuD80I\/+QfD+GaW0MllIMyhCHcduFoUznHcDYr5GhJBhU62t6sNnSjtEU1bcd20oHrBwrpkA7g3\/Mmny33IVrqooWFe876lvQVq7GtFu8ijVyzanZUs\/Cr7k5xX3zjh6yUMAbPiSnTHCl+SEdttkR936fA6de8vIRRGj6eAKqboRxgC1zgsJrj7ZVI7h0QlJbodwY2jzyzcC5khn3tKYjlYeK08iQnzeK5c9JVgQAHyB4uOyfbE50oBCYJE7npjyV7LEN2f7a3GHX4ZWI3pTgbUv+Q1t8BZozQ4pcFQUE+upYucVL3Fr2T8f7HF4G4KbDE4aoLiVrYjy0dUs7rCgjeKu21UPA\/BKx4ebjG+TZjUSGf8TXqrJak1PQOG4tExNBYxLtvBdFoOAsYsKjTOfMYpPXp4vObfktFKPcD1dVdlXYXvS5Dtz3qEkwmruA9fPQ6FYi+OFjw0Pkwkr5Tz+0hRMGgb1JRgVo8SVlW\/NZZIEbKJdW5ZVLyMzdd1dC0ogNDZLPcPR\/HENe2UXtq+0qQw0ekZ+aC2\/RvfAMr5XICX8lHtYmQlAFGRhFNuOysHj7V2AJTuOx2wCXtGzrTPc6eyslsWyJign8bD1r+gkejx\/qKBwwTvZF1aSmiQmFnmMm0jLj7n8v7v6zHCFTuKF1bHZ44eIwMaUDl6MAgHDdvkPl56rYgq\/TM3dKuXnu47GLiRei0EXTT9OMCKcI6XYICsge81ET3k15VfLyI1LNufgqAsafnwl31yqntscXW0NsxW6SkmyXaW1mndxejLBQRjik3civBGTgxgKQbZaO9ZGOrjsSogcCSne+s0zLDxEFjmaYYtpIaU8SFWDja5jyo0jvM3OHUwvElvndZJgreFGG5cKHgwgGKdkYgx6YAvucrgQwqKE\/+nxuhkKWtV9D4h9qFAqZbWc9jOPtWx9h3U3gX3NTLY\/4Z4iy\/FXR9KnKUtCmD1MSRRIOiMca1sNTga3mP\/+qSS5u+pyon5c4c\/jLdEW0GapDz\/yvQcc0MP\/21vSoeIkUN+w\/RzUBvxrawhHGx+FeLlI249+LBKNBQu4Fbw6G9AYpPJf3PdNc0GRMnantA4B7Rm2NsSGdqqrEMuCw1XxzR6ki4jbLC\/ASbcVMr54YsBw+45sggenFshRrYm0QXoUM5XoqEtesby6YfPAjBldyB\/QcuULV6QyAeL44YmxOnKD5E5qQwgfcZUxN01eBgbeSS7bZI3zpFwAMdMQ+dtwHXMuhVXuUGLmNTvNe9DupfPGKbaM8louY1Xw4fmg4PaY7MP2mdYQlEXvSg2geICJVuGRBirH+Xv8VPr7lccN++LXv2NmggoUo\/d18gvhY8XtOrOMon1QGANPh7SzBjR3v19JD170Z6GuZCLtMh681YkKwW\/+Em5rOtexoNQRTjZLNSTthtMyLfAqLk6lZnbbh+7VdCWVfzZoOzUNV+fVwwvyR9ouIzrvDoZ5iGRZU8rEuntap6rBrf9F3FMsz4mvPlCAMp15sovLFpVI8t+8OmKmqQH3LOwd03s6iMJ+0YEWrCaTQYu3kEKoOWC3uhGE8XLSjZBqc3kwVIlzVzOBr97SGjG88JYVDW2FrjQbIv+1yTzOYzMnCDUW3T8GMtfYEQbN6ZtBaD9i4ZeZlQCdkfGuNC6OYO98L7fU4frgff8nNfeka8kHtvNMn4CosFKBRXA5y+kqEE0Qk5feZhfM8NX9x3O0CJobm4HC57VxJ3c0jTe2SA0gAfB4g0keghmDzYgjQAuIY\/o1LMKFiBNue4fnXlhU1L402Zlx\/lzKDera6o3Xgh9IXj3ZqyFlXa9bkyKDtek0ephTZulLc3NLeb1a3KZxId8OmplR8OcZsHluEu+Z3Der0j8Ro7X7kOnNkUxuTV2blqZ4V8DsYKATeKv4ffc1Ub8MLBd9hMs8ehjmC5jkYApM5HvXl4411mPN6MrF8f2hPVgqrd3p\/M80c8wNWjvWIvPLr9Tjqk71hKBq3+Hu0oI1zuoTY2BOhBLyvpjM+mvRd8UlrFJTLGTyCAXvAhIDRIVyrGuscO5Y0sfDc+82Bvrua4FyhZkjb1r8GrGciH0V5HHKjg5dewWnr21qf4q96yf2\/ZjoldFFvKiCd8wum9ZV1OaTbjjg46oSpIyBzxl4qpfrgT1ZX1MvGW4uAJ7WQHjSAex7VGr1Sl+ghe5PQBbURyFiu9PnBRMOMjGYkI2lngd3bdehc+i2fPnNe5LgdsBbmUKmEJH96rlkFT8Co+NYBWKBUsBXyfC+kwXDRyNrt2r7VafWWz\/cwK0\/AJ\/Ucq4vz8E0mzy03Gs+ePW+tP9JOHP6leF0TLhbItvQl3DJy0gj6TyrO9S077EVyukFCXeH1\/yp04lmq4G0urU+pUf2wamP4BVNcVsikPMYo\/e75UI330inXG4+SbJ40q\/MQIfYnXydhVmWVCUXkfRFNbcCu7JclIrzS1WO26q6BOgs2GhA3nEan8CKxa85h\/oCaDPPMGhkQtCU75vBqQV9Hk2+W5zMSSj7R9RiH34MkCxETtY8IwKa+kiRAeMle8ePAmT6HfcBOdTsVGNoRHQAOZewwUycrIOYJ\/54WOmcy9JZW9\/clcgxHGXZq44tJ3BDHQQ4qBgVd5jc9Qy9\/fGS3YxvsZJ3iN7IMs4Jt3GWdfvwNpJaCBJjiiUntJPwdXMjAeUEZ16Tmxdb1l42rjFSCptMJS2N2EPSNb36+staNgzflctLLpmyEK4wyqjA7MB8wBwYFKw4DAhoEFIM7fHJcmsN6HkU8HxypGcoifg5MBBRXe8XL349R6ZDmsMhpyXbXENCljwICB9A=\");\n    certCollection.Import(testCertBytes, \"testcertificate\", X509KeyStorageFlags.DefaultKeySet);\n    return certCollection.Cast&lt;X509Certificate2&gt;().First(c =&gt; c.HasPrivateKey);\n}\n\n[GlobalSetup]\npublic void Setup()\n{\n    using (var listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))\n    {\n        listener.Bind(new IPEndPoint(IPAddress.Loopback, 0));\n        listener.Listen(1);\n\n        var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);\n        client.Connect(listener.LocalEndPoint);\n        Socket server = listener.Accept();\n\n        _client = new NetworkStream(client);\n        _server = new NetworkStream(server);\n    }\n}\n\n[Benchmark]\npublic void SslConnect()\n{\n    using (var sslClient = new SslStream(_client, true, delegate { return true; }))\n    using (var sslServer = new SslStream(_server, true, delegate { return true; }))\n    {\n        Task t = sslServer.AuthenticateAsServerAsync(s_cert, false, SslProtocols.None, false);\n        sslClient.AuthenticateAsClient(\"localhost\", null, SslProtocols.None, false);\n        t.Wait();\n    }\n}<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        RatioSD\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<th align=\"right\">\n        Allocated\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        SslConnect\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        1,151.7 us\n      <\/td>\n<td align=\"right\">\n        34.85 us\n      <\/td>\n<td align=\"right\">\n        102.76 us\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<td align=\"right\">\n        5.8594\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        9.82 KB\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        SslConnect\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        915.5 us\n      <\/td>\n<td align=\"right\">\n        17.73 us\n      <\/td>\n<td align=\"right\">\n        26.54 us\n      <\/td>\n<td align=\"right\">\n        0.80\n      <\/td>\n<td align=\"right\">\n        0.08\n      <\/td>\n<td align=\"right\">\n        1.9531\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        4.13 KB\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 In\u00a0<\/p>\n<p><code>System.Net.Sockets<\/code>\u00a0there&#8217;s another example of taking advantage of the\u00a0<code>IThreadPoolWorkItem<\/code>\u00a0interface discussed earlier. On Windows for asynchronous operations, we utilize &#8220;overlapped I\/O&#8221;, utilizing threads from the I\/O thread pool to execute continuations from socket operations; Windows queues I\/O completion packets that these I\/O pool threads then process, including invoking the continuations. On Unix, however, the mechanism is very different. There&#8217;s no concept of &#8220;overlapped I\/O&#8221; on Unix, and instead asynchrony in\u00a0<code>System.Net.Sockets<\/code>\u00a0is achieved by using\u00a0<code>epoll<\/code>\u00a0(or\u00a0<code>kqueues<\/code>\u00a0on macOS), with all of the sockets in the system registered with an\u00a0<code>epoll<\/code>\u00a0file descriptor, and then one thread monitoring that\u00a0<code>epoll<\/code>\u00a0for changes. Any time an asynchronous operation completes for a socket, the\u00a0<code>epoll<\/code>\u00a0is signaled and the thread blocking on it wakes up to process it. If that thread were to run the socket continuation action then and there, it would end up potentially running unbounded work that could stall every other socket&#8217;s handling indefinitely, and in the extreme case, deadlock. Instead, this thread queues a work item back to the thread pool and then immediately goes back to processing any other socket work. Prior to .NET Core 3.0, that queueing involved an allocation, which meant that every asynchronously completing socket operation on Unix involved at least one allocation. As of PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/32919\">dotnet\/corefx#32919<\/a>, that number drops to zero, as a cached object already being used (and reused) to represent asynchronous operations was changed to also implement\u00a0<code>IThreadPoolWorkItem<\/code>\u00a0and be queueable directly to the thread pool. Other areas of\u00a0<code>System.Net<\/code>\u00a0have benefited from the efforts already alluded to previously, as well. For example,\u00a0<code>Dns.GetHostName<\/code>\u00a0used to use\u00a0<code>StringBuilder<\/code>\u00a0in its marshaling, but as of PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/29594\">dotnet\/corefx#29594<\/a> it no longer does.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">[Benchmark]\npublic string GetHostName() =&gt; Dns.GetHostName();<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        RatioSD\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<th align=\"right\">\n        Allocated\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        GetHostName\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        85.77 us\n      <\/td>\n<td align=\"right\">\n        1.656 us\n      <\/td>\n<td align=\"right\">\n        1.5489 us\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<td align=\"right\">\n        0.4883\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        1176 B\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        GetHostName\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        81.42 us\n      <\/td>\n<td align=\"right\">\n        1.016 us\n      <\/td>\n<td align=\"right\">\n        0.9503 us\n      <\/td>\n<td align=\"right\">\n        0.95\n      <\/td>\n<td align=\"right\">\n        0.02\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        48 B\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 And\u00a0<\/p>\n<p><code>IPAddress.HostToNetworkOrder\/NetworkToHostOrder<\/code>\u00a0have benefiting indirectly from the intrinsics push that was mentioned previously. In .NET Core 2.1,\u00a0<code>BinaryPrimitives.ReverseEndianness<\/code>\u00a0was added with an optimized software implementation, and these<code>\u00a0IPAddress<\/code>\u00a0methods were rewritten as simple wrappers for\u00a0<code>ReverseEndianness<\/code>. Now in .NET Core 3.0, PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/18398\">dotnet\/coreclr#18398<\/a>\u00a0turned\u00a0<code>ReverseEndianness<\/code>\u00a0into a JIT intrinsic for which the JIT can emit a very efficient\u00a0<code>BSWAP<\/code>\u00a0instruction, with the resulting throughput improvements accruing to\u00a0<code>IPAddress<\/code>\u00a0as well.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">private long _value = 1234567890123456789;\n\n[Benchmark]\npublic long HostToNetworkOrder() =&gt; IPAddress.HostToNetworkOrder(_value);<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Median\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        RatioSD\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        HostToNetworkOrder\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        0.4986 ns\n      <\/td>\n<td align=\"right\">\n        0.0398 ns\n      <\/td>\n<td align=\"right\">\n        0.0408 ns\n      <\/td>\n<td align=\"right\">\n        0.4758 ns\n      <\/td>\n<td align=\"right\">\n        1.000\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        HostToNetworkOrder\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        0.0043 ns\n      <\/td>\n<td align=\"right\">\n        0.0090 ns\n      <\/td>\n<td align=\"right\">\n        0.0076 ns\n      <\/td>\n<td align=\"right\">\n        0.0000 ns\n      <\/td>\n<td align=\"right\">\n        0.009\n      <\/td>\n<td align=\"right\">\n        0.02\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3> <\/h3>\n<h3>System.IO<\/h3>\n<p>Often going hand in hand with networking is compression, which has also seen some improvements in .NET Core 3.0. Most notably is that a key dependency was updated. On Unix,\u00a0<code>System.IO.Compression<\/code>\u00a0just uses the zlib library available on the machine, as it&#8217;s a standard part of most any distro\/version. On Windows, however, zlib is generally nowhere to be found, and so it&#8217;s built and shipped as part of .NET Core on Windows. Rather than shipping the standard zlib, .NET Core includes a version modified by Intel with additional performance improvements not yet merged upstream. In .NET Core 3.0, we&#8217;ve sync&#8217;d to the latest available version of ZLib-Intel, version 1.2.11. This brings some very measurable performance improvements, in particular around decompression. There have also been compression-related improvements that take advantage of previous improvements elsewhere in .NET Core. For example, the synchronous\u00a0<code>Stream.CopyTo<\/code>\u00a0was originally non-virtual, but as gains were found by overriding the asynchronous\u00a0<code>CopyToAsync<\/code>\u00a0and specializing its implementation for particular concrete stream types,\u00a0<code>CopyTo<\/code>\u00a0was made virtual to enjoy similar improvements. PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/29751\">dotnet\/corefx#29751<\/a>\u00a0capitalized on this to override\u00a0<code>CopyTo<\/code>\u00a0on\u00a0<code>DeflateStream<\/code>, employing similar optimizations in the synchronous implementation as were employed in the asynchronous implementation, essentially entailing minimizing the interop costs with zlib.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"wrap:false lang:default decode:true\">private byte[] _compressed;\n\n[GlobalSetup]\npublic void Setup()\n{\n    var ms = new MemoryStream();\n    using (var ds = new DeflateStream(ms, CompressionLevel.Fastest))\n    {\n        ds.Write(Enumerable.Range(0, 1_000_000).Select(i =&gt; (byte)i).ToArray(), 0, 1_000_000);\n    }\n    _compressed = ms.ToArray();\n}\n\n[Benchmark]\npublic void DeflateDecompress()\n{\n    using (var ds = new DeflateStream(new MemoryStream(_compressed), CompressionMode.Decompress))\n    {\n        ds.CopyTo(Stream.Null);\n    }\n}<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        DeflateDecompress\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        310.6 us\n      <\/td>\n<td align=\"right\">\n        1.960 us\n      <\/td>\n<td align=\"right\">\n        1.6367 us\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        DeflateDecompress\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        144.9 us\n      <\/td>\n<td align=\"right\">\n        1.050 us\n      <\/td>\n<td align=\"right\">\n        0.9819 us\n      <\/td>\n<td align=\"right\">\n        0.47\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 Improvements were also made to\u00a0<\/p>\n<p><code>BrotliStream<\/code>\u00a0(which as of .NET Core 3.0 is also used by\u00a0<code>HttpClient<\/code>\u00a0to automatically decompress Brotli-encoded content). Previously every new\u00a0<code>BrotliStream<\/code>\u00a0would also allocate a large buffer, but as of PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/35492\">dotnet\/corefx#35492<\/a>, that buffer is pooled, as it is with\u00a0<code>DeflateStream<\/code>\u00a0(additionally,\u00a0<code>BrotliStream<\/code>\u00a0now as of PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/30135\">dotnet\/corefx#30135<\/a>\u00a0overrides\u00a0<code>ReadByte<\/code>\u00a0and\u00a0<code>WriteByte<\/code>\u00a0to avoid allocations in the base implementation).<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">[Benchmark]\npublic void BrotliWrite()\n{\n    using (var bs = new BrotliStream(Stream.Null, CompressionLevel.Fastest))\n    {\n        for (int i = 0; i &lt; 1_000; i++)\n        {\n            bs.WriteByte((byte)i);\n        }\n    }\n}<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<th align=\"right\">\n        Allocated\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        BrotliWrite\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        743.2 us\n      <\/td>\n<td align=\"right\">\n        10.056 us\n      <\/td>\n<td align=\"right\">\n        9.406 us\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        44.9219\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        97680 B\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        BrotliWrite\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        575.5 us\n      <\/td>\n<td align=\"right\">\n        9.181 us\n      <\/td>\n<td align=\"right\">\n        8.588 us\n      <\/td>\n<td align=\"right\">\n        0.77\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        136 B\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 Moving on from compression, it&#8217;s worth highlighting that formatting applies in more situations than just formatting individual primitives.\u00a0<\/p>\n<p><code>TextWriter<\/code>, for example, has multiple methods for writing with format strings, e.g.\u00a0<code>public override void Write(string format, object arg0, arg1)<\/code>. PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/19235\">dotnet\/coreclr#19235<\/a>\u00a0improved on that for StreamWriter by providing specialized overrides that take a more efficient path that reduces allocation:<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">private StreamWriter _writer = new StreamWriter(Stream.Null);\n\n[Benchmark]\npublic void StreamWriterFormat() =&gt; _writer.Write(\"Writing out a value: {0}\", 42);<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<th align=\"right\">\n        Allocated\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        StreamWriterFormat\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        207.4 ns\n      <\/td>\n<td align=\"right\">\n        2.103 ns\n      <\/td>\n<td align=\"right\">\n        1.864 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.0455\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        96 B\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        StreamWriterFormat\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        170.2 ns\n      <\/td>\n<td align=\"right\">\n        1.800 ns\n      <\/td>\n<td align=\"right\">\n        1.595 ns\n      <\/td>\n<td align=\"right\">\n        0.82\n      <\/td>\n<td align=\"right\">\n        0.0114\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        24 B\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 As another example, PR\u00a0<\/p>\n<p><a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/22102\">dotnet\/coreclr#22102<\/a>\u00a0from @TomerWeisberg improved the parsing performance of various primitive types on\u00a0<code>BinaryReader<\/code>\u00a0by special-casing the common situation where the\u00a0<code>BinaryReader<\/code> wraps a\u00a0<code>MemoryStream<\/code>. Or consider PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/30667\">dotnet\/corefx#30667<\/a>\u00a0from @MarcoRossignoli, who added overrides to\u00a0<code>StringWriter<\/code>\u00a0for the\u00a0<code>Write{Line}{Async}<\/code>\u00a0methods that take a\u00a0<code>StringBuilder<\/code>\u00a0argument.\u00a0<code>StringWriter<\/code>\u00a0is just a wrapper around a\u00a0<code>StringBuilder<\/code>, and\u00a0<code>StringBuilder<\/code>\u00a0knows how to append another\u00a0<code>StringBuilder<\/code>\u00a0to it, so these overrides on\u00a0<code>StringWriter<\/code>\u00a0can feed them right through.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">private StringBuilder _underlying;\nprivate StringWriter _writer;\nprivate StringBuilder _sb;\n\n[GlobalSetup]\npublic void Setup()\n{\n    _underlying = new StringBuilder();\n    _writer = new StringWriter(_underlying);\n    _sb = new StringBuilder(\"This is a test. This is only a test.\");\n}\n\n[Benchmark]\npublic void Write()\n{\n    _underlying.Clear();\n    _writer.Write(_sb);\n}<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<th align=\"right\">\n        Allocated\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        Write\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        30.15 ns\n      <\/td>\n<td align=\"right\">\n        0.6065 ns\n      <\/td>\n<td align=\"right\">\n        0.5673 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.0495\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        104 B\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        Write\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        18.57 ns\n      <\/td>\n<td align=\"right\">\n        0.1513 ns\n      <\/td>\n<td align=\"right\">\n        0.1416 ns\n      <\/td>\n<td align=\"right\">\n        0.62\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0<\/p>\n<p><code>System.IO.Pipelines<\/code>\u00a0is another IO-related library that&#8217;s received a lot of attention in .NET Core 3.0. Pipelines was introduced in .NET Core 2.1, and provides buffer-management as part of an I\/O pipeline, used heavily by ASP.NET Core. A variety of PRs have gone into improving its performance. For example, PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/35171\">dotnet\/corefx#35171<\/a>special-cases the common and default case where the\u00a0<code>Pool<\/code>\u00a0specified to be used by a\u00a0<code>Pipe<\/code>\u00a0is the default\u00a0<code>MemoryPool&lt;byte&gt;.Shared<\/code>. Rather than go through\u00a0<code>MemoryPool&lt;byte&gt;.Shared<\/code>\u00a0in this case, the\u00a0<code>Pipe<\/code>\u00a0now bypasses it and goes to the underlying\u00a0<code>ArrayPool&lt;byte&gt;.Shared<\/code>\u00a0directly, which removes a layer of indirection but also the allocation of\u00a0<code>IMemoryOwner&lt;byte&gt;<\/code>\u00a0objects returned from\u00a0<code>MemoryPool&lt;byte&gt;.Rent<\/code>. (Note that for this benchmark, since\u00a0<code>System.IO.Pipelines<\/code>\u00a0is part of a NuGet package rather than in the shared framework, I&#8217;ve added a Benchmark.NET config that specifies what package version to use with each run in order to show the improvements.)<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"wrap:false lang:default decode:true\">\/\/ Run with: dotnet run -c Release -f netcoreapp2.1 --filter *Program*\n\nprivate sealed class Config : ManualConfig \/\/ also add [Config(typeof(Config))] to the Program class\n{\n    public Config()\n    {\n        Add(Job.MediumRun.With(CsProjCoreToolchain.NetCoreApp21).WithNuGet(\"System.IO.Pipelines\", \"4.5.0\").WithId(\"4.5.0\"));\n        Add(Job.MediumRun.With(CsProjCoreToolchain.NetCoreApp30).WithNuGet(\"System.IO.Pipelines\", \"4.6.0-preview5.19224.8\").WithId(\"4.6.0-preview5.19224.8\"));\n    }\n}\n\nprivate readonly Pipe _pipe = new Pipe();\nprivate byte[] _buffer = new byte[1024];\n\n[Benchmark]\npublic async Task ReadWrite()\n{\n    var reader = _pipe.Reader;\n    var writer = _pipe.Writer;\n\n    for (int i = 0; i &lt; 1000; i++)\n    {\n        ValueTask&lt;ReadResult&gt; vt = reader.ReadAsync();\n        await writer.WriteAsync(_buffer);\n        ReadResult rr = await vt;\n        reader.AdvanceTo(rr.Buffer.End);\n    }\n}<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Job\n      <\/th>\n<th>\n        NuGetReferences\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        ReadWrite\n      <\/td>\n<td>\n        4.5.0\n      <\/td>\n<td>\n        System.IO.Pipelines 4.5.0\n      <\/td>\n<td>\n        .NET Core 2.1\n      <\/td>\n<td align=\"right\">\n        406.8 us\n      <\/td>\n<td align=\"right\">\n        12.774 us\n      <\/td>\n<td align=\"right\">\n        17.907 us\n      <\/td>\n<td align=\"right\">\n        11.2305\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        ReadWrite\n      <\/td>\n<td>\n        4.6.0-preview5.19224.8\n      <\/td>\n<td>\n        System.IO.Pipelines 4.6.0-preview5.19224.8\n      <\/td>\n<td>\n        .NET Core 3.0\n      <\/td>\n<td align=\"right\">\n        324.6 us\n      <\/td>\n<td align=\"right\">\n        3.208 us\n      <\/td>\n<td align=\"right\">\n        4.702 us\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 PR\u00a0<\/p>\n<p><a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/33658\">dotnet\/corefx#33658<\/a>\u00a0from @benaadams allows\u00a0<code>Pipe<\/code>\u00a0to use the\u00a0<code>UnsafeQueueUserWorkItem<\/code>boxing-related optimizations described earlier, PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/33755\">dotnet\/corefx#33755<\/a>\u00a0avoids queueing unnecessary work items, PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/35939\">dotnet\/corefx#35939<\/a>\u00a0tweaks the defaults used to better handle buffering in common cases, PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/35216\">dotnet\/corefx#35216<\/a>\u00a0reduces the amount of slicing performed in various pipe operations, PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/35234\">dotnet\/corefx#35234<\/a>\u00a0from @benaadams reduces the locking used in core operations, PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/35509\">dotnet\/corefx#35509<\/a>\u00a0reduces argument validation (decreasing branching costs), PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/33000\">dotnet\/corefx#33000<\/a>\u00a0focused on reducing costs associated with\u00a0<code>ReadOnlySequence&lt;byte&gt;<\/code>\u00a0that&#8217;s the main exchange type pipelines passes around, and PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/29837\">dotnet\/corefx#29837<\/a>\u00a0further optimizes operations like\u00a0<code>GetSpan<\/code>\u00a0and\u00a0<code>Advance<\/code>\u00a0on the\u00a0<code>Pipe<\/code>. The net result is to whittle away at already low CPU and allocation overheads.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"wrap:false lang:default decode:true\">\/\/ Run with: dotnet run -c Release -f netcoreapp2.1 --filter *Program*\n\nprivate sealed class Config : ManualConfig \/\/ also add [Config(typeof(Config))] to the Program class\n{\n    public Config()\n    {\n        Add(Job.MediumRun.With(CsProjCoreToolchain.NetCoreApp21).WithNuGet(\"System.IO.Pipelines\", \"4.5.0\").WithId(\"4.5.0\"));\n        Add(Job.MediumRun.With(CsProjCoreToolchain.NetCoreApp30).WithNuGet(\"System.IO.Pipelines\", \"4.6.0-preview5.19224.8\").WithId(\"4.6.0-preview5.19224.8\"));\n    }\n}\n\nprivate readonly Pipe _pipe1 = new Pipe();\nprivate readonly Pipe _pipe2 = new Pipe();\nprivate byte[] _buffer = new byte[1024];\n\n[GlobalSetup]\npublic void Setup()\n{\n    Task.Run(async () =&gt;\n    {\n        var reader = _pipe2.Reader;\n        var writer = _pipe1.Writer;\n        while (true)\n        {\n            ReadResult rr = await reader.ReadAsync();\n            foreach (ReadOnlyMemory&lt;byte&gt; mem in rr.Buffer)\n            {\n                await writer.WriteAsync(mem);\n            }\n            reader.AdvanceTo(rr.Buffer.End);\n        }\n    });\n}\n\n[Benchmark]\npublic async Task ReadWrite()\n{\n    var reader = _pipe1.Reader;\n    var writer = _pipe2.Writer;\n\n    for (int i = 0; i &lt; 1000; i++)\n    {\n        await writer.WriteAsync(_buffer);\n        long count = 0;\n        while (count &lt; _buffer.Length)\n        {\n            ReadResult rr = await reader.ReadAsync();\n            count += rr.Buffer.Length;\n            reader.AdvanceTo(rr.Buffer.End);\n        }\n    }\n}<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Job\n      <\/th>\n<th>\n        NuGetReferences\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        ReadWrite\n      <\/td>\n<td>\n        4.5.0\n      <\/td>\n<td>\n        System.IO.Pipelines 4.5.0\n      <\/td>\n<td>\n        .NET Core 2.1\n      <\/td>\n<td align=\"right\">\n        3.261 ms\n      <\/td>\n<td align=\"right\">\n        0.0732 ms\n      <\/td>\n<td align=\"right\">\n        0.1002 ms\n      <\/td>\n<td align=\"right\">\n        46.8750\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        ReadWrite\n      <\/td>\n<td>\n        4.6.0-preview5.19224.8\n      <\/td>\n<td>\n        System.IO.Pipelines 4.6.0-preview5.19224.8\n      <\/td>\n<td>\n        .NET Core 3.0\n      <\/td>\n<td align=\"right\">\n        2.947 ms\n      <\/td>\n<td align=\"right\">\n        0.1281 ms\n      <\/td>\n<td align=\"right\">\n        0.1837 ms\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0<\/p>\n<h3>System.Console<\/h3>\n<p><code>Console<\/code>\u00a0isn&#8217;t something one normally thinks of as being performance-sensitive. However, there are two changes in this release that I think are worth calling attention to here. First, there is one area of Console about which we&#8217;ve heard numerous concerns related to performance, where the performance impact visibly impacts users. In particular, interactive console applications generally do a lot of manipulation of the cursor, which also entails asking where the cursor currently is. On Windows, both the setting and getting of the cursor are relatively fast operations, with P\/Invoke calls made to functions exported from kernel32.dll. On Unix, things are more complicated. There&#8217;s no standard POSIX function for getting or setting a terminal&#8217;s cursor position. Instead, there&#8217;s a standard convention for interacting with the terminal via ANSI escape sequences. To set the cursor position, one writes a sequence of characters to stdout (e.g. &#8220;ESC [ 12 ; 34 H&#8221; to indicate 12th row, 34th column) and the terminal interprets that and reacts accordingly. Getting the cursor position is more of an ordeal. To get the current cursor position, an application writes to stdout a request (e.g. &#8220;ESC [ 6 n&#8221;), and in response the terminal writes back to the application&#8217;s stdin a response something like &#8220;ESC [ 12 ; 34 R&#8221;, to indicate the cursor is at the 12th row and 34th column. That response then needs to be read from stdin and parsed. So, in contrast to a fast interop call on Windows, on Unix we need to write, read, and parse text, and do so in a way that doesn&#8217;t cause problems with a user sitting at a keyboard using the app concurrently\u2026 not particularly cheap. When just getting the cursor position now and then, it&#8217;s not a big deal. But when getting it frequently, and when porting code originally written for Windows where the operation was so cheap the code being ported may not have been very frugal with how often it asked for the position (asking for it more than is really needed), this has resulted in visible performance problems. Thankfully, the issue has been addressed in .NET Core 3.0, by PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/36049\">dotnet\/corefx#36049<\/a>\u00a0from @tmds. The change caches the current position and then manually handles updating that cached value based on user interactions, such as handling typing or resizing the terminal window. (Note that Benchmark.NET operates in a way that redirects standard input and output for the process running the test, and that makes Console.CursorLeft\/Top return 0 immediately, so for this test, I&#8217;ve just done a simple console app with a\u00a0<code>Stopwatch<\/code>, which is, as you&#8217;ll see, more than sufficient given the discrepancy between costs in versions.)<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">using System;\nusing System.Diagnostics;\n\npublic class Program\n{\n    static void Main()\n    {\n        var sw = new Stopwatch();\n        for (int iter = 0; iter &lt; 5; iter++)\n        {\n            sw.Restart();\n            for (int i = 0; i &lt; 1_000; i++) { _ = Console.CursorLeft; }\n            sw.Stop();\n            Console.WriteLine(sw.Elapsed.TotalSeconds);\n        }\n    }\n}<\/pre>\n<\/div>\n<pre class=\"lang:default decode:true \">~\/BlogPostBenchmarks$ dotnet run -c Release -f netcoreapp2.1\n18.2152636\n17.9935087\n18.2676408\n17.7891821\n17.4141348\n~\/BlogPostBenchmarks$ dotnet run -c Release -f netcoreapp3.0\n0.0648111\n0.0001539\n0.00013979999999999998\n0.00013529999999999998\n0.0001459<\/pre>\n<p>Another place where\u00a0<code>Console<\/code>\u00a0has been improved affects both Windows and Unix. Interestingly, this change was made for functional reasons (in particular for when running on Windows), but it has performance benefits as well for all OSes. In .NET, most of the times we specify buffer sizes it&#8217;s for performance reasons and represents a trade-off: the smaller the buffer size, the less memory is used but the more times operations may need to be performed to fill that buffer, and conversely the larger the buffer size, the more memory is used but the fewer times the buffer will need to be filled. It&#8217;s rare that the buffer size has a functional impact, but it actually can in\u00a0<code>Console<\/code>. On Windows to read from the console, one calls either the\u00a0<code>ReadFile<\/code>\u00a0or\u00a0<code>ReadConsole<\/code>\u00a0functions, both of which accept a buffer to store the read data into. By default on Windows, reading from the console will not return until a newline, but Windows also needs somewhere to store the typed data, and it does so into the supplied buffer. Thus, Windows won&#8217;t let the user type more characters than can fit into the buffer, which means the line length a user can type is limited by the buffer size. For whatever historical reason, .NET has used a buffer size of 256 characters, limiting the typeable line length to that amount. PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/36212\">dotnet\/corefx#36212<\/a>\u00a0expands that to 4096 characters, which much better matches other programming environments and allows for a much more reasonable line length. However, as is the case when increasing buffer sizes, relevant throughput involving that buffer improves as well, in particular when reading from files piped to stdin. For example, reading 8K of input data from stdin previously would have required 32 calls to\u00a0<code>ReadFile<\/code>; with a 4K buffer, only 2 calls are required. The impact of that can be seen in this benchmark. (Again, this is harder to test with Benchmark.NET, so I&#8217;ve again just used a simple console app.)<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">using System;\nusing System.Diagnostics;\nusing System.IO;\n\npublic class Program\n{\n    static void Main()\n    {\n        \/\/using (var writer = new StreamWriter(@\"tmp.dat\"))\n        \/\/{\n        \/\/    for (int i = 0; i &lt; 10_000_000; i++)\n        \/\/    {\n        \/\/        writer.WriteLine(\"This is a test.  This is only a test.\");\n        \/\/    }\n        \/\/}\n\n        var sw = Stopwatch.StartNew();\n        while (Console.ReadLine() != null) ;\n        Console.WriteLine(sw.Elapsed.TotalSeconds);\n    }\n}<\/pre>\n<\/div>\n<pre class=\"wrap:false lang:default decode:true\">c:\\BlogPostBenchmarks&gt;dotnet run -c Release -f netcoreapp2.1 &lt; c:\\BlogPostBenchmarks\\bin\\Release\\netcoreapp2.1\\tmp.dat\n4.8151814\n\nc:\\BlogPostBenchmarks&gt;dotnet run -c Release -f netcoreapp3.0 &lt; c:\\BlogPostBenchmarks\\bin\\Release\\netcoreapp2.1\\tmp.dat\n1.3161175999999999<\/pre>\n<h3>System.Diagnostics.Process<\/h3>\n<p>There have been various functional improvements to the\u00a0<code>Process<\/code>\u00a0class in .NET Core 3.0, in particular on Unix, but there are a couple of performance-focused improvements I want to call out. PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/31236\">dotnet\/corefx#31236<\/a>\u00a0is another nice example of introducing a new performance-focused API and, at the same time, using it within .NET Core to further improve the performance of core libraries. In this case, it&#8217;s a low-level API on MemoryMarshal that enables efficiently reading structs from spans, something that&#8217;s done in spades as part of the interop in\u00a0<code>System.Diagnostics.Process<\/code>. I like that example, not because it makes for a massive performance improvement, but because it highlights the general pattern I like to see: adding new APIs for others to consume and in the same breath using those APIs to better the technology itself. A more impactful example, though, comes from @joshudson in PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/33289\">dotnet\/corefx#33289<\/a>, which changed the native code used to fork a new process from using the\u00a0<code>fork<\/code>\u00a0function to instead using the\u00a0<code>vfork<\/code>\u00a0function. The benefit of\u00a0<code>vfork<\/code>\u00a0is that it avoids copying the page tables of the parent process into the child process, with the assumption that the child process is then just going to overwrite everything anyway via an almost immediate\u00a0<code>exec<\/code>\u00a0call.\u00a0<code>fork<\/code>\u00a0does copy-on-write, but if the process is modifying a lot of state concurrently (e.g. with the garbage collector running), this can get expensive quickly and unnecessarily. For this benchmark, I&#8217;ve just written a nop C program in a test.c file:<\/p>\n<div class=\"highlight highlight-source-c\">\n<pre class=\"lang:default decode:true\">int main() { return 0; }<\/pre>\n<\/div>\n<p>and compiled it with GCC:<\/p>\n<pre class=\"lang:default decode:true \">gcc -o test test.c<\/pre>\n<p>to give us a target for Process.Start to invoke.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"wrap:false lang:default decode:true\">[Benchmark]\npublic void ProcessStartWait() =&gt; Process.Start(\"\/home\/stephentoub\/BlogPostBenchmarks\/test\").WaitForExit();<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        RatioSD\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<th align=\"right\">\n        Allocated\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        ProcessStartWait\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        1,663.0 us\n      <\/td>\n<td align=\"right\">\n        32.79 us\n      <\/td>\n<td align=\"right\">\n        67.72 us\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        21.45 KB\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        ProcessStartWait\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        536.0 us\n      <\/td>\n<td align=\"right\">\n        10.64 us\n      <\/td>\n<td align=\"right\">\n        28.40 us\n      <\/td>\n<td align=\"right\">\n        0.32\n      <\/td>\n<td align=\"right\">\n        0.02\n      <\/td>\n<td align=\"right\">\n        1.9531\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        16.65 KB\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0<\/p>\n<h3>LINQ<\/h3>\n<p>Previous releases have seen a ton of investment in optimizing LINQ. There&#8217;s less of that in .NET Core 3.0, as a lot of the common patterns have already been covered well. However, there are still some nice improvements to be found in the release. It&#8217;s relatively rare that new operators are added to\u00a0<code>System.Linq<\/code>\u00a0itself, as the very nature of extension methods makes it easy for anyone to build up and share their own library of extension methods they consider to be useful (and several well-established such libraries exist). Even so, .NET Core 2.0 saw a new\u00a0<code>TakeLast<\/code>\u00a0method added. In .NET Core 3.0, PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/36051\">dotnet\/corefx#36051<\/a>\u00a0by @Romasz updated\u00a0<code>TakeLast<\/code>\u00a0to integrate with the internal\u00a0<code>IPartition&lt;T&gt;<\/code>\u00a0interface that enables several operators to cooperate, helping to optimize (in some situations quite heavily) various uses of the operator.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">private IEnumerable&lt;int&gt; _enumerable = new int[1000].Select(i =&gt; i);\n\n[Benchmark]\npublic int SumLast10() =&gt; _enumerable.TakeLast(10).Sum();<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<th align=\"right\">\n        Allocated\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        SumLast10\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        11,935.5 ns\n      <\/td>\n<td align=\"right\">\n        102.793 ns\n      <\/td>\n<td align=\"right\">\n        85.837 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.1526\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        344 B\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        SumLast10\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        141.4 ns\n      <\/td>\n<td align=\"right\">\n        1.310 ns\n      <\/td>\n<td align=\"right\">\n        1.225 ns\n      <\/td>\n<td align=\"right\">\n        0.01\n      <\/td>\n<td align=\"right\">\n        0.0267\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        56 B\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 Just recently, PR\u00a0<\/p>\n<p><a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/37410\">dotnet\/corefx#37410<\/a>\u00a0optimized the relatively common pattern of using\u00a0<code>Enumerable.Range(...).Select(\u2026)<\/code>, teaching\u00a0<code>Select<\/code>\u00a0about the object generated by\u00a0<code>Range<\/code>\u00a0and allowing for the enumeration performed by\u00a0<code>Select<\/code>\u00a0to skip going through\u00a0<code>IEnumerable&lt;T&gt;<\/code>\u00a0and instead just loop through the intended numerical range directly.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">[Benchmark]\npublic int[] RangeSelectToArray() =&gt; Enumerable.Range(0, 100).Select(i =&gt; i * 2).ToArray();<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        RatioSD\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<th align=\"right\">\n        Allocated\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        RangeSelectToArray\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        953.9 ns\n      <\/td>\n<td align=\"right\">\n        20.232 ns\n      <\/td>\n<td align=\"right\">\n        28.363 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<td align=\"right\">\n        0.2460\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        520 B\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        RangeSelectToArray\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        358.0 ns\n      <\/td>\n<td align=\"right\">\n        7.650 ns\n      <\/td>\n<td align=\"right\">\n        7.156 ns\n      <\/td>\n<td align=\"right\">\n        0.37\n      <\/td>\n<td align=\"right\">\n        0.02\n      <\/td>\n<td align=\"right\">\n        0.2441\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        512 B\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0<\/p>\n<p><code>Enumerable.Empty&lt;T&gt;()<\/code>\u00a0was also changed in PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/31025\">dotnet\/corefx#31025<\/a>\u00a0to better compose with optimizations already elsewhere in .NET Core&#8217;s System.Linq implementation. While no one should be writing code that explicitly calls additional LINQ operators directly on the result of\u00a0<code>Enumerable.Empty&lt;T&gt;()<\/code>, it is common to return the result of\u00a0<code>Empty&lt;T&gt;()<\/code>\u00a0as one possible return value from an\u00a0<code>IEnumerable&lt;T&gt;<\/code>-returning method, and then for the caller to tack on additional operators, such that this optimization does actually have a meaningful effect.<\/p>\n<pre class=\"lang:default decode:true\">[Benchmark]\npublic int[] EmptyTakeSelectToArray() =&gt; Enumerable.Empty&lt;int&gt;().Take(10).Select(i =&gt; i).ToArray();<\/pre>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<th align=\"right\">\n        Allocated\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        EmptyTakeSelectToArray\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        71.80 ns\n      <\/td>\n<td align=\"right\">\n        1.4205 ns\n      <\/td>\n<td align=\"right\">\n        1.1861 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.0495\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        104 B\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        EmptyTakeSelectToArray\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        30.09 ns\n      <\/td>\n<td align=\"right\">\n        0.1550 ns\n      <\/td>\n<td align=\"right\">\n        0.1295 ns\n      <\/td>\n<td align=\"right\">\n        0.42\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 Across .NET Core, we&#8217;re also paying more attention to assembly size, in particular as it can impact ahead-of-time (AOT) compilation. PRs like\u00a0<\/p>\n<p><a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/35213\">dotnet\/corefx#35213<\/a>, which employs &#8220;ThrowHelpers&#8221; in the heavily-generic LINQ, help to reduce generated code size, which has benefits in and of itself but can also help with other areas of performance.<\/p>\n<h3>Interop<\/h3>\n<p>Interop is another one of those areas that&#8217;s critically important both to customers of .NET as well as to .NET itself, as a lot of functionality in .NET is layered on top of underlying operating system functionality that requires interop to access. As such, performance improvements in interop itself end up impacting a wide array of components. One notable improvement is in\u00a0<code>SafeHandle<\/code>, and it&#8217;s another example of where moving code from native to managed helped improve performance. SafeHandle is the recommended way for managing the lifetime of native resources, whether represented by handles on Windows or by file descriptors on Unix, and it&#8217;s used in exactly that way internally in all of our managed libraries in coreclr and corefx. One of the reasons it&#8217;s the recommended solution is that it uses appropriate synchronization to ensure that these native resources aren&#8217;t closed from managed code while they&#8217;re still being used, and that means that the interop layer needs to track every time a P\/Invoke call is made with a SafeHandle, invoking DangerousAddRef prior to the call, DangerousRelease after the call, and DangerousGetHandle to extract the actual pointer value to pass to the native function. In previous releases of .NET, the core pieces of those implementations were in the runtime, which meant managed code needed to make\u00a0<code>InternalCall<\/code>s to native code in the runtime for each of those operations. In .NET Core 3.0 as of PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/22564\">dotnet\/coreclr#22564<\/a>, those operations have been ported to managed code, removing the overhead associated with each of those transitions.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">private SafeFileHandle _sfh = new SafeFileHandle((IntPtr)12345, ownsHandle: false);\n\n[Benchmark]\npublic IntPtr SafeHandleOps()\n{\n    bool success = false;\n    try\n    {\n        _sfh.DangerousAddRef(ref success);\n        return _sfh.DangerousGetHandle();\n    }\n    finally\n    {\n        if (success)\n        {\n            _sfh.DangerousRelease();\n        }\n    }\n}<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        SafeHandleOps\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        36.72 ns\n      <\/td>\n<td align=\"right\">\n        0.7285 ns\n      <\/td>\n<td align=\"right\">\n        0.6458 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        SafeHandleOps\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        16.04 ns\n      <\/td>\n<td align=\"right\">\n        0.1322 ns\n      <\/td>\n<td align=\"right\">\n        0.1104 ns\n      <\/td>\n<td align=\"right\">\n        0.44\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 There are also examples for improvements to marshaling. Earlier in this post, I highlighted a variety of cases where\u00a0<\/p>\n<p><code>StringBuilder<\/code>\u00a0was used as part of marshaling and interop. For the record, I personally dislike\u00a0<code>StringBuilder<\/code>\u00a0being used in interop, as it adds cost and complexity for relatively little benefit, and as a result did work in PRs like\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/33780\">dotnet\/corefx#33780<\/a>\u00a0and\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/21120\">dotnet\/coreclr#21120<\/a>\u00a0to remove almost all use of\u00a0<code>StringBuilder<\/code>\u00a0marshaling in coreclr and corefx. However, there is still a lot of code built around\u00a0<code>StringBuilder<\/code>, and it deserves to be as fast as possible. PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/17928\">dotnet\/coreclr#17928<\/a>\u00a0avoids a bunch of unnecessary work and allocation that happens as part of\u00a0<code>StringBuilder<\/code>\u00a0marshaling, and leads to improvements like this:<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">private const int MAX_PATH = 260;\nprivate StringBuilder _sb = new StringBuilder(MAX_PATH);\n\n[DllImport(\"kernel32\", CharSet = CharSet.Unicode, SetLastError = true)]\nprivate static extern uint GetTempPathW(int bufferLen, [Out]StringBuilder buffer);\n\n[Benchmark]\npublic void StringBuilderMarshal() =&gt; GetTempPathW(MAX_PATH, _sb);<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        RatioSD\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<th align=\"right\">\n        Allocated\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        StringBuilderMarshal\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        359.4 ns\n      <\/td>\n<td align=\"right\">\n        7.643 ns\n      <\/td>\n<td align=\"right\">\n        13.386 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<td align=\"right\">\n        0.2584\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        544 B\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        StringBuilderMarshal\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        289.1 ns\n      <\/td>\n<td align=\"right\">\n        5.773 ns\n      <\/td>\n<td align=\"right\">\n        7.707 ns\n      <\/td>\n<td align=\"right\">\n        0.80\n      <\/td>\n<td align=\"right\">\n        0.04\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 And of course, specific uses of interop and marshaling have also improved. For example,\u00a0<\/p>\n<p><code>FileSystemWatcher<\/code>&#8216;s interop on macOS had been using\u00a0<code>MarshalAs<\/code>\u00a0attributes, which forced the runtime to do additional marshaling work on every OS callback, including allocating arrays. PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/34715\">dotnet\/corefx#34715<\/a>\u00a0moved\u00a0<code>FileSystemWatcher<\/code>&#8216;s interop to use a more efficient scheme that doesn&#8217;t entail additional allocations nor marshaling directives. Or consider\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/30099\">dotnet\/corefx#30099<\/a>, where\u00a0<code>System.Drawing<\/code>\u00a0was switched to using a much more efficient scheme of marshaling and interop, with a managed array being pinned and passed directly to native code instead of allocating additional memory and copying to it.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"wrap:false lang:default decode:true\">\/\/ Run with: dotnet run -c Release -f netcoreapp2.1 --filter *Program*\n\nprivate sealed class Config : ManualConfig \/\/ also add [Config(typeof(Config))] to the Program class\n{\n    public Config()\n    {\n        Add(Job.MediumRun.With(CsProjCoreToolchain.NetCoreApp21).WithNuGet(\"System.Drawing.Common\", \"4.5.1\").WithId(\"4.5.1\"));\n        Add(Job.MediumRun.With(CsProjCoreToolchain.NetCoreApp30).WithNuGet(\"System.Drawing.Common\", \"4.6.0-preview5.19224.8\").WithId(\"4.6.0-preview5.19224.8\"));\n    }\n}\n\nprivate Bitmap _image;\nprivate Graphics _graphics;\nprivate Point[] _points;\n\n[GlobalSetup]\npublic void Setup()\n{\n    _image = new Bitmap(100, 100);\n    _graphics = Graphics.FromImage(_image);\n    _points = new[]\n    {\n        new Point(10, 10), new Point(20, 1), new Point(35, 5), new Point(50, 10),\n        new Point(60, 15), new Point(65, 25), new Point(50, 30)\n    };\n}\n\n[Benchmark]\npublic void TransformPoints()\n{\n    _graphics.TransformPoints(CoordinateSpace.World, CoordinateSpace.Page, _points);\n    _graphics.TransformPoints(CoordinateSpace.Device, CoordinateSpace.World, _points);\n    _graphics.TransformPoints(CoordinateSpace.Page, CoordinateSpace.Device, _points);\n}<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Job\n      <\/th>\n<th>\n        NuGetReferences\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Gen 0\n      <\/th>\n<th align=\"right\">\n        Gen 1\n      <\/th>\n<th align=\"right\">\n        Gen 2\n      <\/th>\n<th align=\"right\">\n        Allocated\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        TransformPoints\n      <\/td>\n<td>\n        4.5.1\n      <\/td>\n<td>\n        System.Drawing.Common 4.5.1\n      <\/td>\n<td>\n        .NET Core 2.1\n      <\/td>\n<td align=\"right\">\n        11,010.3 ns\n      <\/td>\n<td align=\"right\">\n        490.050 ns\n      <\/td>\n<td align=\"right\">\n        718.309 ns\n      <\/td>\n<td align=\"right\">\n        0.5798\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        1248 B\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        TransformPoints\n      <\/td>\n<td>\n        4.6.0-preview5.19224.8\n      <\/td>\n<td>\n        System.Drawing.Common 4.6.0-preview5.19224.8\n      <\/td>\n<td>\n        .NET Core 3.0\n      <\/td>\n<td align=\"right\">\n        364.0 ns\n      <\/td>\n<td align=\"right\">\n        6.704 ns\n      <\/td>\n<td align=\"right\">\n        9.827 ns\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<td align=\"right\">\n        &#8211;\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0<\/p>\n<h3>Peanut butter<\/h3>\n<p>In previous sections of this post, I highlighted groups of PRs that addressed various areas of .NET in an impactful way, where some piece of mainstream functionality was significantly improved. But those aren&#8217;t the only areas or kinds of PRs that matter. In .NET we also have what we sometimes refer to as &#8220;peanut butter&#8221;. We have a ton of code that&#8217;s generally great for most applications but that has a myriad of small opportunities for improvements. Those improvements alone don&#8217;t make anything better, but they fix a smearing of performance penalties across a large swath of code, and the more of such issues we can fix, the better performance becomes overall. An allocation removed here, some unnecessary cycles eliminated there, some unnecessary code removed there. Here are just a sampling of PRs that went in to address such &#8220;peanut butter&#8221;:<\/p>\n<ul>\n<li><strong>Lower bounds explicitly provided to\u00a0<code>Array.Copy<\/code>.<\/strong>\u00a0Calling\u00a0<code>Array.Copy(src, dst, length)<\/code>\u00a0requires the runtime to call\u00a0<code>GetLowerBound<\/code>\u00a0on each of the src and the dst arrays. When working with\u00a0<code>T[]<\/code>s, the lower bound is 0, and we can just explicitly pass in 0 for both bounds and avoid the implicit\u00a0<code>GetLowerBound<\/code>\u00a0calls. PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/21756\">dotnet\/coreclr#21756<\/a>\u00a0does that in a variety of places.<\/li>\n<li><strong>Cheaper copying to new arrays.<\/strong>\u00a0In a variety of places, a\u00a0<code>List&lt;T&gt;<\/code>\u00a0stored some data, a new array was then allocated based on the length of the list, and the contents then copied to the array with\u00a0<code>CopyTo<\/code>. PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/22101\">dotnet\/coreclr#22101<\/a>\u00a0from @benaadams recognized the silliness of this and replaced that pattern with simply using\u00a0<code>List&lt;T&gt;.ToArray<\/code>.<\/li>\n<li><strong><code>Nullable&lt;T&gt;.Value<\/code>\u00a0vs\u00a0<code>GetValueOrDefault<\/code>.<\/strong>\u00a0<code>Nullable&lt;T&gt;<\/code>\u00a0has two main members to access the value:\u00a0<code>Value<\/code>\u00a0and\u00a0<code>GetValueOrDefault<\/code>. It&#8217;s initially counter-intuitive, but\u00a0<code>GetValueOrDefault<\/code>\u00a0is actually cheaper:\u00a0<code>Value<\/code>\u00a0needs to check whether the instance has a value or not, throwing if it doesn&#8217;t, whereas\u00a0<code>GetValueOrDefault<\/code>\u00a0just always returns the value field, and it&#8217;ll be\u00a0<code>default<\/code>\u00a0if there was no value. PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/22297\">dotnet\/coreclr#22297<\/a>\u00a0fixed up a variety of call sites where\u00a0<code>GetValueOrDefault<\/code>\u00a0could be used instead.<\/li>\n<li><strong><code>Array.Empty&lt;T&gt;()<\/code>.<\/strong>\u00a0In previous releases, lots of zero-length array allocations were changed to instead use\u00a0<code>Array.Empty&lt;T&gt;()<\/code>, both in libraries and via compiler changes for things like\u00a0<code>params<\/code>\u00a0arrays. That trend continues in .NET Core 3.0, with PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/30235\">dotnet\/corefx#30235<\/a>\u00a0doing another sweep through corefx and replacing even more zero-length allocations with the cached\u00a0<code>Array.Empty&lt;T&gt;()<\/code>.<\/li>\n<li><strong>Avoiding lots of little allocations all over the place.<\/strong>\u00a0For new code being written, we&#8217;re very cost-conscious and keep an eye out for allocations that, even if small and rare, could be easily replaced by something less expensive. For existing code, the most impactful allocations show up in profiling of key scenarios and are squashed whenever possible. But there are a lot of small allocations here and there that generally don&#8217;t pop up on our radar until we have another reason to review and profile the relevant code. In every release, we end up removing a bunch of these. For example, all of these PRs contributed to reducing the allocation peanut butter across coreclr and corefx in .NET Core 3.0: \n<ul>\n<li>In System.Collections:\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/30528\">dotnet\/corefx#30528<\/a><\/li>\n<li>In System.Data:\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/30130\">dotnet\/corefx#30130<\/a><\/li>\n<li>In System.Data.SqlClient:\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/34044\">dotnet\/corefx#34044<\/a>,\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/34047\">dotnet\/corefx#34047<\/a>,\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/34234\">dotnet\/corefx#34234<\/a>,\u00a0 <a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/34999\">dotnet\/corefx#34999<\/a>,\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/35549\">dotnet\/corefx#35549<\/a>,\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/34048\">dotnet\/corefx#34048<\/a>,\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/34390\">dotnet\/corefx#34390<\/a>, and\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/34393\">dotnet\/corefx#34393<\/a>, all from @Wraith2<\/li>\n<li>In System.Diagnostics:\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/21752\">dotnet\/coreclr#21752<\/a><\/li>\n<li>In System.IO: <a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/30509\">dotnet\/corefx#30509<\/a>,\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/30514\">dotnet\/corefx#30514<\/a>,\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/21760\">dotnet\/coreclr#21760<\/a>,\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/37546\">dotnet\/corefx#37546<\/a><\/li>\n<li>In System.Globalization: <a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/18546\">dotnet\/coreclr#18546<\/a>,\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/21121\">dotnet\/coreclr#21121<\/a><\/li>\n<li>In System.Net: <a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/30521\">dotnet\/corefx#30521<\/a>,\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/30530\">dotnet\/corefx#30530<\/a>, <a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/30508\">dotnet\/corefx#30508<\/a>,\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/30529\">dotnet\/corefx#30529<\/a>,\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/34356\">dotnet\/corefx#34356<\/a>,\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/36021\">dotnet\/corefx#36021<\/a><\/li>\n<li>In System.Reflection:\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/21770\">dotnet\/coreclr#21770<\/a>,\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/21758\">dotnet\/coreclr#21758<\/a><\/li>\n<li>In System.Security:\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/30512\">dotnet\/corefx#30512<\/a>, <a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/29612\">dotnet\/corefx#29612<\/a><\/li>\n<li>In System.Uri:\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/33641\">dotnet\/corefx#33641<\/a>,\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/36056\">dotnet\/corefx#36056<\/a><\/li>\n<li>In System.Xml: <a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/34196\">dotnet\/corefx#34196<\/a><\/li>\n<\/ul>\n<\/li>\n<li><strong>Avoiding explicit static cctors.<\/strong>\u00a0Any type that has static fields initialized ends up with a static constructor (cctor) to run that initialization. But depending on how the initialization is authored can impact performance. In particular, if the developer explicitly writes a static cctor rather than initializing the fields as part of the static field declarations, the C# compiler will not mark the type as\u00a0<code>beforefieldinit<\/code>. Having the type marked\u00a0<code>beforefieldinit<\/code>\u00a0can be beneficial for performance, because it allows the runtime more flexibility in when it performs the initialization, which in turn allows the JIT more flexibility about how it can optimize, and whether locking might be needed when accessing static methods on the type. PRs like\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/21718\">dotnet\/coreclr#21718<\/a>\u00a0and\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/21715\">dotnet\/coreclr#21715<\/a>\u00a0from @benaadams have removed such static cctors that can layer in small costs across a wide swath of accessing code.<\/li>\n<li><strong>Using a cheaper, sufficient equivalent.<\/strong>\u00a0<code>IndexOf<\/code>\u00a0on strings and spans returns the position of a found element, whereas\u00a0<code>Contains<\/code>\u00a0just returns whether the element was found. The latter can be slightly more efficient, because it doesn&#8217;t need to track the exact location of an element, just that it existed. Even so, lots of call sites that could have used\u00a0<code>Contains<\/code>\u00a0instead used\u00a0<code>IndexOf<\/code>. PRs\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/19874\">dotnet\/coreclr#19874<\/a>\u00a0and\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/32249\">dotnet\/corefx#32249<\/a>\u00a0by @grant-d addressed that. Another example,\u00a0<code>SocketsHttpHandler<\/code>(the default\u00a0<code>HttpMessageHandler<\/code>\u00a0behind\u00a0<code>HttpClient<\/code>) was using\u00a0<code>DateTime.UtcNow<\/code>\u00a0when determining whether a connection could be reused for the next request or not, but\u00a0<code>Environment.TickCount<\/code>\u00a0is cheaper and has sufficient resolution and accuracy for this purpose, so PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/35401\">dotnet\/corefx#35401<\/a>\u00a0switched it to use that. Another example, PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/37548\">dotnet\/corefx#37548<\/a>\u00a0tweaks the overloads of Array.Copy used in a bunch of places to avoid unnecessary\u00a0<code>GetLowerBound()<\/code>\u00a0calls to lookup the lower bound for arrays we know have a lower bound of 0.<\/li>\n<li><strong>Simplifying interop.<\/strong>\u00a0The interop infrastructure in .NET is quite powerful and comprehensive, with lots of knobs that allow for specifying how calls should be made and how data should be transformed. However, many come with a cost, such as needing the runtime to generate a marshaling stub to perform the various required transformations. PRs\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/36544\">dotnet\/corefx#36544<\/a>\u00a0and\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/36071\">dotnet\/corefx#36071<\/a>, for example, tweaked interop signatures to avoid overheads associated with such marshaling code.<\/li>\n<li><strong>Avoiding unnecessary globalization.<\/strong>\u00a0Due to how various\u00a0<code>System.String<\/code>\u00a0APIs were designed almost two decades ago, it can be easy to accidentally employ culture-aware string comparisons when it&#8217;s not intended. Such comparisons can be functionally incorrect for a given task and also more costly, involving more expensive calls to the operating system or globalization library. In particular,\u00a0<code>String.IndexOf<\/code>\u00a0with a\u00a0<code>char<\/code>\u00a0argument uses ordinal comparison, but\u00a0<code>String.IndexOf<\/code>\u00a0with a\u00a0<code>string<\/code>\u00a0argument (even if it&#8217;s a single character) uses the current culture to perform the comparison. PRs\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/37499\">dotnet\/corefx#37499<\/a>\u00a0addresses a bunch of such cases in\u00a0<code>System.Net<\/code>, an area in which one almost always wants to do ordinal comparisons, generally the case when doing parsing for text-based protocols.<\/li>\n<li><strong>Avoiding unnecessary\u00a0<code>ExecutionContext<\/code>\u00a0flow.<\/strong>\u00a0<code>ExecutionContext<\/code>\u00a0is the primary vehicle for ambient state &#8220;flowing&#8221; through a program and across asynchronous calls, in particular\u00a0<code>AsyncLocal&lt;T&gt;<\/code>. In order to achieve such flow, code that spawns an async operation (e.g.\u00a0<code>Task.Run<\/code>,\u00a0<code>Timer<\/code>, etc.) or code that creates a continuation to run when some other operation finishes (e.g.\u00a0<code>await<\/code>) needs to &#8220;capture&#8221; the current\u00a0<code>ExecutionContext<\/code>, hang on to it, and then later when executing the relevant work, use that captured\u00a0<code>ExecutionContext<\/code>&#8216;s\u00a0<code>Run<\/code>\u00a0method to do so. If the work being performed doesn&#8217;t actually require the\u00a0<code>ExecutionContext<\/code>, we can avoid flowing it to avoid the small associated overhead. PRs\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/37551\">dotnet\/corefx#37551<\/a>,\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/33235\">dotnet\/corefx#33235<\/a>, and\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/33080\">dotnet\/corefx#33080<\/a>\u00a0are examples: they switch several uses of\u00a0<code>CancellationToken.Register<\/code>over to the new\u00a0<code>CancellationToken.UnsafeRegister<\/code>\u00a0method, the only difference compared to\u00a0<code>Register<\/code>\u00a0being that it doesn&#8217;t flow\u00a0<code>ExecutionContext<\/code>. As another example, PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/18670\">dotnet\/coreclr#18670<\/a>\u00a0changed\u00a0<code>CancellationTokenSource<\/code>\u00a0so that when it creates a\u00a0<code>Timer<\/code>, it doesn&#8217;t unnecessarily capture\u00a0<code>ExecutionContext<\/code>. Or consider PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/20294\">dotnet\/coreclr#20294<\/a>, which ensures that any such captured\u00a0<code>ExecutionContext<\/code>\u00a0is dropped as soon as it&#8217;s not needed from completed\u00a0<code>Task<\/code>s.<\/li>\n<li><strong>Centralized \/ optimized bit operations.<\/strong>\u00a0PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/22118\">dotnet\/coreclr#22118<\/a>\u00a0from @benaadams introduced a\u00a0<code>BitOperations<\/code>\u00a0class that serves to centralize a bunch of bit-twiddling operations (rotating, leading zero count, population count, log, etc.). This type was later augmented and enhanced in PRs from @grant-d like\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/22497\">dotnet\/coreclr#22497<\/a>,\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/22584\">dotnet\/coreclr#22584<\/a>, and\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/22630\">dotnet\/coreclr#22630<\/a>, which also serve to use these shared helpers from everywhere across\u00a0<code>System.Private.Corelib<\/code>\u00a0where such bit-twiddling operations are required. This ensures that all such call sites (of which there are currently ~70) get the best implementation the runtime can muster, whether that be an implementation that takes advantage of the current hardware&#8217;s instruction set or one that utilizes a software fallback.<\/li>\n<\/ul>\n<h3>GC<\/h3>\n<p>No blog post on performance would be complete without discussing the garbage collector. Many of the improvements cited thus far have involved reducing allocations, which is in part about reducing direct costs but more so about reducing the load placed on the garbage collector and minimizing the work it needs to do. But improving the GC itself is also a key focus, and one that&#8217;s gotten attention in this release, as it has in previous releases. PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/21523\">dotnet\/coreclr#21523<\/a>\u00a0includes a variety of performance improvements, from improvements to locking to better free list management. PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/23251\">dotnet\/coreclr#23251<\/a>\u00a0from @mjsabby adds support to the GC for Large Pages (&#8220;Huge Pages&#8221; on Linux), which can be opted-into by very large applications that experience bottlenecks due to the translation lookaside buffer (TLB). And PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/22003\">dotnet\/coreclr#22003<\/a>\u00a0further optimized the write barriers employed by the GC. One notable piece of work is improving behavior on machines with a large number of processors, e.g. PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/23824\">dotnet\/coreclr#23824<\/a>. Rather than trying to explain it here, I&#8217;ll simply refer to @Maoni0&#8217;s blog post on the subject:\u00a0<a href=\"https:\/\/blogs.msdn.microsoft.com\/maoni\/2019\/04\/03\/making-cpu-configuration-better-for-gc-on-machines-with-64-cpus\/\" rel=\"nofollow\">https:\/\/blogs.msdn.microsoft.com\/maoni\/2019\/04\/03\/making-cpu-configuration-better-for-gc-on-machines-with-64-cpus\/<\/a>. Similarly, a lot of work has gone into the release to improve the behavior and performance of the GC when operating in a containerized environment (and in particular in one that&#8217;s heavily constrained), such as in PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/22180\">dotnet\/coreclr#22180<\/a>. Again, @Maoni0 can do a much better job than I can describing this work, and you can read all about it her two blog posts,\u00a0<a href=\"https:\/\/blogs.msdn.microsoft.com\/maoni\/2018\/11\/16\/running-with-server-gc-in-a-small-container-scenario-part-0\/\" rel=\"nofollow\">running-with-server-gc-in-a-small-container-scenario-part-0<\/a>\u00a0and\u00a0<a href=\"https:\/\/blogs.msdn.microsoft.com\/maoni\/2019\/02\/04\/running-with-server-gc-in-a-small-container-scenario-part-1-hard-limit-for-the-gc-heap\/\" rel=\"nofollow\">running-with-server-gc-in-a-small-container-scenario-part-1-hard-limit-for-the-gc-heap<\/a>.<\/p>\n<h3>JIT<\/h3>\n<p>A lot of goodness has gone into the just-in-time (JIT) compiler in .NET Core 3.0. One of the most impactful changes is tiered compilation (this is split across many PRs, but for example PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/23599\">dotnet\/coreclr#23599<\/a>). Tiered compilation is a solution for the problem that very good compilation from MSIL to native code takes time; the more analysis to be done, the more optimizations to be applied, the longer it takes. But with a JIT compiler that does that code generation at runtime, that time comes at the direct expense of application start-up, and so you&#8217;re left with a trade-off: do you spend more time generating better code but take longer to get going, or do you spend less time generating less-good code but get going faster? Tiered compilation is a scheme for accomplishing both. The idea is that methods are first compiled with a fast pass that applies few-to-no optimizations but that completes very quickly, and then as methods are seen to execute again and again, those methods are re-JIT&#8217;d, this time with more time spent on code quality. Interestingly, though, tiered compilation isn&#8217;t just about start-up time. There are optimizations that the re-compilation can take advantage of that weren&#8217;t available the first time around. For example, tiered compilation can apply to ready-to-run (R2R) images, a form of precompilation employed by assemblies in the .NET Core shared framework. These assemblies contain precompiled native code, but in some ways the optimizations that can be applied during that native code generation are limited in order to aid in version resiliency, e.g. cross-module inlining doesn&#8217;t happen with R2R. So, the R2R code can help enable faster start-up, but then methods found to be used frequently can be re-compiled via tiered compilation, thereby taking advantage of such optimizations the original precompiled code was restricted from using. Here&#8217;s an example of that. First, we can run the following benchmark.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"wrap:false lang:default decode:true\">private XmlDocument _doc = new XmlDocument();\n\n[Benchmark]\npublic void LoadXml()\n{\n    _doc.RemoveAll();\n    _doc.LoadXml(\"&lt;Root&gt;&lt;Element attrib=\\\"foo\\\" attrib2=\\\"foo2\\\"&gt;foo&lt;\/Element&gt;&lt;Element attrib=\\\"foo\\\" attrib2=\\\"foo2\\\"&gt;foo&lt;\/Element&gt;&lt;Element attrib=\\\"foo\\\" attrib2=\\\"foo2\\\"&gt;foo&lt;\/Element&gt;&lt;Element attrib=\\\"foo\\\" attrib2=\\\"foo2\\\"&gt;foo&lt;\/Element&gt;&lt;Element attrib=\\\"foo\\\" attrib2=\\\"foo2\\\"&gt;foo&lt;\/Element&gt;&lt;Element attrib=\\\"foo\\\" attrib2=\\\"foo2\\\"&gt;foo&lt;\/Element&gt;&lt;Element attrib=\\\"foo\\\" attrib2=\\\"foo2\\\"&gt;foo&lt;\/Element&gt;&lt;Element attrib=\\\"foo\\\" attrib2=\\\"foo2\\\"&gt;foo&lt;\/Element&gt;&lt;\/Root&gt;\");\n}<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        LoadXml\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        9.576 us\n      <\/td>\n<td align=\"right\">\n        0.1523 us\n      <\/td>\n<td align=\"right\">\n        0.1425 us\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        LoadXml\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        7.414 us\n      <\/td>\n<td align=\"right\">\n        0.0980 us\n      <\/td>\n<td align=\"right\">\n        0.0868 us\n      <\/td>\n<td align=\"right\">\n        0.78\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 Then, we can run it again, but this time with tiered compilation disabled by setting the\u00a0<\/p>\n<p><code>COMPlus_TieredCompilation<\/code>environment variable to 0.<\/p>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<th align=\"right\">\n        RatioSD\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        LoadXml\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        9.650 us\n      <\/td>\n<td align=\"right\">\n        0.1638 us\n      <\/td>\n<td align=\"right\">\n        0.1279 us\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<td align=\"right\">\n        0.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        LoadXml\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        9.002 us\n      <\/td>\n<td align=\"right\">\n        0.2018 us\n      <\/td>\n<td align=\"right\">\n        0.2073 us\n      <\/td>\n<td align=\"right\">\n        0.93\n      <\/td>\n<td align=\"right\">\n        0.03\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 There are a variety of environment variables that configure tiered compilation and in what situations it&#8217;s enabled. For more details, see\u00a0<\/p>\n<p><a href=\"https:\/\/github.com\/dotnet\/coreclr\/issues\/24064\">https:\/\/github.com\/dotnet\/coreclr\/issues\/24064<\/a>. Another really cool improvement in the JIT comes in PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/20886\">dotnet\/coreclr#20886<\/a>. In previous releases of .NET, the JIT could optimize the usage of some primitive type\u00a0<code>static readonly<\/code>\u00a0fields as if they were constants. For example, if a\u00a0<code>static readonly int<\/code>\u00a0field were initialized to the value\u00a0<code>42<\/code>\u00a0by the time some code that used that field was JIT compiled, the JIT compiler would effectively treat that field instead as a\u00a0<code>const<\/code>, and do constant folding and all other forms of optimizations that would otherwise apply. In .NET Core 3.0, the JIT can now utilize the type of\u00a0<code>static readonly<\/code>fields to do additional optimizations. For example, if a\u00a0<code>static readonly<\/code>\u00a0field is typed as a base type but is then set to a derived type, the JIT might be able to see the actual type of the object stored in the field, and then when a virtual method is called on it, devirtualize the call and even potentially inline it.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">private static readonly Base s_base;\n\nstatic Program() =&gt; s_base = new Derived();\n\n[Benchmark]\npublic void AccessStatic() =&gt; s_base.Method();\n\nprivate sealed class Derived : Base { public override void Method() { } }\nprivate abstract class Base { public abstract void Method(); }<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Median\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        AccessStatic\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        0.5625 ns\n      <\/td>\n<td align=\"right\">\n        0.0147 ns\n      <\/td>\n<td align=\"right\">\n        0.0130 ns\n      <\/td>\n<td align=\"right\">\n        0.5616 ns\n      <\/td>\n<td align=\"right\">\n        1.000\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        AccessStatic\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        0.0015 ns\n      <\/td>\n<td align=\"right\">\n        0.0060 ns\n      <\/td>\n<td align=\"right\">\n        0.0062 ns\n      <\/td>\n<td align=\"right\">\n        0.0000 ns\n      <\/td>\n<td align=\"right\">\n        0.003\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 That highlights some improvements that have gone into devirtualization, but there are others, such as in PRs\u00a0<\/p>\n<p><a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/20447\">dotnet\/coreclr#20447<\/a>,\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/20292\">dotnet\/coreclr#20292<\/a>, and\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/20640\">dotnet\/coreclr#20640<\/a>\u00a0which, when combined with PRs like\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/20637\">dotnet\/coreclr#20637<\/a>\u00a0from @benaadams, help with APIs like\u00a0<code>ArrayPool&lt;T&gt;.Shared&lt;span style=\"color: #52595e; font-family: Arimo, Helvetica Neue, Arial, sans-serif;\"&gt;&lt;span style=\"font-size: 16px; background-color: #f7f7f9;\"&gt;.&lt;\/span&gt;&lt;\/span&gt;<\/code><\/p>\n<pre class=\"lang:default decode:true\">[Benchmark]\npublic void RentReturn() =&gt; ArrayPool&lt;byte&gt;.Shared.Return(ArrayPool&lt;byte&gt;.Shared.Rent(256));<\/pre>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        RentReturn\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        32.92 ns\n      <\/td>\n<td align=\"right\">\n        0.3357 ns\n      <\/td>\n<td align=\"right\">\n        0.2803 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        RentReturn\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        25.74 ns\n      <\/td>\n<td align=\"right\">\n        0.2392 ns\n      <\/td>\n<td align=\"right\">\n        0.1867 ns\n      <\/td>\n<td align=\"right\">\n        0.78\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 Another nice improvement is around zeroing of locals. Even when the\u00a0<\/p>\n<p><code>initlocals<\/code>\u00a0flag isn&#8217;t set (as of PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/corefx\/pull\/34406\">dotnet\/corefx#34406<\/a>, it&#8217;s cleared for all assemblies in coreclr and corefx), the JIT still needs to zero out references in locals so that the GC doesn&#8217;t see and misinterpret garbage, and that zero&#8217;ing can take a measurable amount of time, in particular in methods that do a lot of work with spans. PRs\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/23498\">dotnet\/coreclr#23498<\/a>\u00a0and\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/13868\">dotnet\/coreclr#13868<\/a>\u00a0make some nice improvements in this area.<\/p>\n<div class=\"highlight highlight-source-cs\">\n<pre class=\"lang:default decode:true\">private byte[] _bytes = new byte[1];\n\n[Benchmark]\npublic void StackZero()\n{\n    Span&lt;byte&gt; a, b;\n    a = _bytes;\n    b = _bytes;\n    Nop(a, b);\n}\n\n[MethodImpl(MethodImplOptions.NoInlining)]\nprivate void Nop(Span&lt;byte&gt; a, Span&lt;byte&gt; b) { }<\/pre>\n<\/div>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        StackZero\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        8.948 ns\n      <\/td>\n<td align=\"right\">\n        0.2479 ns\n      <\/td>\n<td align=\"right\">\n        0.2546 ns\n      <\/td>\n<td align=\"right\">\n        1.00\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        StackZero\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        2.389 ns\n      <\/td>\n<td align=\"right\">\n        0.0740 ns\n      <\/td>\n<td align=\"right\">\n        0.0727 ns\n      <\/td>\n<td align=\"right\">\n        0.27\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0 Another example relates to structs. As more and more recognition has come to .NET performance, in particular around allocation, there&#8217;s been a significant increase in the use of value types, often wrapping one another. For example, awaiting a\u00a0<\/p>\n<p><code>ValueTask&lt;T&gt;<\/code>\u00a0results in calling\u00a0<code>GetAwaiter()<\/code>\u00a0on that value task, and that returns a\u00a0<code>ValueTaskAwaiter&lt;T&gt;<\/code>\u00a0that wraps the\u00a0<code>ValueTask&lt;T&gt;<\/code>. PR\u00a0<a href=\"https:\/\/github.com\/dotnet\/coreclr\/pull\/19429\">dotnet\/coreclr#19429<\/a>\u00a0improves the situation by removing unnecessary copies involved in these operations.<\/p>\n<pre class=\"lang:default decode:true\">[Benchmark]\npublic int WrapUnwrap() =&gt; ValueTuple.Create(ValueTuple.Create(ValueTuple.Create(42))).Item1.Item1.Item1;<\/pre>\n<table style=\"border-style: solid;\" border=\"1\">\n<thead>\n<tr>\n<th>\n        Method\n      <\/th>\n<th>\n        Toolchain\n      <\/th>\n<th align=\"right\">\n        Mean\n      <\/th>\n<th align=\"right\">\n        Error\n      <\/th>\n<th align=\"right\">\n        StdDev\n      <\/th>\n<th align=\"right\">\n        Median\n      <\/th>\n<th align=\"right\">\n        Ratio\n      <\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n        WrapUnwrap\n      <\/td>\n<td>\n        netcoreapp2.1\n      <\/td>\n<td align=\"right\">\n        1.2198 ns\n      <\/td>\n<td align=\"right\">\n        0.0717 ns\n      <\/td>\n<td align=\"right\">\n        0.0599 ns\n      <\/td>\n<td align=\"right\">\n        1.2095 ns\n      <\/td>\n<td align=\"right\">\n        1.000\n      <\/td>\n<\/tr>\n<tr>\n<td>\n        WrapUnwrap\n      <\/td>\n<td>\n        netcoreapp3.0\n      <\/td>\n<td align=\"right\">\n        0.0002 ns\n      <\/td>\n<td align=\"right\">\n        0.0007 ns\n      <\/td>\n<td align=\"right\">\n        0.0006 ns\n      <\/td>\n<td align=\"right\">\n        0.0000 ns\n      <\/td>\n<td align=\"right\">\n        0.000\n      <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0<\/p>\n<h3>What&#8217;s Next?<\/h3>\n<p>As I write this post, I count 29 pending performance-focused PRs in the coreclr repo and another 8\u00a0 in the corefx repo. Some of those are likely to be merged in time for the .NET Core 3.0 release, as will, I&#8217;m sure, additional PRs that haven&#8217;t even been opened yet. In short, even after all of the improvements detailed in for\u00a0<a href=\"https:\/\/blogs.msdn.microsoft.com\/dotnet\/2017\/06\/07\/performance-improvements-in-net-core\/\" rel=\"nofollow\">.NET Core 2.0<\/a>,\u00a0<a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/performance-improvements-in-net-core-2-1\" rel=\"nofollow\">.NET Core 2.1<\/a>, and now in this post for .NET Core 3.0, and even with all of those improvements contributing to ASP.NET Core being one of the fastest web servers on the planet, there is still incredible opportunity for performance to keep getting better and better, and for you to help achieve that. Hopefully this post has made you excited about the potential .NET Core 3.0 holds. I look forward to reviewing your PRs as we all contribute to this exciting future together!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Take a tour through some of the many improvements, big and small, that have gone into the .NET Core 3.0 runtime and core libraries to make apps and services leaner and faster.<\/p>\n","protected":false},"author":360,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[685,196,756,3009],"tags":[8082],"class_list":["post-23254","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dotnet","category-dotnet-core","category-csharp","category-performance","tag-dotnetperf"],"acf":[],"blog_post_summary":"<p>Take a tour through some of the many improvements, big and small, that have gone into the .NET Core 3.0 runtime and core libraries to make apps and services leaner and faster.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/23254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/360"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=23254"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/23254\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=23254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=23254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=23254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}