-
-
Notifications
You must be signed in to change notification settings - Fork 672
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1746 from pCYSl5EDgo/net8-small-step
.NET 8 Update(8): Update Benchmarks not using Dynamic PGO and BinaryFormatter
- Loading branch information
Showing
14 changed files
with
514 additions
and
551 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright (c) All contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
extern alias e; | ||
|
||
namespace Benchmark; | ||
|
||
public class BoolDeserializeTest | ||
{ | ||
#pragma warning disable SA1117 | ||
[Params( | ||
"0", | ||
"1 true", "1 false", "1 rand", | ||
"3 true", "3 false", "3 rand", | ||
"8 rand", | ||
"16 rand", | ||
"31 rand", | ||
"64 rand", | ||
"4096 rand", | ||
"4194304 rand")] | ||
public string Setting { get; set; } = string.Empty; | ||
#pragma warning restore SA1117 | ||
|
||
private byte[] binary = []; | ||
|
||
[GlobalSetup] | ||
public void SetUp() | ||
{ | ||
var span = Setting.AsSpan(); | ||
var firstSpace = span.IndexOf(' '); | ||
var sizeSpan = span; | ||
if (firstSpace >= 0) | ||
{ | ||
sizeSpan = sizeSpan[..firstSpace]; | ||
} | ||
|
||
var size = int.Parse(sizeSpan); | ||
var input = size == 0 ? [] : new bool[size]; | ||
if (input.Length != 0) | ||
{ | ||
span = span[(firstSpace + 1)..]; | ||
switch (span) | ||
{ | ||
case "true": | ||
Array.Fill(input, true); | ||
break; | ||
case "false": | ||
Array.Fill(input, false); | ||
break; | ||
default: | ||
foreach (ref var item in input.AsSpan()) | ||
{ | ||
item = (Random.Shared.Next() & 1) == 0; | ||
} | ||
|
||
break; | ||
} | ||
} | ||
|
||
binary = MessagePackSerializer.Serialize(input); | ||
} | ||
|
||
[Benchmark(Baseline = true)] | ||
public bool[]? Old() | ||
{ | ||
MessagePackReader reader = new(binary); | ||
return BooleanArrayFormatter.Instance.Deserialize(ref reader, default!); | ||
} | ||
|
||
[Benchmark] | ||
public bool[]? Simd() | ||
{ | ||
MessagePackReader reader = new(binary); | ||
return e::MessagePack.Formatters.BooleanArrayFormatter.Instance.Deserialize(ref reader, default!); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright (c) All contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
extern alias e; | ||
|
||
namespace Benchmark; | ||
|
||
public class BoolSerializeTest | ||
{ | ||
#pragma warning disable SA1117 | ||
[Params( | ||
"0", | ||
"1 true", "1 false", "1 rand", | ||
"3 true", "3 false", "3 rand", | ||
"8 rand", | ||
"16 rand", | ||
"31 rand", | ||
"64 rand", | ||
"4096 rand", | ||
"4194304 rand")] | ||
public string Setting { get; set; } = string.Empty; | ||
#pragma warning restore SA1117 | ||
|
||
private bool[] input = []; | ||
|
||
[GlobalSetup] | ||
public void SetUp() | ||
{ | ||
var span = Setting.AsSpan(); | ||
var firstSpace = span.IndexOf(' '); | ||
var sizeSpan = span; | ||
if (firstSpace >= 0) | ||
{ | ||
sizeSpan = sizeSpan[..firstSpace]; | ||
} | ||
|
||
var size = int.Parse(sizeSpan); | ||
input = size == 0 ? [] : new bool[size]; | ||
if (input.Length == 0) | ||
{ | ||
return; | ||
} | ||
|
||
span = span[(firstSpace + 1)..]; | ||
switch (span) | ||
{ | ||
case "true": | ||
Array.Fill(input, true); | ||
break; | ||
case "false": | ||
Array.Fill(input, false); | ||
break; | ||
default: | ||
foreach (ref var item in input.AsSpan()) | ||
{ | ||
item = (Random.Shared.Next() & 1) == 0; | ||
} | ||
|
||
break; | ||
} | ||
} | ||
|
||
[Benchmark(Baseline = true)] | ||
public ReadOnlyMemory<byte> Old() | ||
{ | ||
ArrayBufferWriter<byte> bufferWriter = new(); | ||
MessagePackWriter writer = new(bufferWriter); | ||
BooleanArrayFormatter.Instance.Serialize(ref writer, input, default!); | ||
writer.Flush(); | ||
return bufferWriter.WrittenMemory; | ||
} | ||
|
||
[Benchmark] | ||
public ReadOnlyMemory<byte> Simd() | ||
{ | ||
ArrayBufferWriter<byte> bufferWriter = new(); | ||
MessagePackWriter writer = new(bufferWriter); | ||
e::MessagePack.Formatters.BooleanArrayFormatter.Instance.Serialize(ref writer, input, default!); | ||
writer.Flush(); | ||
return bufferWriter.WrittenMemory; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright (c) All contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
extern alias e; | ||
|
||
namespace Benchmark; | ||
|
||
public class DoubleTest | ||
{ | ||
[Params(0, 1, 3, 8, 31, 64, 1024, 16777216)] | ||
public int Size { get; set; } | ||
|
||
private double[] input = []; | ||
|
||
[GlobalSetup] | ||
public void SetUp() | ||
{ | ||
input = new double[Size]; | ||
for (var i = 0; i < input.Length; i++) | ||
{ | ||
input[i] = Random.Shared.NextDouble(); | ||
} | ||
} | ||
|
||
[Benchmark(Baseline = true)] | ||
public ReadOnlyMemory<byte> Old() | ||
{ | ||
ArrayBufferWriter<byte> bufferWriter = new(); | ||
MessagePackWriter writer = new(bufferWriter); | ||
DoubleArrayFormatter.Instance.Serialize(ref writer, input, default!); | ||
writer.Flush(); | ||
return bufferWriter.WrittenMemory; | ||
} | ||
|
||
[Benchmark] | ||
public ReadOnlyMemory<byte> Simd() | ||
{ | ||
ArrayBufferWriter<byte> bufferWriter = new(); | ||
MessagePackWriter writer = new(bufferWriter); | ||
e::MessagePack.Formatters.DoubleArrayFormatter.Instance.Serialize(ref writer, input, default!); | ||
writer.Flush(); | ||
return bufferWriter.WrittenMemory; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright (c) All contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
extern alias e; | ||
|
||
namespace Benchmark; | ||
|
||
public class IntTest | ||
{ | ||
#pragma warning disable SA1117 | ||
[Params( | ||
"0", | ||
"1 rand", "1 0", | ||
"3 rand", "3 0", | ||
"8 rand", "8 0", | ||
"16 rand", "16 0", | ||
"31 rand", "31 0", | ||
"64 rand", "64 0", | ||
"4096 rand", "4096 0", | ||
"4194304 rand", "4194304 0")] | ||
public string Setting { get; set; } = string.Empty; | ||
#pragma warning restore SA1117 | ||
|
||
private int[] input = []; | ||
|
||
[GlobalSetup] | ||
public void SetUp() | ||
{ | ||
var span = Setting.AsSpan(); | ||
var firstSpace = span.IndexOf(' '); | ||
var sizeSpan = span; | ||
if (firstSpace >= 0) | ||
{ | ||
sizeSpan = sizeSpan[..firstSpace]; | ||
} | ||
|
||
var size = int.Parse(sizeSpan); | ||
input = size == 0 ? [] : new int[size]; | ||
if (input.Length == 0) | ||
{ | ||
return; | ||
} | ||
|
||
span = span[(firstSpace + 1)..]; | ||
switch (span) | ||
{ | ||
case "rand": | ||
Random.Shared.NextBytes(MemoryMarshal.AsBytes(input.AsSpan())); | ||
break; | ||
default: | ||
Array.Fill(input, int.Parse(span)); | ||
break; | ||
} | ||
} | ||
|
||
[Benchmark(Baseline = true)] | ||
public ReadOnlyMemory<byte> Old() | ||
{ | ||
ArrayBufferWriter<byte> bufferWriter = new(); | ||
MessagePackWriter writer = new(bufferWriter); | ||
Int32ArrayFormatter.Instance.Serialize(ref writer, input, default!); | ||
writer.Flush(); | ||
return bufferWriter.WrittenMemory; | ||
} | ||
|
||
[Benchmark] | ||
public ReadOnlyMemory<byte> Simd() | ||
{ | ||
ArrayBufferWriter<byte> bufferWriter = new(); | ||
MessagePackWriter writer = new(bufferWriter); | ||
e::MessagePack.Formatters.Int32ArrayFormatter.Instance.Serialize(ref writer, input, default!); | ||
writer.Flush(); | ||
return bufferWriter.WrittenMemory; | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,41 @@ | ||
// Copyright (c) All contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Benchmark; | ||
#if NET7_0_OR_GREATER | ||
using System.Runtime.Intrinsics; | ||
#else | ||
using System.Runtime.Intrinsics.Arm; | ||
using System.Runtime.Intrinsics.X86; | ||
#endif | ||
using BenchmarkDotNet.Columns; | ||
using BenchmarkDotNet.Configs; | ||
using BenchmarkDotNet.Jobs; | ||
using BenchmarkDotNet.Running; | ||
|
||
namespace HardwareIntrinsicsBenchmark | ||
namespace Benchmark; | ||
|
||
internal class Program | ||
{ | ||
internal class Program | ||
private static void Main(string[] args) | ||
{ | ||
private static void Main() | ||
var noDynamicPGO = new EnvironmentVariable("DOTNET_TieredPGO", "0"); | ||
IConfig config = DefaultConfig.Instance | ||
.HideColumns(Column.EnvironmentVariables, Column.RatioSD, Column.Error) | ||
.AddJob(Job.Default.WithEnvironmentVariables([ | ||
new("DOTNET_EnableHWIntrinsic", "0"), | ||
noDynamicPGO | ||
]).WithId("Scalar").AsBaseline()); | ||
|
||
#if NET7_0_OR_GREATER | ||
if (Vector128.IsHardwareAccelerated) | ||
#else | ||
if (Sse42.IsSupported || AdvSimd.IsSupported) | ||
#endif | ||
{ | ||
BenchmarkRunner.Run<UnsafeUnmanagedStructArrayBenchmark>(); | ||
BenchmarkRunner.Run<BooleanArrayBenchmarkMessagePackNoSingleInstructionMultipleDataVsMessagePackSingleInstructionMultipleData>(); | ||
BenchmarkRunner.Run<Int8ArrayBenchmarkMessagePackNoSingleInstructionMultipleDataVsMessagePackSingleInstructionMultipleData>(); | ||
BenchmarkRunner.Run<Int16ArrayBenchmarkMessagePackNoSingleInstructionMultipleDataVsMessagePackSingleInstructionMultipleData>(); | ||
BenchmarkRunner.Run<Int32ArrayBenchmarkMessagePackNoSingleInstructionMultipleDataVsMessagePackSingleInstructionMultipleData>(); | ||
BenchmarkRunner.Run<SingleArrayBenchmarkMessagePackNoSingleInstructionMultipleDataVsMessagePackSingleInstructionMultipleData>(); | ||
BenchmarkRunner.Run<DoubleArrayBenchmarkMessagePackNoSingleInstructionMultipleDataVsMessagePackSingleInstructionMultipleData>(); | ||
config = config.AddJob(Job.Default.WithEnvironmentVariable(noDynamicPGO).WithId("Vector")); | ||
} | ||
|
||
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly) | ||
.Run(args, config); | ||
} | ||
} |
Oops, something went wrong.