From dea5949035b4d093be262701a3720cb537223b69 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Fri, 8 Aug 2025 21:05:08 +0100 Subject: [PATCH] Add benchmark for BAT swapping. --- AaruBenchmark/Algorithms/BatSwapping.cs | 99 +++++++++++++++++++++++++ AaruBenchmark/Program.cs | 15 +++- 2 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 AaruBenchmark/Algorithms/BatSwapping.cs diff --git a/AaruBenchmark/Algorithms/BatSwapping.cs b/AaruBenchmark/Algorithms/BatSwapping.cs new file mode 100644 index 0000000..2e358b5 --- /dev/null +++ b/AaruBenchmark/Algorithms/BatSwapping.cs @@ -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 span = _batData; + + uint[] blockAllocationTable = + MemoryMarshal.Cast(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 span = _batData; + + uint[] blockAllocationTable = + MemoryMarshal.Cast(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 span = _batData; + + uint[] blockAllocationTable = + MemoryMarshal.Cast(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 span = _batData; + + uint[] blockAllocationTable = + MemoryMarshal.Cast(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 \ No newline at end of file diff --git a/AaruBenchmark/Program.cs b/AaruBenchmark/Program.cs index 40ce8c9..d147cc1 100644 --- a/AaruBenchmark/Program.cs +++ b/AaruBenchmark/Program.cs @@ -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(config); + BenchmarkRunner.Run(config); BenchmarkRunner.Run(config); BenchmarkRunner.Run(config); BenchmarkRunner.Run(config); BenchmarkRunner.Run(config); - BenchmarkRunner.Run(config);*/ + BenchmarkRunner.Run(config); BenchmarkRunner.Run(config); - /*BenchmarkRunner.Run(config); + BenchmarkRunner.Run(config); BenchmarkRunner.Run(config); BenchmarkRunner.Run(config); BenchmarkRunner.Run(config); @@ -43,7 +46,11 @@ namespace AaruBenchmark BenchmarkRunner.Run(config); BenchmarkRunner.Run(config); BenchmarkRunner.Run(config); - BenchmarkRunner.Run(config);*/ + BenchmarkRunner.Run(config); + +#if NET10_0_OR_GREATER + BenchmarkRunner.Run(config); +#endif } } } \ No newline at end of file