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

Optimize RLP length calculation #6729

Merged
merged 6 commits into from
Feb 15, 2024
Merged
Changes from 4 commits
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
92 changes: 14 additions & 78 deletions src/Nethermind/Nethermind.Serialization.Rlp/Rlp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,7 @@ public static int SerializeLength(Span<byte> buffer, int position, int value)

if (value < 1 << 16)
{
buffer[position] = (byte)(value >> 8);
buffer[position + 1] = ((byte)value);
BinaryPrimitives.WriteInt16BigEndian(buffer.Slice(position), (short)value);
PaulusParssinen marked this conversation as resolved.
Show resolved Hide resolved
return position + 2;
}

Expand All @@ -420,31 +419,14 @@ public static int SerializeLength(Span<byte> buffer, int position, int value)
return position + 3;
}

buffer[position] = (byte)(value >> 24);
buffer[position + 1] = (byte)(value >> 16);
buffer[position + 2] = (byte)(value >> 8);
buffer[position + 3] = (byte)value;
BinaryPrimitives.WriteInt32BigEndian(buffer.Slice(position), value);
return position + 4;
}

public static int LengthOfLength(int value)
{
if (value < 1 << 8)
{
return 1;
}

if (value < 1 << 16)
{
return 2;
}

if (value < 1 << 24)
{
return 3;
}

return 4;
int bits = 32 - BitOperations.LeadingZeroCount((uint)value | 1);
return (bits + 7) / 8;
}

public static byte[] SerializeLength(int value)
Expand Down Expand Up @@ -1608,74 +1590,28 @@ public static int LengthOfNonce(ulong _)

public static int LengthOf(long value)
{
// everything has a length prefix
if (value < 0)
if ((ulong)value < 128)
{
return 9;
return 1;
}

if (value < 256L * 256L * 256L * 256L * 256L * 256L * 256L)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wild set of if statements has always bothered me when I saw it

else
{
if (value < 256L * 256L * 256L * 256L * 256L * 256L)
{
if (value < 256L * 256L * 256L * 256L * 256L)
{
if (value < 256L * 256L * 256L * 256L)
{
if (value < 256 * 256 * 256)
{
if (value < 256 * 256)
{
if (value < 128)
{
return 1;
}

return value < 256 ? 2 : 3;
}

return 4;
}

return 5;
}

return 6;
}

return 7;
}

return 8;
// everything has a length prefix
return 1 + sizeof(ulong) - (BitOperations.LeadingZeroCount((ulong)value) / 8);
}

return 9;
}

public static int LengthOf(int value)
{
// everything has a length prefix
if (value < 0)
if ((uint)value < 128)
{
return 9; // we cast it to long now
return 1;
PaulusParssinen marked this conversation as resolved.
Show resolved Hide resolved
}

if (value < 256 * 256 * 256)
else
{
if (value < 256 * 256)
{
if (value < 128)
{
return 1;
}

return value < 256 ? 2 : 3;
}

return 4;
// everything has a length prefix
return 1 + sizeof(uint) - (BitOperations.LeadingZeroCount((uint)value) / 8);
}

return 5;
}

public static int LengthOf(Hash256? item)
Expand Down
Loading