Skip to content

Instantly share code, notes, and snippets.

View ocoanet's full-sized avatar

Olivier Coanet ocoanet

View GitHub Profile
@ocoanet
ocoanet / Sequencer V1
Last active August 29, 2015 14:01
Task based sequencer
using System;
using System.Threading.Tasks;
namespace Seq
{
public class Sequencer
{
private readonly object _lock = new object();
private Task _task = Task.FromResult(0);
@ocoanet
ocoanet / Sequencer V2
Last active August 29, 2015 14:01
Task based sequencer
using System;
using System.Threading;
using System.Threading.Tasks;
using log4net;
namespace Seq
{
// note: I do not usually comment my code that much :)
public class Sequencer
using System.Runtime.InteropServices;
using System.Threading;
namespace Foo
{
/// <summary>
/// Naive SPMC stack.
/// </summary>
public class NaiveStack
{
using System.Runtime.InteropServices;
using System.Threading;
namespace Foo
{
/// <summary>
/// Naive SPMC queue.
/// </summary>
public class NaiveQueue
{
Method | Mean | Error | StdDev |
------------------ |----------:|----------:|----------:|
IndexerUnsafe | 3.4213 ns | 0.0049 ns | 0.0041 ns |
IndexerArray | 4.9329 ns | 0.0093 ns | 0.0082 ns |
// fixed (int* pointer = _array)
00007FFD3B9E0620 sub rsp,28h
00007FFD3B9E0624 xor eax,eax
00007FFD3B9E0626 mov qword ptr [rsp+20h],rax
00007FFD3B9E062B mov rax,qword ptr [rcx+8]
00007FFD3B9E062F mov qword ptr [rsp+20h],rax
00007FFD3B9E0634 test rax,rax
00007FFD3B9E0637 je 00007FFD3B9E0644
00007FFD3B9E0639 mov rax,qword ptr [rsp+20h]
00007FFD3B9E063E cmp dword ptr [rax+8],0
00007FFD3B9E05E0 sub rsp,28h
00007FFD3B9E05E4 mov rax,qword ptr [rcx+8]
00007FFD3B9E05E8 mov edx,dword ptr [rcx+18h]
00007FFD3B9E05EB cmp edx,dword ptr [rax+8]
00007FFD3B9E05EE jae 00007FFD3B9E05FC
00007FFD3B9E05F0 movsxd rdx,edx
00007FFD3B9E05F3 mov eax,dword ptr [rax+rdx*4+10h]
00007FFD3B9E05F7 add rsp,28h
00007FFD3B9E05FB ret
public T this[long sequence]
{
get
{
ref var firstItem = ref Unsafe.As<object, T>(ref _entries[0]);
var index = _bufferPad + (int)(sequence & _indexMask);
return Unsafe.Add(ref firstItem, index);
}
}
00007FFD3B9D1DE0 push rdi
00007FFD3B9D1DE1 push rsi
00007FFD3B9D1DE2 sub rsp,28h
00007FFD3B9D1DE6 mov rsi,qword ptr [rcx+8]
00007FFD3B9D1DEA mov ecx,dword ptr [rcx+10h]
00007FFD3B9D1DED movsxd rdi,ecx
00007FFD3B9D1DF0 mov ecx,dword ptr [rsi]
00007FFD3B9D1DF2 mov rcx,qword ptr [rsi+40h]
00007FFD3B9D1DF6 xor edx,edx
00007FFD3B9D1DF8 mov r8,7FFD92BA70B0h
public T this[long sequence]
{
get
{
var index = _bufferPad + (int)(sequence & _indexMask);
return (T)_entries[index];
}
}