Add benchmark for BAT swapping.

This commit is contained in:
2025-08-08 21:05:08 +01:00
parent a2fb3cabdf
commit dea5949035
2 changed files with 110 additions and 4 deletions

View File

@@ -0,0 +1,99 @@
#if NET10_0_OR_GREATER
using System;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
namespace AaruBenchmark.Algorithms;
[SimpleJob(RuntimeMoniker.Net10_0)]
[SimpleJob(RuntimeMoniker.NativeAot10_0)]
[MemoryDiagnoser(false)]
[HideColumns("Job", "Error", "StdDev", "Median", "RatioSD", "Alloc Ratio")]
public class BatSwapping
{
byte[] _batData;
[GlobalSetup]
public void Setup()
{
_batData = new byte[1024 * 1024]; // 1 MB
new Random(42).NextBytes(_batData); // Fill with deterministic random bytes
}
[Benchmark(Baseline = true)]
public void Loop()
{
ReadOnlySpan<byte> span = _batData;
uint[] blockAllocationTable =
MemoryMarshal.Cast<byte, uint>(span)[..(_batData.Length / sizeof(uint))].ToArray();
uint test = Swap(blockAllocationTable[1024]);
// Swap all uint endianness
for(int i = 0; i < blockAllocationTable.Length; i++) blockAllocationTable[i] = Swap(blockAllocationTable[i]);
if(test != blockAllocationTable[1024]) throw new Exception("Swapping failed");
}
[Benchmark]
public void ParallelLoop()
{
ReadOnlySpan<byte> span = _batData;
uint[] blockAllocationTable =
MemoryMarshal.Cast<byte, uint>(span)[..(_batData.Length / sizeof(uint))].ToArray();
uint test = Swap(blockAllocationTable[1024]);
// Swap all uint endianness
Parallel.For(0, blockAllocationTable.Length, i => { blockAllocationTable[i] = Swap(blockAllocationTable[i]); });
if(test != blockAllocationTable[1024]) throw new Exception("Swapping failed");
}
[Benchmark]
public void Linq()
{
ReadOnlySpan<byte> span = _batData;
uint[] blockAllocationTable =
MemoryMarshal.Cast<byte, uint>(span)[..(_batData.Length / sizeof(uint))].ToArray();
uint test = Swap(blockAllocationTable[1024]);
// Swap all uint endianness
blockAllocationTable = blockAllocationTable.Select(Swap).ToArray();
if(test != blockAllocationTable[1024]) throw new Exception("Swapping failed");
}
[Benchmark]
public void ParallelLinq()
{
ReadOnlySpan<byte> span = _batData;
uint[] blockAllocationTable =
MemoryMarshal.Cast<byte, uint>(span)[..(_batData.Length / sizeof(uint))].ToArray();
uint test = Swap(blockAllocationTable[1024]);
// Swap all uint endianness
blockAllocationTable = blockAllocationTable.AsParallel().AsOrdered().Select(Swap).ToArray();
if(test != blockAllocationTable[1024]) throw new Exception("Swapping failed");
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint Swap(uint x)
{
x = x << 8 & 0xFF00FF00 | x >> 8 & 0xFF00FF;
return x << 16 | x >> 16;
}
}
#endif

View File

@@ -3,6 +3,9 @@ using System.Globalization;
using System.IO;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Running;
#if NET10_0_OR_GREATER
using AaruBenchmark.Algorithms;
#endif
// ReSharper disable ArrangeNamespaceBody
@@ -17,14 +20,14 @@ namespace AaruBenchmark
var config = ManualConfig.Create(DefaultConfig.Instance);
config.CultureInfo = CultureInfo.InvariantCulture;
/* BenchmarkRunner.Run<ADCBenchs>(config);
BenchmarkRunner.Run<ADCBenchs>(config);
BenchmarkRunner.Run<AppleRleBenchs>(config);
BenchmarkRunner.Run<TeleDiskLzhBenchs>(config);
BenchmarkRunner.Run<GzipBenchs>(config);
BenchmarkRunner.Run<Bzip2Benchs>(config);
BenchmarkRunner.Run<LzipBenchs>(config);*/
BenchmarkRunner.Run<LzipBenchs>(config);
BenchmarkRunner.Run<LzmaBenchs>(config);
/*BenchmarkRunner.Run<FlacBenchs>(config);
BenchmarkRunner.Run<FlacBenchs>(config);
BenchmarkRunner.Run<CompressGzipBenchs>(config);
BenchmarkRunner.Run<CompressBzip2Benchs>(config);
BenchmarkRunner.Run<CompressLzipBenchs>(config);
@@ -43,7 +46,11 @@ namespace AaruBenchmark
BenchmarkRunner.Run<Sha384Benchs>(config);
BenchmarkRunner.Run<Sha512Benchs>(config);
BenchmarkRunner.Run<SpamSumBenchs>(config);
BenchmarkRunner.Run<LambdaBenchs>(config);*/
BenchmarkRunner.Run<LambdaBenchs>(config);
#if NET10_0_OR_GREATER
BenchmarkRunner.Run<BatSwapping>(config);
#endif
}
}
}