Skip to content

Commit

Permalink
Fix allocation (#7175)
Browse files Browse the repository at this point in the history
  • Loading branch information
flcl42 authored Jun 12, 2024
1 parent 4fee4bf commit c0072bf
Showing 1 changed file with 67 additions and 75 deletions.
142 changes: 67 additions & 75 deletions src/Nethermind/Nethermind.Optimism/OPL1CostHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,109 +145,101 @@ public static UInt256 ComputeL1CostPreEcotone(UInt256 dataGasWithOverhead, UInt2
// Based on:
// https://github.com/ethereum-optimism/op-geth/blob/7c2819836018bfe0ca07c4e4955754834ffad4e0/core/types/rollup_cost.go
// https://github.com/Vectorized/solady/blob/5315d937d79b335c668896d7533ac603adac5315/js/solady.js
[SkipLocalsInit]
// Do not use SkipLocalsInit, `ht` should be zeros initially
public static uint ComputeFlzCompressLen(Transaction tx)
{
byte[] encoded = Rlp.Encode(tx, RlpBehaviors.SkipTypedWrapping).Bytes;

[SkipLocalsInit]
static uint FlzCompressLen(byte[] data)
{
uint n = 0;
uint[] ht = ArrayPool<uint>.Shared.Rent(8192);
try
Span<uint> ht = stackalloc uint[8192];

uint u24(uint i) => data[i] | ((uint)data[i + 1] << 8) | ((uint)data[i + 2] << 16);
uint cmp(uint p, uint q, uint e)
{
uint u24(uint i) => data[i] | ((uint)data[i + 1] << 8) | ((uint)data[i + 2] << 16);
uint cmp(uint p, uint q, uint e)
uint l = 0;
for (e -= q; l < e; l++)
{
uint l = 0;
for (e -= q; l < e; l++)
if (data[p + (int)l] != data[q + (int)l])
{
if (data[p + (int)l] != data[q + (int)l])
{
e = 0;
}
e = 0;
}
return l;
}
void literals(uint r)
return l;
}
void literals(uint r)
{
n += 0x21 * (r / 0x20);
r %= 0x20;
if (r != 0)
{
n += 0x21 * (r / 0x20);
r %= 0x20;
if (r != 0)
{
n += r + 1;
}
n += r + 1;
}
void match(uint l)
{
l--;
n += 3 * (l / 262);
if (l % 262 >= 6)
{
n += 3;
}
else
{
n += 2;
}
}
uint hash(uint v) => ((2654435769 * v) >> 19) & 0x1fff;
uint setNextHash(uint ip)
}
void match(uint l)
{
l--;
n += 3 * (l / 262);
if (l % 262 >= 6)
{
ht[hash(u24(ip))] = ip;
return ip + 1;
n += 3;
}
uint a = 0;
uint ipLimit = (uint)data.Length - 13;
if (data.Length < 13)
else
{
ipLimit = 0;
n += 2;
}
for (uint ip = a + 2; ip < ipLimit;)
}
uint hash(uint v) => ((2654435769 * v) >> 19) & 0x1fff;
uint setNextHash(uint ip, ref Span<uint> ht)
{
ht[(int)hash(u24(ip))] = ip;
return ip + 1;
}
uint a = 0;
uint ipLimit = (uint)data.Length - 13;
if (data.Length < 13)
{
ipLimit = 0;
}
for (uint ip = a + 2; ip < ipLimit;)
{
uint d;
uint r;
for (; ; )
{
uint d;
uint r;
for (; ; )
{
uint s = u24(ip);
uint h = hash(s);
r = ht[h];
ht[h] = ip;
d = ip - r;
if (ip >= ipLimit)
{
break;
}
ip++;
if (d <= 0x1fff && s == u24(r))
{
break;
}
}
uint s = u24(ip);
int h = (int)hash(s);
r = ht[h];
ht[h] = ip;
d = ip - r;
if (ip >= ipLimit)
{
break;
}
ip--;
if (ip > a)
ip++;
if (d <= 0x1fff && s == u24(r))
{
literals(ip - a);
break;
}
uint l = cmp(r + 3, ip + 3, ipLimit + 9);
match(l);
ip = setNextHash(setNextHash(ip + l));
a = ip;
}
literals((uint)data.Length - a);
return n;
}
finally
{
ArrayPool<uint>.Shared.Return(ht);
if (ip >= ipLimit)
{
break;
}
ip--;
if (ip > a)
{
literals(ip - a);
}
uint l = cmp(r + 3, ip + 3, ipLimit + 9);
match(l);
ip = setNextHash(setNextHash(ip + l, ref ht), ref ht);
a = ip;
}
literals((uint)data.Length - a);
return n;
}

return FlzCompressLen(encoded);
}
}

0 comments on commit c0072bf

Please sign in to comment.