1
0
mirror of https://github.com/oliverbooth/TcpDotNet synced 2024-11-23 00:28:48 +00:00

perf: remove LINQ in ctor lookup

This commit is contained in:
Oliver Booth 2024-02-14 15:22:41 +00:00
parent 45a0b3898a
commit 50a73d21eb
Signed by: oliverbooth
GPG Key ID: E60B570D1B7557B5

View File

@ -140,10 +140,7 @@ public abstract class ClientNode : Node
return null;
}
const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
ConstructorInfo? constructor =
packetType.GetConstructors(bindingFlags).FirstOrDefault(c => c.GetParameters().Length == 0);
ConstructorInfo? constructor = GetConstructor(packetType);
if (constructor is null)
return null;
@ -259,6 +256,24 @@ public abstract class ClientNode : Node
return WaitForPacketAsync<TPacket>(completionSource, cancellationToken);
}
private static ConstructorInfo? GetConstructor(Type packetType)
{
const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
ConstructorInfo[] constructors = packetType.GetConstructors(bindingFlags);
ConstructorInfo? constructor = null;
for (var index = 0; index < constructors.Length; index++)
{
ConstructorInfo current = constructors[index];
if (current.GetParameters().Length == 0)
{
constructor = current;
break;
}
}
return constructor;
}
private async Task<TPacket> WaitForPacketAsync<TPacket>(TaskCompletionSource<Packet> completionSource,
CancellationToken cancellationToken = default)
where TPacket : Packet