Skip to content

Commit

Permalink
Further optimize Rpl.LengthOf(...) (#6734)
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams authored Feb 16, 2024
1 parent 7137949 commit e1ab58a
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions src/Nethermind/Nethermind.Serialization.Rlp/Rlp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1555,17 +1555,36 @@ public static int LengthOf(UInt256? item)
return item is null ? LengthOfNull : LengthOf(item.Value);
}

public static int LengthOf(UInt256 item)
public static int LengthOf(in UInt256 item)
{
if (item < 128UL)
ulong value;
int size;
if (item.u3 > 0)
{
value = item.u3;
size = 1 + sizeof(ulong) * 4;
}
else if (item.u2 > 0)
{
value = item.u2;
size = 1 + sizeof(ulong) * 3;
}
else if (item.u1 > 0)
{
value = item.u1;
size = 1 + sizeof(ulong) * 2;
}
else if (item.u0 < 128)
{
return 1;
}
else
{
value = item.u0;
size = 1 + sizeof(ulong);
}

Span<byte> bytes = stackalloc byte[32];
item.ToBigEndian(bytes);
int length = bytes.WithoutLeadingZeros().Length;
return length + 1;
return size - (BitOperations.LeadingZeroCount(value) / 8);
}

public static int LengthOf(byte[][]? arrays)
Expand Down Expand Up @@ -1601,22 +1620,7 @@ public static int LengthOf(long value)
}
}

public static int LengthOf(int value)
{
if (value < 0)
{
return 9; // will be a sign extended long -> ulong
}
if ((uint)value < 128)
{
return 1;
}
else
{
// everything has a length prefix
return 1 + sizeof(uint) - (BitOperations.LeadingZeroCount((uint)value) / 8);
}
}
public static int LengthOf(int value) => LengthOf((long)value);

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

0 comments on commit e1ab58a

Please sign in to comment.