Skip to content

Commit

Permalink
Skip condition check
Browse files Browse the repository at this point in the history
  • Loading branch information
mayuki committed Dec 27, 2024
1 parent 0826679 commit 345623d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 36 deletions.
8 changes: 4 additions & 4 deletions src/Multicaster/InMemory/DynamicInMemoryProxyFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ static Core()
{
if (method.ReturnType == typeof(void))
{
// Fire-and-forget
// Action<T, T1, T2...>
// Fire-and-forget (void)
// Action<TTarget, T1, T2...>
Type[] delegateParamTypes = [typeof(T), .. method.GetParameters().Select(x => x.ParameterType)];
var delegateType = (method.GetParameters().Length switch
{
Expand All @@ -68,7 +68,7 @@ static Core()
_ => throw new NotImplementedException(),
}).MakeGenericType(delegateParamTypes);

// Invoke<T1, T2...>(T1, T2, ..., Action<T, T1, T2...>)
// Invoke<T1, T2...>(T1, T2, ..., Action<TTarget, T1, T2...>)
var methodInvoke = typeof(InMemoryProxyBase<TKey, T>).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)
.Single(x => x.Name == "Invoke" && x.GetGenericArguments().Length == method.GetParameters().Length);
if (methodInvoke.ContainsGenericParameters)
Expand All @@ -78,7 +78,7 @@ static Core()

// private static readonly Action<...> _thunk{MethodName}Delegate;
var fieldDelegate = typeBuilder.DefineField($"_thunk{method.Name}Delegate", delegateType, FieldAttributes.Private | FieldAttributes.Static);
// private static void _Thunk_{MethodName}(T self, T1 arg1, T2 arg2...) => self.MethodName(arg1, arg2...);
// private static void _Thunk_{MethodName}(TTarget self, T1 arg1, T2 arg2...) => self.MethodName(arg1, arg2...);
var methodThunk = typeBuilder.DefineMethod($"_Thunk_{method.Name}", MethodAttributes.Private | MethodAttributes.Static, typeof(void), delegateParamTypes);
{
var il = methodThunk.GetILGenerator();
Expand Down
65 changes: 33 additions & 32 deletions src/Multicaster/InMemory/InMemoryProxyBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ public abstract class InMemoryProxyBase<TKey, T>
private readonly IReceiverHolder<TKey, T> _receivers;
private readonly ImmutableArray<TKey> _excludes;
private readonly ImmutableArray<TKey>? _targets;
private readonly bool _alwaysInvokable;

public InMemoryProxyBase(IReceiverHolder<TKey, T> receivers, ImmutableArray<TKey> excludes, ImmutableArray<TKey>? targets)
{
_receivers = receivers;
_excludes = excludes;
_targets = targets;
_alwaysInvokable = _excludes.IsEmpty && _targets is null;
}

protected void Invoke(Action<T> invoker)
{
using var snapshot = _receivers.AsSnapshot();
foreach (var receiverRegistration in snapshot.AsSpan())
{
if (!CanInvoke(receiverRegistration)) continue;
if (!_alwaysInvokable && !CanInvoke(receiverRegistration)) continue;
try
{
invoker(receiverRegistration.Receiver);
Expand All @@ -38,7 +40,7 @@ protected void Invoke<T1>(T1 arg1, Action<T, T1> invoker)
using var snapshot = _receivers.AsSnapshot();
foreach (var receiverRegistration in snapshot.AsSpan())
{
if (!CanInvoke(receiverRegistration)) continue;
if (!_alwaysInvokable && !CanInvoke(receiverRegistration)) continue;
try
{
invoker(receiverRegistration.Receiver, arg1);
Expand All @@ -54,7 +56,7 @@ protected void Invoke<T1, T2>(T1 arg1, T2 arg2, Action<T, T1, T2> invoker)
using var snapshot = _receivers.AsSnapshot();
foreach (var receiverRegistration in snapshot.AsSpan())
{
if (!CanInvoke(receiverRegistration)) continue;
if (!_alwaysInvokable && !CanInvoke(receiverRegistration)) continue;
try
{
invoker(receiverRegistration.Receiver, arg1, arg2);
Expand All @@ -70,7 +72,7 @@ protected void Invoke<T1, T2, T3>(T1 arg1, T2 arg2, T3 arg3, Action<T, T1, T2, T
using var snapshot = _receivers.AsSnapshot();
foreach (var receiverRegistration in snapshot.AsSpan())
{
if (!CanInvoke(receiverRegistration)) continue;
if (!_alwaysInvokable && !CanInvoke(receiverRegistration)) continue;
try
{
invoker(receiverRegistration.Receiver, arg1, arg2, arg3);
Expand All @@ -86,7 +88,7 @@ protected void Invoke<T1, T2, T3, T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, Action
using var snapshot = _receivers.AsSnapshot();
foreach (var receiverRegistration in snapshot.AsSpan())
{
if (!CanInvoke(receiverRegistration)) continue;
if (!_alwaysInvokable && !CanInvoke(receiverRegistration)) continue;
try
{
invoker(receiverRegistration.Receiver, arg1, arg2, arg3, arg4);
Expand All @@ -102,7 +104,7 @@ protected void Invoke<T1, T2, T3, T4, T5>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5
using var snapshot = _receivers.AsSnapshot();
foreach (var receiverRegistration in snapshot.AsSpan())
{
if (!CanInvoke(receiverRegistration)) continue;
if (!_alwaysInvokable && !CanInvoke(receiverRegistration)) continue;
try
{
invoker(receiverRegistration.Receiver, arg1, arg2, arg3, arg4, arg5);
Expand All @@ -118,7 +120,7 @@ protected void Invoke<T1, T2, T3, T4, T5, T6>(T1 arg1, T2 arg2, T3 arg3, T4 arg4
using var snapshot = _receivers.AsSnapshot();
foreach (var receiverRegistration in snapshot.AsSpan())
{
if (!CanInvoke(receiverRegistration)) continue;
if (!_alwaysInvokable && !CanInvoke(receiverRegistration)) continue;
try
{
invoker(receiverRegistration.Receiver, arg1, arg2, arg3, arg4, arg5, arg6);
Expand All @@ -134,7 +136,7 @@ protected void Invoke<T1, T2, T3, T4, T5, T6, T7>(T1 arg1, T2 arg2, T3 arg3, T4
using var snapshot = _receivers.AsSnapshot();
foreach (var receiverRegistration in snapshot.AsSpan())
{
if (!CanInvoke(receiverRegistration)) continue;
if (!_alwaysInvokable && !CanInvoke(receiverRegistration)) continue;
try
{
invoker(receiverRegistration.Receiver, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
Expand All @@ -150,7 +152,7 @@ protected void Invoke<T1, T2, T3, T4, T5, T6, T7, T8>(T1 arg1, T2 arg2, T3 arg3,
using var snapshot = _receivers.AsSnapshot();
foreach (var receiverRegistration in snapshot.AsSpan())
{
if (!CanInvoke(receiverRegistration)) continue;
if (!_alwaysInvokable && !CanInvoke(receiverRegistration)) continue;
try
{
invoker(receiverRegistration.Receiver, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
Expand All @@ -166,7 +168,7 @@ protected void Invoke<T1, T2, T3, T4, T5, T6, T7, T8, T9>(T1 arg1, T2 arg2, T3 a
using var snapshot = _receivers.AsSnapshot();
foreach (var receiverRegistration in snapshot.AsSpan())
{
if (!CanInvoke(receiverRegistration)) continue;
if (!_alwaysInvokable && !CanInvoke(receiverRegistration)) continue;
try
{
invoker(receiverRegistration.Receiver, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
Expand All @@ -182,7 +184,7 @@ protected void Invoke<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(T1 arg1, T2 arg2,
using var snapshot = _receivers.AsSnapshot();
foreach (var receiverRegistration in snapshot.AsSpan())
{
if (!CanInvoke(receiverRegistration)) continue;
if (!_alwaysInvokable && !CanInvoke(receiverRegistration)) continue;
try
{
invoker(receiverRegistration.Receiver, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
Expand All @@ -198,7 +200,7 @@ protected void Invoke<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>(T1 arg1, T2
using var snapshot = _receivers.AsSnapshot();
foreach (var receiverRegistration in snapshot.AsSpan())
{
if (!CanInvoke(receiverRegistration)) continue;
if (!_alwaysInvokable && !CanInvoke(receiverRegistration)) continue;
try
{
invoker(receiverRegistration.Receiver, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11);
Expand All @@ -214,7 +216,7 @@ protected void Invoke<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(T1 arg1
using var snapshot = _receivers.AsSnapshot();
foreach (var receiverRegistration in snapshot.AsSpan())
{
if (!CanInvoke(receiverRegistration)) continue;
if (!_alwaysInvokable && !CanInvoke(receiverRegistration)) continue;
try
{
invoker(receiverRegistration.Receiver, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12);
Expand All @@ -230,7 +232,7 @@ protected void Invoke<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13>(T1
using var snapshot = _receivers.AsSnapshot();
foreach (var receiverRegistration in snapshot.AsSpan())
{
if (!CanInvoke(receiverRegistration)) continue;
if (!_alwaysInvokable && !CanInvoke(receiverRegistration)) continue;
try
{
invoker(receiverRegistration.Receiver, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13);
Expand All @@ -246,7 +248,7 @@ protected void Invoke<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T1
using var snapshot = _receivers.AsSnapshot();
foreach (var receiverRegistration in snapshot.AsSpan())
{
if (!CanInvoke(receiverRegistration)) continue;
if (!_alwaysInvokable && !CanInvoke(receiverRegistration)) continue;
try
{
invoker(receiverRegistration.Receiver, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14);
Expand All @@ -263,7 +265,7 @@ protected TResult InvokeWithResult<TResult>(Func<T, TResult> invoker)
using var snapshot = _receivers.AsSnapshot();
foreach (var receiverRegistration in snapshot.AsSpan())
{
if (!CanInvoke(receiverRegistration)) continue;
if (!_alwaysInvokable && !CanInvoke(receiverRegistration)) continue;
try
{
return invoker(receiverRegistration.Receiver);
Expand All @@ -281,7 +283,7 @@ protected TResult InvokeWithResult<T1, TResult>(T1 arg1, Func<T, T1, TResult> in
using var snapshot = _receivers.AsSnapshot();
foreach (var receiverRegistration in snapshot.AsSpan())
{
if (!CanInvoke(receiverRegistration)) continue;
if (!_alwaysInvokable && !CanInvoke(receiverRegistration)) continue;
try
{
return invoker(receiverRegistration.Receiver, arg1);
Expand All @@ -299,7 +301,7 @@ protected TResult InvokeWithResult<T1, T2, TResult>(T1 arg1, T2 arg2, Func<T, T1
using var snapshot = _receivers.AsSnapshot();
foreach (var receiverRegistration in snapshot.AsSpan())
{
if (!CanInvoke(receiverRegistration)) continue;
if (!_alwaysInvokable && !CanInvoke(receiverRegistration)) continue;
try
{
return invoker(receiverRegistration.Receiver, arg1, arg2);
Expand All @@ -317,7 +319,7 @@ protected TResult InvokeWithResult<T1, T2, T3, TResult>(T1 arg1, T2 arg2, T3 arg
using var snapshot = _receivers.AsSnapshot();
foreach (var receiverRegistration in snapshot.AsSpan())
{
if (!CanInvoke(receiverRegistration)) continue;
if (!_alwaysInvokable && !CanInvoke(receiverRegistration)) continue;
try
{
return invoker(receiverRegistration.Receiver, arg1, arg2, arg3);
Expand All @@ -335,7 +337,7 @@ protected TResult InvokeWithResult<T1, T2, T3, T4, TResult>(T1 arg1, T2 arg2, T3
using var snapshot = _receivers.AsSnapshot();
foreach (var receiverRegistration in snapshot.AsSpan())
{
if (!CanInvoke(receiverRegistration)) continue;
if (!_alwaysInvokable && !CanInvoke(receiverRegistration)) continue;
try
{
return invoker(receiverRegistration.Receiver, arg1, arg2, arg3, arg4);
Expand All @@ -353,7 +355,7 @@ protected TResult InvokeWithResult<T1, T2, T3, T4, T5, TResult>(T1 arg1, T2 arg2
using var snapshot = _receivers.AsSnapshot();
foreach (var receiverRegistration in snapshot.AsSpan())
{
if (!CanInvoke(receiverRegistration)) continue;
if (!_alwaysInvokable && !CanInvoke(receiverRegistration)) continue;
try
{
return invoker(receiverRegistration.Receiver, arg1, arg2, arg3, arg4, arg5);
Expand All @@ -371,7 +373,7 @@ protected TResult InvokeWithResult<T1, T2, T3, T4, T5, T6, TResult>(T1 arg1, T2
using var snapshot = _receivers.AsSnapshot();
foreach (var receiverRegistration in snapshot.AsSpan())
{
if (!CanInvoke(receiverRegistration)) continue;
if (!_alwaysInvokable && !CanInvoke(receiverRegistration)) continue;
try
{
return invoker(receiverRegistration.Receiver, arg1, arg2, arg3, arg4, arg5, arg6);
Expand All @@ -389,7 +391,7 @@ protected TResult InvokeWithResult<T1, T2, T3, T4, T5, T6, T7, TResult>(T1 arg1,
using var snapshot = _receivers.AsSnapshot();
foreach (var receiverRegistration in snapshot.AsSpan())
{
if (!CanInvoke(receiverRegistration)) continue;
if (!_alwaysInvokable && !CanInvoke(receiverRegistration)) continue;
try
{
return invoker(receiverRegistration.Receiver, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
Expand All @@ -407,7 +409,7 @@ protected TResult InvokeWithResult<T1, T2, T3, T4, T5, T6, T7, T8, TResult>(T1 a
using var snapshot = _receivers.AsSnapshot();
foreach (var receiverRegistration in snapshot.AsSpan())
{
if (!CanInvoke(receiverRegistration)) continue;
if (!_alwaysInvokable && !CanInvoke(receiverRegistration)) continue;
try
{
return invoker(receiverRegistration.Receiver, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
Expand All @@ -425,7 +427,7 @@ protected TResult InvokeWithResult<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult>(
using var snapshot = _receivers.AsSnapshot();
foreach (var receiverRegistration in snapshot.AsSpan())
{
if (!CanInvoke(receiverRegistration)) continue;
if (!_alwaysInvokable && !CanInvoke(receiverRegistration)) continue;
try
{
return invoker(receiverRegistration.Receiver, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
Expand All @@ -443,7 +445,7 @@ protected TResult InvokeWithResult<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TRes
using var snapshot = _receivers.AsSnapshot();
foreach (var receiverRegistration in snapshot.AsSpan())
{
if (!CanInvoke(receiverRegistration)) continue;
if (!_alwaysInvokable && !CanInvoke(receiverRegistration)) continue;
try
{
return invoker(receiverRegistration.Receiver, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
Expand All @@ -461,7 +463,7 @@ protected TResult InvokeWithResult<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,
using var snapshot = _receivers.AsSnapshot();
foreach (var receiverRegistration in snapshot.AsSpan())
{
if (!CanInvoke(receiverRegistration)) continue;
if (!_alwaysInvokable && !CanInvoke(receiverRegistration)) continue;
try
{
return invoker(receiverRegistration.Receiver, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11);
Expand All @@ -479,7 +481,7 @@ protected TResult InvokeWithResult<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,
using var snapshot = _receivers.AsSnapshot();
foreach (var receiverRegistration in snapshot.AsSpan())
{
if (!CanInvoke(receiverRegistration)) continue;
if (!_alwaysInvokable && !CanInvoke(receiverRegistration)) continue;
try
{
return invoker(receiverRegistration.Receiver, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12);
Expand All @@ -497,7 +499,7 @@ protected TResult InvokeWithResult<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,
using var snapshot = _receivers.AsSnapshot();
foreach (var receiverRegistration in snapshot.AsSpan())
{
if (!CanInvoke(receiverRegistration)) continue;
if (!_alwaysInvokable && !CanInvoke(receiverRegistration)) continue;
try
{
return invoker(receiverRegistration.Receiver, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13);
Expand All @@ -515,7 +517,7 @@ protected TResult InvokeWithResult<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,
using var snapshot = _receivers.AsSnapshot();
foreach (var receiverRegistration in snapshot.AsSpan())
{
if (!CanInvoke(receiverRegistration)) continue;
if (!_alwaysInvokable && !CanInvoke(receiverRegistration)) continue;
try
{
return invoker(receiverRegistration.Receiver, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14);
Expand All @@ -533,7 +535,7 @@ protected TResult InvokeWithResult<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,
using var snapshot = _receivers.AsSnapshot();
foreach (var receiverRegistration in snapshot.AsSpan())
{
if (!CanInvoke(receiverRegistration)) continue;
if (!_alwaysInvokable && !CanInvoke(receiverRegistration)) continue;
try
{
return invoker(receiverRegistration.Receiver, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15);
Expand All @@ -557,5 +559,4 @@ private void ThrowIfNotSingle()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool CanInvoke(ReceiverRegistration<TKey, T> r)
=> !r.HasKey || r.Key is null || (!_excludes.Contains(r.Key) && (_targets is null || _targets.Value.Contains(r.Key)));

}

0 comments on commit 345623d

Please sign in to comment.