Skip to content

Commit

Permalink
减少多余的方法减少 JIT 压力
Browse files Browse the repository at this point in the history
  • Loading branch information
lindexi committed Jan 5, 2024
1 parent 6e1c17f commit 28d76f4
Showing 1 changed file with 14 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,13 @@ public void AddNotifyHandler<T>(string routedPath, Func<T, Task> handler)
/// <param name="handler"></param>
public void AddNotifyHandler<T>(string routedPath, Func<T, JsonIpcDirectRoutedContext, Task> handler)
{
AddNotifyHandler(routedPath, CreateHandleNotify(handler));
Task HandleNotify(MemoryStream stream, JsonIpcDirectRoutedContext context)
{
var argument = ToObject<T>(stream);
return handler(argument!, context);
}

AddNotifyHandler(routedPath, new NotifyHandler() { AsyncHandler = HandleNotify });
}

/// <summary>
Expand All @@ -114,7 +120,13 @@ public void AddNotifyHandler<T>(string routedPath, Action<T> handler)
/// <exception cref="InvalidOperationException"></exception>
public void AddNotifyHandler<T>(string routedPath, Action<T, JsonIpcDirectRoutedContext> handler)
{
AddNotifyHandler(routedPath, CreateHandleNotify(handler));
void HandleNotify(MemoryStream stream, JsonIpcDirectRoutedContext context)
{
var argument = ToObject<T>(stream);
handler(argument!, context);
}

AddNotifyHandler(routedPath, new NotifyHandler() { SyncHandler = HandleNotify });
}

private void AddNotifyHandler(string routedPath, NotifyHandler notifyHandler)
Expand All @@ -133,28 +145,6 @@ private class NotifyHandler
public Action<MemoryStream, JsonIpcDirectRoutedContext>? SyncHandler { get; set; }
}

private NotifyHandler CreateHandleNotify<T>(Action<T, JsonIpcDirectRoutedContext> handler)
{
void HandleNotify(MemoryStream stream, JsonIpcDirectRoutedContext context)
{
var argument = ToObject<T>(stream);
handler(argument!, context);
}

return new NotifyHandler() { SyncHandler = HandleNotify };
}

private NotifyHandler CreateHandleNotify<T>(Func<T, JsonIpcDirectRoutedContext, Task> handler)
{
Task HandleNotify(MemoryStream stream, JsonIpcDirectRoutedContext context)
{
var argument = ToObject<T>(stream);
return handler(argument!, context);
}

return new NotifyHandler() { AsyncHandler = HandleNotify };
}


private ConcurrentDictionary<string, NotifyHandler> HandleNotifyDictionary { get; } = new ConcurrentDictionary<string, NotifyHandler>();

Expand Down

0 comments on commit 28d76f4

Please sign in to comment.