Skip to content

Commit

Permalink
- Marked Swap() with AggressiveInlining, and used this in SwapIfGreat…
Browse files Browse the repository at this point in the history
…er(). Together these two changes give a 15% performance improvement in the synthetic benchmark.

- Added IntroSortKVWBenchmarks.
  • Loading branch information
colgreen committed Jan 9, 2021
1 parent 8cff18c commit 6d8b272
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 18 deletions.
46 changes: 46 additions & 0 deletions Redzen.Benchmarks/IntroSortKVWBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using BenchmarkDotNet.Attributes;
using Redzen.Random;
using Redzen.Sorting;

namespace Redzen.Benchmarks
{
[InvocationCount(1000)]
[MinWarmupCount(6, forceAutoWarmup: true)] // when InvocationCount is set, BDN does not run Pilot Stage, so to get the code promoted to Tier 1 before Actual Workload, we enforce more Warmups
public class IntroSortKVWBenchmarks
{
[Params(50_000)]
public int Length;

int[] _keys;
int[] _values;
int[] _values2;
IRandomSource _rng = RandomDefaults.CreateRandomSource(123);

[GlobalSetup]
public void GlobalSetup()
{
_keys = new int[Length];
_values = new int[Length];
_values2 = new int[Length];

for(int i=0; i < _keys.Length; i++)
{
_keys[i] = _rng.Next();
_values[i] = _keys[i];
_values2[i] = _keys[i];
}
}

[IterationSetup]
public void IterationSetup()
{
SortUtils.Shuffle<int>(_keys, _rng);
}

[Benchmark]
public void Sort()
{
IntroSort<int,int,int>.Sort(_keys, _values, _values2);
}
}
}
10 changes: 7 additions & 3 deletions Redzen.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using BenchmarkDotNet.Running;
using System;
using System.Diagnostics;
using BenchmarkDotNet.Running;
using Redzen.Random;
using Redzen.Sorting;

namespace Redzen.Benchmarks
{
Expand All @@ -12,10 +16,10 @@ static void Main(string[] args)
//var xoshiro256StarStarSummary = BenchmarkRunner.Run<Xoshiro256StarStarRandomBenchmark>();
//var xoshiro512StarStarSummary = BenchmarkRunner.Run<Xoshiro512StarStarRandomBenchmark>();

var zigguratGaussianSummary = BenchmarkRunner.Run<ZigguratGaussianDistributionBenchmark>();
//var zigguratGaussianSummary = BenchmarkRunner.Run<ZigguratGaussianDistributionBenchmark>();
//var boxMullerGaussianSummary = BenchmarkRunner.Run<BoxMullerGaussianDistributionBenchmark>();

//ArraySortPerfTest.RunTests(50_000, 1000);
var introSortSummary = BenchmarkRunner.Run<IntroSortKVWBenchmarks>();
}
}
}
20 changes: 5 additions & 15 deletions Redzen/Sorting/IntroSortKVW.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private static int PickPivotAndPartition(
Span<V> values,
Span<W> wspan)
{
Debug.Assert(keys.Length >= __introsortSizeThreshold);
Debug.Assert(keys.Length >= __introsortSizeThreshold);

int hi = keys.Length - 1;

Expand Down Expand Up @@ -261,27 +261,17 @@ private static void SwapIfGreater(
Span<K> keys,
Span<V> vspan,
Span<W> wspan,
int i, int j)
int i,int j)
{
Debug.Assert(i != j);

ref K keyRef = ref keys[i];
if (GreaterThan(ref keyRef, ref keys[j]))
if(GreaterThan(ref keys[i],ref keys[j]))
{
K key = keyRef;
keys[i] = keys[j];
keys[j] = key;

V v = vspan[i];
vspan[i] = vspan[j];
vspan[j] = v;

W w = wspan[i];
wspan[i] = wspan[j];
wspan[j] = w;
Swap(keys,vspan,wspan,i,j);
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void Swap(
Span<K> keys,
Span<V> vspan,
Expand Down

0 comments on commit 6d8b272

Please sign in to comment.