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

Vectorize some Bytes operations #5359

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions src/Nethermind/Nethermind.Core.Test/BytesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,13 @@ public void Get_bit_works(byte value, int position, bool expectedResult)

[TestCase("0x", 0)]
[TestCase("0x1000", 1)]
[TestCase("0x100000", 2)]
[TestCase("0x10000000", 3)]
[TestCase("0x1000000000", 4)]
[TestCase("0x100000000000", 5)]
[TestCase("0x10000000000000", 6)]
[TestCase("0x1000000000000000", 7)]
[TestCase("0x100000000000000000", 8)]
[TestCase("0x0000", 2)]
[TestCase("0x000100", 1)]
public void Trailing_zeros_count_works(string hex, int expectedResult)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,25 @@ public class Bytes32ConverterTests : ConverterTestBase<byte[]>
{
[TestCase(null)]
[TestCase(new byte[0])]
[TestCase(new byte[] { 1 })]
public void Empty_value(byte[] values)
{
TestConverter(
values,
(before, after) => Bytes.AreEqual(before.WithoutLeadingZeros(), after.WithoutLeadingZeros()),
new Bytes32Converter());
}

[TestCase(new byte[] { 1 })]
[TestCase(new byte[] { 0, 1 })]
[TestCase(new byte[] { 0, 0, 1 })]
[TestCase(new byte[] { 0, 0, 1, 0 })]
[TestCase(new byte[] { 0, 0, 1, 0, 0 })]
public void NonEmpty_value(byte[] values)
{
TestConverter(
values,
(before, after) => Bytes.AreEqual(before.WithoutLeadingZeros(), after.WithoutLeadingZeros()),
new Bytes32Converter());
}
benaadams marked this conversation as resolved.
Show resolved Hide resolved
}
}
2 changes: 2 additions & 0 deletions src/Nethermind/Nethermind.Core.Test/KeccakTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public void Zero()

[TestCase("0x0000000000000000000000000000000000000000000000000000000000000000", null, -1)]
[TestCase("0x0000000000000000000000000000000000000000000000000000000000000000", "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", -1)]
[TestCase("0x0000000000000000000000000000000000000000000000000000000000000000", "0x0000000000000000000000000000000000000000000000000000000000000000", 0)]
[TestCase("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", 0)]
[TestCase("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", "0x0000000000000000000000000000000000000000000000000000000000000000", 1)]
public void Compare(string a, string b, int result)
{
Expand Down
57 changes: 10 additions & 47 deletions src/Nethermind/Nethermind.Core/Extensions/Bytes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,61 +151,29 @@ public static bool AreEqual(Span<byte> a1, Span<byte> a2)

public static bool IsZero(this byte[] bytes)
{
return IsZero((ReadOnlySpan<byte>)bytes);
return bytes.AsSpan().IndexOfAnyExcept((byte)0) < 0;
}

public static bool IsZero(this Span<byte> bytes)
{
return IsZero((ReadOnlySpan<byte>)bytes);
return bytes.IndexOfAnyExcept((byte)0) < 0;
}

public static bool IsZero(this ReadOnlySpan<byte> bytes)
{
if (bytes.Length == 32)
{
return bytes[31] == 0 && bytes.SequenceEqual(Zero32);
}

for (int i = 0; i < bytes.Length / 2; i++)
{
if (bytes[i] != 0)
{
return false;
}

if (bytes[bytes.Length - i - 1] != 0)
{
return false;
}
}

return bytes.Length % 2 == 0 || bytes[bytes.Length / 2] == 0;
return bytes.IndexOfAnyExcept((byte)0) < 0;
}

public static int LeadingZerosCount(this Span<byte> bytes, int startIndex = 0)
{
for (int i = startIndex; i < bytes.Length; i++)
{
if (bytes[i] != 0)
{
return i - startIndex;
}
}

return bytes.Length - startIndex;
int nonZeroIndex = bytes[startIndex..].IndexOfAnyExcept((byte)0);
return nonZeroIndex < 0 ? bytes.Length - startIndex : nonZeroIndex;
}

public static int TrailingZerosCount(this byte[] bytes)
{
for (int i = 0; i < bytes.Length; i++)
{
if (bytes[bytes.Length - i - 1] != 0)
{
return i;
}
}

return bytes.Length;
int lastIndex = bytes.AsSpan().LastIndexOfAnyExcept((byte)0);
return lastIndex < 0 ? bytes.Length : bytes.Length - lastIndex - 1;
}

public static Span<byte> WithoutLeadingZeros(this byte[] bytes)
Expand All @@ -215,15 +183,10 @@ public static Span<byte> WithoutLeadingZeros(this byte[] bytes)

public static Span<byte> WithoutLeadingZeros(this Span<byte> bytes)
{
for (int i = 0; i < bytes.Length; i++)
{
if (bytes[i] != 0)
{
return bytes.Slice(i, bytes.Length - i);
}
}
if (bytes.Length == 0) return new byte[] { 0 };

return new byte[] { 0 };
int nonZeroIndex = bytes.IndexOfAnyExcept((byte)0);
return nonZeroIndex < 0 ? bytes[^1..] : bytes[nonZeroIndex..];
}

public static byte[] Concat(byte prefix, byte[] bytes)
Expand Down