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 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
43 changes: 37 additions & 6 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,16 +40,44 @@ 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++)

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

if (Vector256.IsHardwareAccelerated && data.Length >= Vector256<byte>.Count)
{
dataCost += data[i] == 0 ? GasCostOf.TxDataZero : txDataNonZeroGasCost;
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();
var zeros = BitOperations.PopCount(flags);
dataCost += zeros * GasCostOf.TxDataZero + (Vector256<byte>.Count - zeros) * txDataNonZeroGasCost;
benaadams marked this conversation as resolved.
Show resolved Hide resolved
}

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();
var zeros = BitOperations.PopCount(flags);
dataCost += zeros * GasCostOf.TxDataZero + (Vector128<byte>.Count - zeros) * txDataNonZeroGasCost;
}

if (transaction.IsContractCreation && releaseSpec.IsEip3860Enabled)
data = data[i..];
}

for (int i = 0; i < data.Length; i++)
{
dataCost += EvmPooledMemory.Div32Ceiling((UInt256)data.Length) * GasCostOf.InitCodeWord;
dataCost += data[i] == 0 ? GasCostOf.TxDataZero : txDataNonZeroGasCost;
}

return dataCost;
Expand Down