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

Reduce allocations in StorageRangesMessageSerializer #6534

Merged
merged 2 commits into from
Jan 14, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using DotNetty.Buffers;
using Nethermind.Core.Crypto;
using Nethermind.Serialization.Rlp;
using Nethermind.State.Snap;

namespace Nethermind.Network.P2P.Subprotocols.Snap.Messages
{
public class StorageRangesMessageSerializer : IZeroMessageSerializer<StorageRangeMessage>
public sealed class StorageRangesMessageSerializer : IZeroMessageSerializer<StorageRangeMessage>
{
private readonly Func<RlpStream, PathWithStorageSlot> _decodeSlot;
private readonly Func<RlpStream, PathWithStorageSlot[]> _decodeSlotArray;

public StorageRangesMessageSerializer()
{
// Capture closures once
_decodeSlot = DecodeSlot;
_decodeSlotArray = s => s.DecodeArray(_decodeSlot);
}

public void Serialize(IByteBuffer byteBuffer, StorageRangeMessage message)
{
(int contentLength, int allSlotsLength, int[] accountSlotsLengths, int proofsLength) = CalculateLengths(message);
Expand Down Expand Up @@ -71,7 +82,7 @@ public StorageRangeMessage Deserialize(IByteBuffer byteBuffer)
stream.ReadSequenceLength();

message.RequestId = stream.DecodeLong();
message.Slots = stream.DecodeArray(s => s.DecodeArray(DecodeSlot));
benaadams marked this conversation as resolved.
Show resolved Hide resolved
message.Slots = stream.DecodeArray(_decodeSlotArray);
message.Proofs = stream.DecodeArray(s => s.DecodeByteArray());

return message;
Expand Down
24 changes: 18 additions & 6 deletions src/Nethermind/Nethermind.State/Snap/PathWithStorageSlot.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Numerics;
using Nethermind.Core.Crypto;

namespace Nethermind.State.Snap
{
public class PathWithStorageSlot
public readonly struct PathWithStorageSlot(ValueHash256 keyHash, byte[] slotRlpValue)
: IEquatable<PathWithStorageSlot>, IEqualityOperators<PathWithStorageSlot, PathWithStorageSlot, bool>
{
public PathWithStorageSlot(ValueHash256 keyHash, byte[] slotRlpValue)
public ValueHash256 Path { get; } = keyHash;
public byte[] SlotRlpValue { get; } = slotRlpValue;

public bool Equals(in PathWithStorageSlot other)
{
Path = keyHash;
SlotRlpValue = slotRlpValue;
return Path == other.Path && SlotRlpValue.AsSpan().SequenceEqual(other.SlotRlpValue);
}

public ValueHash256 Path { get; set; }
public byte[] SlotRlpValue { get; set; }
public bool Equals(PathWithStorageSlot other) => Equals(in other);
benaadams marked this conversation as resolved.
Show resolved Hide resolved

public static bool operator ==(PathWithStorageSlot left, PathWithStorageSlot right) => left.Equals(in right);

public static bool operator !=(PathWithStorageSlot left, PathWithStorageSlot right) => !left.Equals(in right);

public override bool Equals(object obj) => obj is PathWithStorageSlot pws && Equals(in pws);

public override int GetHashCode() => throw new NotImplementedException();
}
}