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
Changes from 3 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
62 changes: 55 additions & 7 deletions src/Nethermind/Nethermind.Evm/IntrinsicGasCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
using Nethermind.Core;
using Nethermind.Core.Eip2930;
using Nethermind.Core.Specs;
using Nethermind.Int256;
using System.Runtime.Intrinsics;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

namespace Nethermind.Evm;

Expand Down Expand Up @@ -37,19 +40,64 @@ private static long DataCost(Transaction transaction, IReleaseSpec releaseSpec)
{
long txDataNonZeroGasCost =
releaseSpec.IsEip2028Enabled ? GasCostOf.TxDataNonZeroEip2028 : GasCostOf.TxDataNonZero;
long dataCost = 0;
Span<byte> data = transaction.Data.GetValueOrDefault().Span;
for (int i = 0; i < data.Length; i++)
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)
{
dataCost += data[i] == 0 ? GasCostOf.TxDataZero : txDataNonZeroGasCost;
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..];
}

if (transaction.IsContractCreation && releaseSpec.IsEip3860Enabled)
for (int i = 0; i < data.Length; i++)
{
dataCost += EvmPooledMemory.Div32Ceiling((UInt256)data.Length) * GasCostOf.InitCodeWord;
if (data[i] == 0)
{
totalZeros++;
}
}

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

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

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