Skip to content

Instantly share code, notes, and snippets.

View ocoanet's full-sized avatar

Olivier Coanet ocoanet

View GitHub Profile
@ocoanet
ocoanet / InterfaceDispatchBenchmarks_StaticOrNonStatic.cs
Last active January 27, 2023 08:45
InterfaceDispatchBenchmarks
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
namespace ConsoleApp_Bench_1;
public class InterfaceDispatchBenchmarks_StaticOrNonStatic
{
public static Accessor1[] Instances_Typed { get; } = Enumerable.Range(0, OperationsPerInvoke).Select(_ => new Accessor1()).ToArray();
public static IAccessor[] Instances_SingleType { get; } = Enumerable.Range(0, OperationsPerInvoke).Select(_ => (IAccessor)new Accessor1()).ToArray();
public static IAccessor[] Instances_MultiTypes { get; } = Enumerable.Range(0, OperationsPerInvoke).Select(CreateAccessor).ToArray();
@ocoanet
ocoanet / AsyncEventStream.cs
Last active October 5, 2022 20:04
DisruptorTasks
var stream = ringBuffer.NewAsyncEventStream();
await foreach (var batch in stream.ConfigureAwait(false))
{
foreach (var evt in batch)
{
// Process batch
}
}
@ocoanet
ocoanet / Getting-Started.md
Last active August 17, 2022 12:10
Disruptor - Getting started

Disruptor

The type you are probably the most interested in is Disruptor<T>. It contains a ring buffer and has convenient methods to register consumers. The ring buffer has a fixed capacity and cannot be resized, so you need to take a moment to think about the appropriate capacity for your use case.

// Size of the ring buffer, must be power of 2.
const int bufferSize = 1024;

// Create the disruptor
var disruptor = new Disruptor<SampleEvent>(() => new SampleEvent(), bufferSize);
@ocoanet
ocoanet / ReleaseNotes.md
Last active January 18, 2022 17:52
Disruptor V5

The V5 is clearly a major version, with the addition of two new event handler interfaces and the refactoring of the wait strategy API. A few changes were ported from the Java version, the more important being the removal of event handling extension interfaces. However many changes are .NET specific and some of them tend move the API away from the Java version in order to turn it into idiomatic .NET.

Major

  • Drop net452 and netstandard2.0 target frameworks (breaking change)
  • Remove ILifecycleAware (breaking change) Move methods to event handler interfaces with default implementations
  • Remove ITimeoutHandler (breaking change) Move OnTimeout to event handler interfaces with default implementation
  • Remove IBatchStartAware (breaking change) Move OnBatchStart to event handler interfaces with default implementation
  • Refactor IWaitStrategy (breaking change)
@ocoanet
ocoanet / Batching1.cs
Last active December 16, 2021 08:55
DisruptorAsyncExamples
public class H1_Batching1 : IBatchEventHandler<SampleEvent>
{
private readonly ISampleEventPersister _persister;
public H1_Batching(ISampleEventPersister persister)
{
_persister = persister;
}
public void OnBatch(ReadOnlySpan<SampleEvent> batch, long sequence)
| Method | Mean | Error | StdDev | Ratio |
|------------------- |----------:|----------:|----------:|------:|
| ReadWriteTyped | 0.6410 ns | 0.0016 ns | 0.0013 ns | 1.00 |
| ReadWriteSolution1 | 1.0355 ns | 0.0034 ns | 0.0030 ns | 1.62 |
| ReadWriteSolution3 | 1.0345 ns | 0.0039 ns | 0.0036 ns | 1.61 |
private static class TypeCache<T>
{
public static readonly bool CanUsePadding16 =
!IsReferenceOrContainsReferences(typeof(T)) && Unsafe.SizeOf<T>() <= 16;
// No static constructor
private static bool IsReferenceOrContainsReferences(Type type)
{
// Custom implementation using reflection
| Method | Mean | Error | StdDev | Ratio |
|------------------- |----------:|----------:|----------:|------:|
| ReadWriteTyped | 0.6433 ns | 0.0041 ns | 0.0036 ns | 1.00 |
| ReadWriteSolution1 | 1.0042 ns | 0.0033 ns | 0.0027 ns | 1.56 |
| ReadWriteSolution3 | 1.0082 ns | 0.0055 ns | 0.0049 ns | 1.57 |
public void Init<T>() => GC.KeepAlive(TypeCache<T>.CanUsePadding16);
| Method | Mean | Error | StdDev | Ratio |
|------------------- |----------:|----------:|----------:|------:|
| ReadWriteTyped | 0.6394 ns | 0.0013 ns | 0.0011 ns | 1.00 |
| ReadWriteSolution1 | 1.0342 ns | 0.0023 ns | 0.0020 ns | 1.62 |
| ReadWriteSolution3 | 4.2584 ns | 0.0021 ns | 0.0018 ns | 6.66 |