Skip to content

Commit

Permalink
Reduce allocations in StorageRangesMessageSerializer (#6534)
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams authored Jan 14, 2024
1 parent 729fc6c commit 864d265
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
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));
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);

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();
}
}

0 comments on commit 864d265

Please sign in to comment.