.Net 6 性能改进系列-前言

起因

本来这篇是准备翻译这篇博客的,地址为:  Announcing .NET 6 Preview 1,愣生生的拖到了这篇博客出来Performance Improvements in .NET 6 ,托了差不多5个月.也是厉害了.
本来只是想翻译一篇文章,但看到.Net 6性能改进这篇文章,我的头有点大,内容太多了,英语还是处于记单词的状态.

顺便说了写这篇博文的时间,为2021年8月20日,因为疫情的原因,我已经在家办公两周了.两周的时间出了小区买了两次菜,连下楼的次数一只手都数的过来.

考虑这篇文章内容真的太多了,改进的地方也太多了,文章中加了很多.Net 源码的issue(议题).所以准备拆分成一个系列.

从下文开始,我就不是我了,而是Performance Improvements in .NET 6这篇文章的作者了.

进入正题

4年前,在.Net Core 2.0发布后,我写了 .Net Core性能改进 内容主要是关于.Net Core在性能方面的改进.这篇文章的反馈是很好的.

大概是在1年后,.Net Core 2.1发布的时候,我写了 在.Net Core 2.1性能改进 .

后边到了.Net Core 3.0发布的时候,我写了 .Net Core 3.0 性能改进,就这样好像变成了传统,一年一度.

在.Net 5发布后,同样如此.于是有了 Performance Improvements in .NET 5(原文) ,.Net 5这篇文章我进行了简单的翻译地址为: .Net 5 性能改进 

直到今天(2021.08.19),我写了这边文章 Performance Improvements in .NET 6

.Net源码仓库 ,从主分支迁出到.Net 6上一年的时间,有超过6500次提交合并请求,这还是排除自动化的和并请求.不过这篇文章关于性能改进的大概在400次提交.

性能改进分类

.Net 6 性能改进,进行归类,改进的地方真的太多了,大到即时编译和垃圾回收,小道Linq的性能优化

准备工作

创建一个性能测试的项目:
#创建性能测试的项目
dotnet new console -o net6perf
#使用BenchmarkDotNet进行性能测试
dotnet add package benchmarkdotnet

修改项目工程文件:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <!--测试版本分别.Net Framework 4.8 /.Net 5和.Net 6-->
    <TargetFrameworks>net48;net5.0;net6.0</TargetFrameworks>
    <Nullable>annotations</Nullable>
    <!--开始不安全标志,操作指针的时候,需要卡其-->
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
    <!--指定c#语言语言 这里为C# 10-->
    <LangVersion>10</LangVersion>
    <!--GC的模式为服务器模式-->
    <ServerGarbageCollection>true</ServerGarbageCollection>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="benchmarkdotnet" Version="0.13.1" />
  </ItemGroup>

  <ItemGroup Condition=" '$(TargetFramework)' == 'net48' ">
    <Reference Include="System.Net.Http" />
  </ItemGroup>

</Project>

使用BenchmarkDotNet配置:

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Order;
using Perfolizer.Horology;
using System;
using System.Buffers;
using System.Buffers.Binary;
using System.Buffers.Text;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics;
using System.Diagnostics.Tracing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Net.WebSockets;
using System.Numerics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.IO.Compression;
#if NETCOREAPP3_0_OR_GREATER
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
#endif

[DisassemblyDiagnoser(maxDepth: 1)] // change to 0 for just the [Benchmark] method
[MemoryDiagnoser(displayGenColumns: false)]
public class Program
{
    public static void Main(string[] args) =>
        BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, DefaultConfig.Instance
            //.WithSummaryStyle(new SummaryStyle(CultureInfo.InvariantCulture, printUnitsInHeader: false, SizeUnit.B, TimeUnit.Microsecond))
            );

    // BENCHMARKS GO HERE
}
#执行命令
dotnet run -c Release -f net48 -filter "**" --runtimes net48 net50 net60


秋风 2021-03-14