Skip to content

Commit

Permalink
Remove unsafe code from MultiProducerSequencer
Browse files Browse the repository at this point in the history
  • Loading branch information
ocoanet committed Apr 1, 2018
1 parent c7078c0 commit f777435
Show file tree
Hide file tree
Showing 8 changed files with 698 additions and 30 deletions.
17 changes: 9 additions & 8 deletions src/Disruptor.Benchmarks/Disruptor.Benchmarks.csproj
Expand Up @@ -2,26 +2,27 @@

<PropertyGroup>
<AssemblyName>Disruptor.Benchmarks</AssemblyName>
<RootNamespace>Disruptor.Benchmarks</RootNamespace>
<Title>Disruptor.Benchmarks</Title>
<RootNamespace>Disruptor.Benchmarks</RootNamespace>
<Title>Disruptor.Benchmarks</Title>
<TargetFramework>net47</TargetFramework>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
<OutputType>exe</OutputType>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<OutputType>exe</OutputType>
</PropertyGroup>

<ItemGroup>
<None Remove="BenchmarkDotNet.Artifacts\**" />
<None Remove="Disruptor.Benchmarks.v3.ncrunchproject" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.10.10" />
<PackageReference Include="BenchmarkDotNet" Version="0.10.10" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.4.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Disruptor\Disruptor.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="ObjectLayoutInspector">
<HintPath>..\..\tools\ObjectLayoutInspector\ObjectLayoutInspector.dll</HintPath>
Expand Down
53 changes: 53 additions & 0 deletions src/Disruptor.Benchmarks/Int32ArrayBenchmarks.cs
@@ -0,0 +1,53 @@
using BenchmarkDotNet.Attributes;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Disruptor.Benchmarks
{
public unsafe class Int32ArrayBenchmarks
{
private int[] _array;
private GCHandle _gcHandle;
private int* _fixedArrayPointer;

public Int32ArrayBenchmarks()
{
_array = new int[1024];
_gcHandle = GCHandle.Alloc(new int[1024], GCHandleType.Pinned);
_fixedArrayPointer = (int*)_gcHandle.AddrOfPinnedObject();
}

public int Index = 371;

[Benchmark]
[MethodImpl(MethodImplOptions.NoInlining)]
public void Write()
{
_array[Index] = 777;
}

[Benchmark]
[MethodImpl(MethodImplOptions.NoInlining)]
public void WriteFixed()
{
fixed (int* pointer = _array)
{
pointer[Index] = 888;
}
}

[Benchmark]
[MethodImpl(MethodImplOptions.NoInlining)]
public void WritePointer()
{
_fixedArrayPointer[Index] = 666;
}

[Benchmark]
[MethodImpl(MethodImplOptions.NoInlining)]
public void WriteUnsafe()
{
Unsafe.Add(ref _array[0], Index) = 999;
}
}
}
51 changes: 51 additions & 0 deletions src/Disruptor.Benchmarks/MultiProducerSequencerBenchmarks.cs
@@ -0,0 +1,51 @@
using BenchmarkDotNet.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Disruptor.Benchmarks
{
public unsafe class MultiProducerSequencerBenchmarks
{
private MultiProducerSequencer _sequencer;
private MultiProducerSequencerPointer _sequencerPointer;
private long _sequence;
private long _sequencePointer;

public MultiProducerSequencerBenchmarks()
{
_sequencer = new MultiProducerSequencer(1024, new BusySpinWaitStrategy());
_sequencerPointer = new MultiProducerSequencerPointer(1024, new BusySpinWaitStrategy());
_sequence = _sequencer.Next();
_sequencePointer = _sequencerPointer.Next();
}

[Benchmark]
public void Publish()
{
_sequencer.Publish(_sequence);
}

[Benchmark]
public bool IsAvailable()
{
return _sequencer.IsAvailable(_sequence);
}

[Benchmark]
public void PublishPointer()
{
_sequencerPointer.Publish(_sequence);
}

[Benchmark]
public bool IsAvailablePointer()
{
return _sequencerPointer.IsAvailable(_sequence);
}
}
}

0 comments on commit f777435

Please sign in to comment.