Skip to content

Commit

Permalink
Trie RlpStreams as structs (NethermindEth#6584)
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams authored Jan 21, 2024
1 parent 807b87c commit 3d20677
Show file tree
Hide file tree
Showing 7 changed files with 537 additions and 71 deletions.
10 changes: 10 additions & 0 deletions src/Nethermind/Nethermind.Serialization.Rlp/ByteArrayExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ public static RlpStream AsRlpStream(in this CappedArray<byte> bytes)
return new(in bytes.IsNotNull ? ref bytes : ref CappedArray<byte>.Empty);
}

public static RlpFactory AsRlpFactory(this byte[]? bytes)
{
return new(bytes ?? Array.Empty<byte>());
}

public static RlpFactory AsRlpFactory(in this CappedArray<byte> bytes)
{
return new(in bytes.IsNotNull ? ref bytes : ref CappedArray<byte>.Empty);
}

public static Rlp.ValueDecoderContext AsRlpValueContext(this byte[]? bytes)
{
return new(bytes ?? Array.Empty<byte>());
Expand Down
29 changes: 29 additions & 0 deletions src/Nethermind/Nethermind.Serialization.Rlp/RlpFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using Nethermind.Core;
using Nethermind.Core.Buffers;

namespace Nethermind.Serialization.Rlp
{
public class RlpFactory
{
public long MemorySize => MemorySizes.SmallObjectOverhead
+ MemorySizes.Align(MemorySizes.ArrayOverhead + _data.Length)
+ MemorySizes.Align(sizeof(int));

private readonly CappedArray<byte> _data;

public ref readonly CappedArray<byte> Data => ref _data;

public RlpFactory(in CappedArray<byte> data)
{
_data = data;
}

public ValueRlpStream GetRlpStream()
{
return new(in _data);
}
}
}
8 changes: 1 addition & 7 deletions src/Nethermind/Nethermind.Serialization.Rlp/RlpStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class RlpStream
private static readonly LogEntryDecoder _logEntryDecoder = LogEntryDecoder.Instance;

private readonly CappedArray<byte> _data;
private int _position = 0;

protected RlpStream()
{
Expand All @@ -52,11 +53,6 @@ public RlpStream(in CappedArray<byte> data)
_data = data;
}

public RlpStream Clone()
{
return new(_data);
}

public void Encode(Block value)
{
_blockDecoder.Encode(this, value);
Expand Down Expand Up @@ -188,8 +184,6 @@ public virtual void Write(IReadOnlyList<byte> bytesToWrite)

public ref readonly CappedArray<byte> Data => ref _data;

private int _position = 0;

public virtual int Position
{
get
Expand Down
Loading

0 comments on commit 3d20677

Please sign in to comment.