From 5f44d14780130c06e74335a4c2d022e156d28c3f Mon Sep 17 00:00:00 2001 From: Lunar Starstrum Date: Thu, 12 Dec 2024 02:13:52 -0600 Subject: [PATCH] Actually execute the delegates; Make parallelism optional --- .../Benchmarks/AsyncEventHandlerBenchmarks.cs | 3 +- src/Moonlight.Api/Events/AsyncServerEvent.cs | 35 +++++++++++++++---- .../Events/AsyncServerEventArgs.cs | 4 +-- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/benchmarks/Benchmarks/AsyncEventHandlerBenchmarks.cs b/benchmarks/Benchmarks/AsyncEventHandlerBenchmarks.cs index bc33c4b..54314a2 100644 --- a/benchmarks/Benchmarks/AsyncEventHandlerBenchmarks.cs +++ b/benchmarks/Benchmarks/AsyncEventHandlerBenchmarks.cs @@ -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 @@ -22,7 +23,7 @@ public AsyncEventHandlerBenchmarks() List> asyncEvents = []; foreach (int i in Enumerable.Range(0, Environment.ProcessorCount + 1).Where(x => x % 4 == 0).Append(1).Append(2).Append(5)) { - AsyncServerEvent asyncEvent = new(); + AsyncServerEvent asyncEvent = new(new ConfigurationBuilder().Build()); asyncEvents.Add(asyncEvent); int j = 0; while (j < i) diff --git a/src/Moonlight.Api/Events/AsyncServerEvent.cs b/src/Moonlight.Api/Events/AsyncServerEvent.cs index 5a7617c..bed3b26 100644 --- a/src/Moonlight.Api/Events/AsyncServerEvent.cs +++ b/src/Moonlight.Api/Events/AsyncServerEvent.cs @@ -3,6 +3,7 @@ using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Threading.Tasks; +using Microsoft.Extensions.Configuration; namespace Moonlight.Api.Events { @@ -14,8 +15,16 @@ public sealed record AsyncServerEvent where TEventArgs : AsyncServer private readonly Dictionary, AsyncServerEventPriority> _preHandlers = []; private readonly Dictionary, AsyncServerEventPriority> _postHandlers = []; - private AsyncServerEventPreHandler _preEventHandlerDelegate = _ => new ValueTask(true); - private AsyncServerEventHandler _postEventHandlerDelegate = _ => ValueTask.CompletedTask; + private AsyncServerEventPreHandler _preEventHandlerDelegate; + private AsyncServerEventHandler _postEventHandlerDelegate; + private readonly IConfiguration _configuration; + + public AsyncServerEvent(IConfiguration configuration) + { + _configuration = configuration; + _preEventHandlerDelegate = LazyPreHandler; + _postEventHandlerDelegate = LazyPostHandler; + } public void AddPreHandler(AsyncServerEventPreHandler handler, AsyncServerEventPriority priority = AsyncServerEventPriority.Normal) => _preHandlers.Add(handler, priority); public void AddPostHandler(AsyncServerEventHandler handler, AsyncServerEventPriority priority = AsyncServerEventPriority.Normal) => _postHandlers.Add(handler, priority); @@ -44,7 +53,7 @@ public void Prepare() { _preEventHandlerDelegate = async ValueTask (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 => { @@ -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) => { @@ -92,9 +101,21 @@ public void Prepare() } } - private ValueTask EmptyPreHandler(TEventArgs _) => ValueTask.FromResult(true); - private ValueTask EmptyPostHandler(TEventArgs _) => ValueTask.CompletedTask; + private static ValueTask EmptyPreHandler(TEventArgs _) => ValueTask.FromResult(true); + private static ValueTask EmptyPostHandler(TEventArgs _) => ValueTask.CompletedTask; + + private ValueTask 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}"; } } diff --git a/src/Moonlight.Api/Events/AsyncServerEventArgs.cs b/src/Moonlight.Api/Events/AsyncServerEventArgs.cs index 7c4f540..c52421f 100644 --- a/src/Moonlight.Api/Events/AsyncServerEventArgs.cs +++ b/src/Moonlight.Api/Events/AsyncServerEventArgs.cs @@ -1,6 +1,4 @@ -using System; - namespace Moonlight.Api.Events { - public class AsyncServerEventArgs : EventArgs; + public class AsyncServerEventArgs : System.EventArgs; }