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 DataCost #6953

Merged
merged 4 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
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
57 changes: 57 additions & 0 deletions src/Nethermind/Nethermind.Core/Extensions/Bytes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,63 @@ public static int CountLeadingZeros(this ReadOnlySpan<byte> bytes)
return leadingZeros;
}

public static int CountZeros(this Span<byte> data)
=> CountZeros((ReadOnlySpan<byte>)data);

public static int CountZeros(this ReadOnlySpan<byte> data)
{
int totalZeros = 0;
if (Vector512.IsHardwareAccelerated && data.Length >= Vector512<byte>.Count)
{
ref byte bytes = ref MemoryMarshal.GetReference(data);
int i = 0;
for (; i < data.Length - Vector512<byte>.Count; i += Vector512<byte>.Count)
{
Vector512<byte> dataVector = Unsafe.ReadUnaligned<Vector512<byte>>(ref Unsafe.Add(ref bytes, i));
ulong flags = Vector512.Equals(dataVector, default).ExtractMostSignificantBits();
totalZeros += BitOperations.PopCount(flags);
}

data = data[i..];
}
if (Vector256.IsHardwareAccelerated && data.Length >= Vector256<byte>.Count)
{
ref byte bytes = ref MemoryMarshal.GetReference(data);
int i = 0;
for (; i < data.Length - Vector256<byte>.Count; i += Vector256<byte>.Count)
{
Vector256<byte> dataVector = Unsafe.ReadUnaligned<Vector256<byte>>(ref Unsafe.Add(ref bytes, i));
uint flags = Vector256.Equals(dataVector, default).ExtractMostSignificantBits();
totalZeros += BitOperations.PopCount(flags);
}

data = data[i..];
}
if (Vector128.IsHardwareAccelerated && data.Length >= Vector128<byte>.Count)
{
ref byte bytes = ref MemoryMarshal.GetReference(data);
int i = 0;
for (; i < data.Length - Vector128<byte>.Count; i += Vector128<byte>.Count)
{
Vector128<byte> dataVector = Unsafe.ReadUnaligned<Vector128<byte>>(ref Unsafe.Add(ref MemoryMarshal.GetReference(data), i));
uint flags = Vector128.Equals(dataVector, default).ExtractMostSignificantBits();
totalZeros += BitOperations.PopCount(flags);
}

data = data[i..];
}

for (int i = 0; i < data.Length; i++)
{
if (data[i] == 0)
{
totalZeros++;
}
}
Comment on lines +1018 to +1024
Copy link
Member

Choose a reason for hiding this comment

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

btw can we optimize it here a bit more on ulongs?


return totalZeros;
}

[DebuggerStepThrough]
public static byte[] FromUtf8HexString(scoped ReadOnlySpan<byte> hexString)
{
Expand Down
54 changes: 4 additions & 50 deletions src/Nethermind/Nethermind.Evm/IntrinsicGasCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Runtime.Intrinsics;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using Nethermind.Core.Extensions;

namespace Nethermind.Evm;

Expand Down Expand Up @@ -41,63 +42,16 @@ private static long DataCost(Transaction transaction, IReleaseSpec releaseSpec)
long txDataNonZeroGasCost =
releaseSpec.IsEip2028Enabled ? GasCostOf.TxDataNonZeroEip2028 : GasCostOf.TxDataNonZero;
Span<byte> data = transaction.Data.GetValueOrDefault().Span;
int dataLength = data.Length;
int totalZeros = 0;
if (Vector512.IsHardwareAccelerated && data.Length >= Vector512<byte>.Count)
{
ref byte bytes = ref MemoryMarshal.GetReference(data);
int i = 0;
for (; i < data.Length - Vector512<byte>.Count; i += Vector512<byte>.Count)
{
Vector512<byte> dataVector = Unsafe.ReadUnaligned<Vector512<byte>>(ref Unsafe.Add(ref bytes, i));
ulong flags = Vector512.Equals(dataVector, default).ExtractMostSignificantBits();
totalZeros += BitOperations.PopCount(flags);
}

data = data[i..];
}
if (Vector256.IsHardwareAccelerated && data.Length >= Vector256<byte>.Count)
{
ref byte bytes = ref MemoryMarshal.GetReference(data);
int i = 0;
for (; i < data.Length - Vector256<byte>.Count; i += Vector256<byte>.Count)
{
Vector256<byte> dataVector = Unsafe.ReadUnaligned<Vector256<byte>>(ref Unsafe.Add(ref bytes, i));
uint flags = Vector256.Equals(dataVector, default).ExtractMostSignificantBits();
totalZeros += BitOperations.PopCount(flags);
}

data = data[i..];
}
if (Vector128.IsHardwareAccelerated && data.Length >= Vector128<byte>.Count)
{
ref byte bytes = ref MemoryMarshal.GetReference(data);
int i = 0;
for (; i < data.Length - Vector128<byte>.Count; i += Vector128<byte>.Count)
{
Vector128<byte> dataVector = Unsafe.ReadUnaligned<Vector128<byte>>(ref Unsafe.Add(ref MemoryMarshal.GetReference(data), i));
uint flags = Vector128.Equals(dataVector, default).ExtractMostSignificantBits();
totalZeros += BitOperations.PopCount(flags);
}

data = data[i..];
}

for (int i = 0; i < data.Length; i++)
{
if (data[i] == 0)
{
totalZeros++;
}
}
int totalZeros = data.CountZeros();

var baseDataCost = (transaction.IsContractCreation && releaseSpec.IsEip3860Enabled
? EvmPooledMemory.Div32Ceiling((UInt256)dataLength) * GasCostOf.InitCodeWord
? EvmPooledMemory.Div32Ceiling((UInt256)data.Length) * GasCostOf.InitCodeWord
: 0);

return baseDataCost +
totalZeros * GasCostOf.TxDataZero +
(dataLength - totalZeros) * txDataNonZeroGasCost;
(data.Length - totalZeros) * txDataNonZeroGasCost;
}

private static long AccessListCost(Transaction transaction, IReleaseSpec releaseSpec)
Expand Down