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

Optimize TryCalculateFeePerBlobGas #8173

Merged
merged 1 commit into from
Feb 6, 2025
Merged
Changes from all 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
21 changes: 13 additions & 8 deletions src/Nethermind/Nethermind.Evm/BlobGasCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,35 +50,40 @@ public static bool TryCalculateFeePerBlobGas(ulong excessBlobGas, UInt256 blobGa
{
static bool FakeExponentialOverflow(UInt256 factor, UInt256 num, UInt256 denominator, out UInt256 feePerBlobGas)
{
UInt256 output = UInt256.Zero;

if (UInt256.MultiplyOverflow(factor, denominator, out UInt256 numAccum))
UInt256 accumulator;
if (factor == UInt256.One)
{
// Skip expensive 256bit multiplication if factor is 1
accumulator = denominator;
}
else if (UInt256.MultiplyOverflow(factor, denominator, out accumulator))
{
feePerBlobGas = UInt256.MaxValue;
return true;
}

for (UInt256 i = 1; numAccum > 0; i++)
UInt256 output = default;
for (ulong i = 1; !accumulator.IsZero; i++)
Copy link
Member

Choose a reason for hiding this comment

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

in practice accumulator should never go pass ulong, should we have separate path for it?

{
if (UInt256.AddOverflow(output, numAccum, out output))
if (UInt256.AddOverflow(output, accumulator, out output))
{
feePerBlobGas = UInt256.MaxValue;
return true;
}

if (UInt256.MultiplyOverflow(numAccum, num, out UInt256 updatedNumAccum))
if (UInt256.MultiplyOverflow(accumulator, num, out UInt256 updatedAccumulator))
{
feePerBlobGas = UInt256.MaxValue;
return true;
}

if (UInt256.MultiplyOverflow(i, denominator, out UInt256 multipliedDeniminator))
if (UInt256.MultiplyOverflow(i, denominator, out UInt256 multipliedDenominator))
{
feePerBlobGas = UInt256.MaxValue;
return true;
}

numAccum = updatedNumAccum / multipliedDeniminator;
accumulator = updatedAccumulator / multipliedDenominator;
}

feePerBlobGas = output / denominator;
Expand Down
Loading