Skip to content

Commit

Permalink
Actually execute the delegates; Make parallelism optional
Browse files Browse the repository at this point in the history
  • Loading branch information
OoLunar committed Dec 12, 2024
1 parent e288d79 commit 5f44d14
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
3 changes: 2 additions & 1 deletion benchmarks/Benchmarks/AsyncEventHandlerBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq.Expressions;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using Microsoft.Extensions.Configuration;
using Moonlight.Api.Events;

namespace Moonlight.Benchmarks
Expand All @@ -22,7 +23,7 @@ public AsyncEventHandlerBenchmarks()
List<AsyncServerEvent<AsyncServerEventArgs>> asyncEvents = [];
foreach (int i in Enumerable.Range(0, Environment.ProcessorCount + 1).Where(x => x % 4 == 0).Append(1).Append(2).Append(5))
{
AsyncServerEvent<AsyncServerEventArgs> asyncEvent = new();
AsyncServerEvent<AsyncServerEventArgs> asyncEvent = new(new ConfigurationBuilder().Build());
asyncEvents.Add(asyncEvent);
int j = 0;
while (j < i)
Expand Down
35 changes: 28 additions & 7 deletions src/Moonlight.Api/Events/AsyncServerEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;

namespace Moonlight.Api.Events
{
Expand All @@ -14,8 +15,16 @@ public sealed record AsyncServerEvent<TEventArgs> where TEventArgs : AsyncServer
private readonly Dictionary<AsyncServerEventPreHandler<TEventArgs>, AsyncServerEventPriority> _preHandlers = [];
private readonly Dictionary<AsyncServerEventHandler<TEventArgs>, AsyncServerEventPriority> _postHandlers = [];

private AsyncServerEventPreHandler<TEventArgs> _preEventHandlerDelegate = _ => new ValueTask<bool>(true);
private AsyncServerEventHandler<TEventArgs> _postEventHandlerDelegate = _ => ValueTask.CompletedTask;
private AsyncServerEventPreHandler<TEventArgs> _preEventHandlerDelegate;
private AsyncServerEventHandler<TEventArgs> _postEventHandlerDelegate;
private readonly IConfiguration _configuration;

public AsyncServerEvent(IConfiguration configuration)
{
_configuration = configuration;
_preEventHandlerDelegate = LazyPreHandler;
_postEventHandlerDelegate = LazyPostHandler;
}

public void AddPreHandler(AsyncServerEventPreHandler<TEventArgs> handler, AsyncServerEventPriority priority = AsyncServerEventPriority.Normal) => _preHandlers.Add(handler, priority);
public void AddPostHandler(AsyncServerEventHandler<TEventArgs> handler, AsyncServerEventPriority priority = AsyncServerEventPriority.Normal) => _postHandlers.Add(handler, priority);
Expand Down Expand Up @@ -44,7 +53,7 @@ public void Prepare()
{
_preEventHandlerDelegate = async ValueTask<bool> (TEventArgs eventArgs) => await preHandlers[0](eventArgs) && await preHandlers[1](eventArgs);
}
else if (preHandlers.Count < Math.Min(Environment.ProcessorCount, 8))
else if (!_configuration.GetValue("Moonlight:Events:Parallelize", false) || preHandlers.Count < _configuration.GetValue("Moonlight:Events:MinParallelHandlers", Environment.ProcessorCount))
{
_preEventHandlerDelegate = async eventArgs =>
{
Expand Down Expand Up @@ -75,7 +84,7 @@ public void Prepare()
{
_postEventHandlerDelegate = postHandlers[0];
}
else if (postHandlers.Count < Math.Min(Environment.ProcessorCount, 8))
else if (!_configuration.GetValue("Moonlight:Events:Parallelize", false) || postHandlers.Count < _configuration.GetValue("Moonlight:Events:MinParallelHandlers", Environment.ProcessorCount))
{
_postEventHandlerDelegate = async (TEventArgs eventArgs) =>
{
Expand All @@ -92,9 +101,21 @@ public void Prepare()
}
}

private ValueTask<bool> EmptyPreHandler(TEventArgs _) => ValueTask.FromResult(true);
private ValueTask EmptyPostHandler(TEventArgs _) => ValueTask.CompletedTask;
private static ValueTask<bool> EmptyPreHandler(TEventArgs _) => ValueTask.FromResult(true);
private static ValueTask EmptyPostHandler(TEventArgs _) => ValueTask.CompletedTask;

private ValueTask<bool> LazyPreHandler(TEventArgs eventArgs)
{
Prepare();
return _preEventHandlerDelegate(eventArgs);
}

private ValueTask LazyPostHandler(TEventArgs eventArgs)
{
Prepare();
return _postEventHandlerDelegate(eventArgs);
}

public override string ToString() => $"{PreHandlers.Count}, {PostHandlers.Count}";
public override string ToString() => $"{GetType()}, PreHandlers: {_preHandlers.Count}, PostHandlers: {_postHandlers.Count}";
}
}
4 changes: 1 addition & 3 deletions src/Moonlight.Api/Events/AsyncServerEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;

namespace Moonlight.Api.Events
{
public class AsyncServerEventArgs : EventArgs;
public class AsyncServerEventArgs : System.EventArgs;
}

0 comments on commit 5f44d14

Please sign in to comment.