Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce string allocs by networking #6971

Merged
merged 2 commits into from
May 2, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 22 additions & 32 deletions src/Nethermind/Nethermind.Network.Enr/NodeRecordSigner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,53 +54,43 @@ public NodeRecord Deserialize(RlpStream rlpStream)
while (rlpStream.Position < startPosition + recordRlpLength)
{
ReadOnlySpan<byte> key = rlpStream.DecodeByteArraySpan();
if (key.Length == 2)
switch (key.Length)
{
if (key.SequenceEqual(EnrContentKey.IdU8))
{
case 2 when key.SequenceEqual(EnrContentKey.IdU8):
rlpStream.SkipItem();
nodeRecord.SetEntry(IdEntry.Instance);
}
else if (key.SequenceEqual(EnrContentKey.IpU8))
{
break;
case 2 when key.SequenceEqual(EnrContentKey.IpU8):
ReadOnlySpan<byte> ipBytes = rlpStream.DecodeByteArraySpan();
IPAddress address = new(ipBytes);
nodeRecord.SetEntry(new IpEntry(address));
}
}
else if (key.Length == 3)
{
if (key.SequenceEqual(EnrContentKey.EthU8))
{
break;
case 3 when key.SequenceEqual(EnrContentKey.EthU8):
_ = rlpStream.ReadSequenceLength();
_ = rlpStream.ReadSequenceLength();
byte[] forkHash = rlpStream.DecodeByteArray();
long nextBlock = rlpStream.DecodeLong();
nodeRecord.SetEntry(new EthEntry(forkHash, nextBlock));
}
else if (key.SequenceEqual(EnrContentKey.TcpU8))
{
break;
case 3 when key.SequenceEqual(EnrContentKey.TcpU8):
int tcpPort = rlpStream.DecodeInt();
nodeRecord.SetEntry(new TcpEntry(tcpPort));
}
else if (key.SequenceEqual(EnrContentKey.UdpU8))
{
break;
case 3 when key.SequenceEqual(EnrContentKey.UdpU8):
int udpPort = rlpStream.DecodeInt();
nodeRecord.SetEntry(new UdpEntry(udpPort));
}
}
else if (key.SequenceEqual(EnrContentKey.Secp256K1U8))
{
ReadOnlySpan<byte> keyBytes = rlpStream.DecodeByteArraySpan();
CompressedPublicKey reportedKey = new(keyBytes);
nodeRecord.SetEntry(new Secp256K1Entry(reportedKey));
}
else
{
// snap
canVerify = false;
rlpStream.SkipItem();
nodeRecord.Snap = true;
break;
case 9 when key.SequenceEqual(EnrContentKey.Secp256K1U8):
ReadOnlySpan<byte> keyBytes = rlpStream.DecodeByteArraySpan();
CompressedPublicKey reportedKey = new(keyBytes);
nodeRecord.SetEntry(new Secp256K1Entry(reportedKey));
break;
default:
// snap
canVerify = false;
rlpStream.SkipItem();
nodeRecord.Snap = true;
break;
benaadams marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down