Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove unsafe code from MultiProducerSequencer
- Loading branch information
Showing
8 changed files
with
698 additions
and
30 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
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,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
51
src/Disruptor.Benchmarks/MultiProducerSequencerBenchmarks.cs
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,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); | ||
} | ||
} | ||
} |
Oops, something went wrong.