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

7591 abi encoding optimization #7617

Closed
Closed
Show file tree
Hide file tree
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
17 changes: 14 additions & 3 deletions src/Nethermind/Nethermind.Abi/AbiAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;

using DotNetty.Buffers;
using Nethermind.Core;
using Nethermind.Core.Extensions;
using Nethermind.Serialization.Rlp;

namespace Nethermind.Abi
{
Expand All @@ -21,14 +24,22 @@ private AbiAddress() : base(160)

public override byte[] Encode(object? arg, bool packed)
{
NettyAbiStream nettyAbiStream = new NettyAbiStream(PooledByteBufferAllocator.Default.Buffer(1));
while (true)
{
switch (arg)
{
case Address input:
{
byte[] bytes = input.Bytes;
return packed ? bytes : bytes.PadLeft(UInt256.LengthInBytes);
if (packed)
{
nettyAbiStream.Write(input.Bytes.ToList());
}
else
{
nettyAbiStream.Write(input.Bytes.PadLeft(UInt256.LengthInBytes).ToList());
}
return nettyAbiStream.AsSpan().ToArray();
Comment on lines +34 to +42
Copy link
Member

Choose a reason for hiding this comment

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

you allocate both list and array, not achieving goal of removing allocations

}
case string stringInput:
{
Expand Down
3 changes: 3 additions & 0 deletions src/Nethermind/Nethermind.Abi/AbiEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using DotNetty.Buffers;
using Nethermind.Core.Extensions;
using static Microsoft.FSharp.Core.ByRefKinds;

namespace Nethermind.Abi
{
Expand All @@ -21,6 +23,7 @@ public class AbiEncoder : IAbiEncoder

public byte[] Encode(AbiEncodingStyle encodingStyle, AbiSignature signature, params object[] arguments)
{

bool packed = (encodingStyle & AbiEncodingStyle.Packed) == AbiEncodingStyle.Packed;
bool includeSig = encodingStyle == AbiEncodingStyle.IncludeSignature;

Expand Down
5 changes: 5 additions & 0 deletions src/Nethermind/Nethermind.Abi/Nethermind.Abi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@

<ItemGroup>
<ProjectReference Include="..\Nethermind.Core\Nethermind.Core.csproj" />
<ProjectReference Include="..\Nethermind.Serialization.Rlp\Nethermind.Serialization.Rlp.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="MathNet.Numerics.FSharp" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Nethermind.DotNetty.Buffers" />
</ItemGroup>

</Project>
125 changes: 125 additions & 0 deletions src/Nethermind/Nethermind.Abi/NettyAbiStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DotNetty.Buffers;
using DotNetty.Common.Utilities;
using Nethermind.Serialization.Rlp;

namespace Nethermind.Abi
{
public sealed class NettyAbiStream : IDisposable
{

private readonly IByteBuffer _buffer;

private readonly int _initialPosition;

public NettyAbiStream(IByteBuffer buffer)
{
_buffer = buffer;
_initialPosition = buffer.ReaderIndex;
}

public void Write(ReadOnlySpan<byte> bytesToWrite)
{
_buffer.EnsureWritable(bytesToWrite.Length);

Span<byte> target =
_buffer.Array.AsSpan(_buffer.ArrayOffset + _buffer.WriterIndex, bytesToWrite.Length);
bytesToWrite.CopyTo(target);
int newWriterIndex = _buffer.WriterIndex + bytesToWrite.Length;

_buffer.SetWriterIndex(newWriterIndex);
}

public void Write(IReadOnlyList<byte> bytesToWrite)
{
_buffer.EnsureWritable(bytesToWrite.Count);
Span<byte> target =
_buffer.Array.AsSpan(_buffer.ArrayOffset + _buffer.WriterIndex, bytesToWrite.Count);
for (int i = 0; i < bytesToWrite.Count; ++i)
{
target[i] = bytesToWrite[i];
}

int newWriterIndex = _buffer.WriterIndex + bytesToWrite.Count;
_buffer.SetWriterIndex(newWriterIndex);
}

public void WriteByte(byte byteToWrite)
{
_buffer.EnsureWritable(1);
_buffer.WriteByte(byteToWrite);
}

public void WriteZero(int length)
{
_buffer.EnsureWritable(length);
_buffer.WriteZero(length);
}

public byte ReadByte()
{
return _buffer.ReadByte();
}

public Span<byte> Read(int length)
{
Span<byte> span = _buffer.Array.AsSpan(_buffer.ArrayOffset + _buffer.ReaderIndex, length);
_buffer.SkipBytes(span.Length);
return span;
}

public Span<byte> Peek(int offset, int length)
{
Span<byte> span = _buffer.Array.AsSpan(_buffer.ArrayOffset + _buffer.ReaderIndex + offset, length);
return span;
}

public byte PeekByte()
{
byte result = _buffer.ReadByte();
_buffer.SetReaderIndex(_buffer.ReaderIndex - 1);
return result;
}

public byte PeekByte(int offset)
{
_buffer.MarkReaderIndex();
_buffer.SkipBytes(offset);
byte result = _buffer.ReadByte();
_buffer.ResetReaderIndex();
return result;
}

public void SkipBytes(int length)
{
_buffer.SkipBytes(length);
}

public int Position
{
get => _buffer.ReaderIndex - _initialPosition;
set => _buffer.SetReaderIndex(_initialPosition + value);
}

public int Length => _buffer.ReadableBytes + (_buffer.ReaderIndex - _initialPosition);

public bool HasBeenRead => _buffer.ReadableBytes <= 0;

public string Description => "|NettyAibStream|description missing|";

/// <summary>
/// Note: this include already read bytes, not just the remaining one.
/// </summary>
/// <returns></returns>
public Span<byte> AsSpan() => _buffer.AsSpan(_initialPosition);

public void Dispose()
{
_buffer.SafeRelease();
}
}
}
2 changes: 1 addition & 1 deletion src/bench_precompiles
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please ignore this file. Will undo these changes once I have some confirmations from your end.

Submodule bench_precompiles updated 92 files
+0 −408 .gitignore
+0 −1 vectors/blsg1add/current/bench-g1add-1.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-1.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-10.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-11.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-12.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-128.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-13.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-14.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-15.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-16.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-17.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-18.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-19.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-2.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-20.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-2048.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-21.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-22.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-23.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-24.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-25.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-256.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-26.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-27.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-28.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-29.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-3.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-30.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-31.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-32.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-4.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-4096.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-5.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-512.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-6.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-64.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-7.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-8.csv
+0 −1 vectors/blsg1msm/current/bench-g1msm-9.csv
+0 −1 vectors/blsg1mul/current/bench-g1mul-1.csv
+0 −1 vectors/blsg2add/current/bench-g2add-1.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-1.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-10.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-11.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-12.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-128.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-13.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-14.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-15.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-16.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-17.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-18.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-19.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-2.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-20.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-2048.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-21.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-22.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-23.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-24.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-25.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-256.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-26.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-27.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-28.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-29.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-3.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-30.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-31.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-32.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-4.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-4096.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-5.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-512.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-6.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-64.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-7.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-8.csv
+0 −1 vectors/blsg2msm/current/bench-g2msm-9.csv
+0 −1 vectors/blsg2mul/current/bench-g2mul-1.csv
+0 −1 vectors/blsmapfp2tog2/current/bench-mapfp2-1.csv
+0 −1 vectors/blsmapfptog1/current/bench-mapfp-1.csv
+0 −1 vectors/blspairingcheck/current/bench-pairing-1.csv
+0 −1 vectors/blspairingcheck/current/bench-pairing-2.csv
+0 −1 vectors/blspairingcheck/current/bench-pairing-3.csv
+0 −1 vectors/blspairingcheck/current/bench-pairing-4.csv
+0 −1 vectors/blspairingcheck/current/bench-pairing-5.csv
+0 −1 vectors/blspairingcheck/current/bench-pairing-6.csv
+0 −1 vectors/blspairingcheck/current/bench-pairing-7.csv
+0 −1 vectors/blspairingcheck/current/bench-pairing-8.csv
+0 −142 vectors/modexp/current/file.json