Skip to content

Commit 372e6e7

Browse files
authored
Change ValueRlpStream to be Span based (#7961)
1 parent 7c4a5be commit 372e6e7

File tree

3 files changed

+25
-27
lines changed

3 files changed

+25
-27
lines changed

src/Nethermind/Nethermind.Serialization.Rlp/ValueRlpStream.cs

+22-24
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,27 @@ namespace Nethermind.Serialization.Rlp;
1515

1616
public ref struct ValueRlpStream(in CappedArray<byte> data)
1717
{
18-
private readonly ref readonly CappedArray<byte> _data = ref data;
18+
public readonly ReadOnlySpan<byte> Data = data.AsSpan();
1919
private int _position = 0;
2020

2121
internal readonly string Description =>
22-
Data.AsSpan(0, Math.Min(Rlp.DebugMessageContentLength, Length)).ToHexString() ?? "0x";
23-
24-
public readonly ref readonly CappedArray<byte> Data => ref _data;
22+
Data[..Math.Min(Rlp.DebugMessageContentLength, Data.Length)].ToHexString() ?? "0x";
2523

2624
public int Position
2725
{
2826
readonly get => _position;
2927
set => _position = value;
3028
}
3129

32-
public readonly bool IsNull => Unsafe.IsNullRef(ref Unsafe.AsRef(in _data));
30+
public readonly bool IsNull => Unsafe.IsNullRef(ref MemoryMarshal.GetReference(Data));
3331
public readonly bool IsNotNull => !IsNull;
3432
public readonly int Length => Data.Length;
3533

3634
public int PeekNumberOfItemsRemaining(int? beforePosition = null, int maxSearch = int.MaxValue)
3735
{
38-
int positionStored = Position;
36+
int positionStored = _position;
3937
int numberOfItems = 0;
40-
while (Position < (beforePosition ?? Length))
38+
while (_position < (beforePosition ?? Data.Length))
4139
{
4240
int prefix = ReadByte();
4341
if (prefix <= 128)
@@ -61,7 +59,7 @@ public int PeekNumberOfItemsRemaining(int? beforePosition = null, int maxSearch
6159
}
6260
else
6361
{
64-
Position--;
62+
_position--;
6563
int sequenceLength = ReadSequenceLength();
6664
SkipBytes(sequenceLength);
6765
}
@@ -73,7 +71,7 @@ public int PeekNumberOfItemsRemaining(int? beforePosition = null, int maxSearch
7371
}
7472
}
7573

76-
Position = positionStored;
74+
_position = positionStored;
7775
return numberOfItems;
7876
}
7977

@@ -157,7 +155,7 @@ public int ReadSequenceLength()
157155
if (prefix < 192)
158156
{
159157
throw new RlpException(
160-
$"Expected a sequence prefix to be in the range of <192, 255> and got {prefix} at position {Position} in the message of length {Length} starting with {Description}");
158+
$"Expected a sequence prefix to be in the range of <192, 255> and got {prefix} at position {_position} in the message of length {Data.Length} starting with {Description}");
161159
}
162160

163161
if (prefix <= 247)
@@ -271,22 +269,22 @@ static void ThrowArgumentOutOfRangeException(int lengthOfLength)
271269

272270
public byte ReadByte()
273271
{
274-
return Data![_position++];
272+
return Data[_position++];
275273
}
276274

277275
public readonly byte PeekByte()
278276
{
279-
return Data![_position];
277+
return Data[_position];
280278
}
281279

282280
public void SkipBytes(int length)
283281
{
284282
_position += length;
285283
}
286284

287-
public Span<byte> Read(int length)
285+
public ReadOnlySpan<byte> Read(int length)
288286
{
289-
Span<byte> data = Data.AsSpan(_position, length);
287+
ReadOnlySpan<byte> data = Data.Slice(_position, length);
290288
_position += length;
291289
return data;
292290
}
@@ -302,10 +300,10 @@ public Span<byte> Read(int length)
302300
if (prefix != 128 + 32)
303301
{
304302
throw new RlpException(
305-
$"Unexpected prefix of {prefix} when decoding {nameof(Hash256)} at position {Position} in the message of length {Length} starting with {Description}");
303+
$"Unexpected prefix of {prefix} when decoding {nameof(Hash256)} at position {_position} in the message of length {Data.Length} starting with {Description}");
306304
}
307305

308-
Span<byte> keccakSpan = Read(32);
306+
ReadOnlySpan<byte> keccakSpan = Read(32);
309307
if (keccakSpan.SequenceEqual(Keccak.OfAnEmptyString.Bytes))
310308
{
311309
return Keccak.OfAnEmptyString;
@@ -331,28 +329,28 @@ public bool DecodeValueKeccak(out ValueHash256 keccak)
331329
if (prefix != 128 + 32)
332330
{
333331
throw new RlpException(
334-
$"Unexpected prefix of {prefix} when decoding {nameof(Hash256)} at position {Position} in the message of length {Length} starting with {Description}");
332+
$"Unexpected prefix of {prefix} when decoding {nameof(Hash256)} at position {_position} in the message of length {Data.Length} starting with {Description}");
335333
}
336334

337-
Span<byte> keccakSpan = Read(32);
335+
ReadOnlySpan<byte> keccakSpan = Read(32);
338336
keccak = new ValueHash256(keccakSpan);
339337
return true;
340338
}
341339

342-
public readonly Span<byte> PeekNextItem()
340+
public readonly ReadOnlySpan<byte> PeekNextItem()
343341
{
344342
int length = PeekNextRlpLength();
345343
return Peek(length);
346344
}
347345

348-
public readonly Span<byte> Peek(int length)
346+
public readonly ReadOnlySpan<byte> Peek(int length)
349347
{
350348
return Peek(0, length);
351349
}
352350

353-
public readonly Span<byte> Peek(int offset, int length)
351+
public readonly ReadOnlySpan<byte> Peek(int offset, int length)
354352
{
355-
return Data.AsSpan(_position + offset, length);
353+
return Data.Slice(_position + offset, length);
356354
}
357355

358356
public byte[] DecodeByteArray()
@@ -450,7 +448,7 @@ public void SkipItem()
450448

451449
public void Reset()
452450
{
453-
Position = 0;
451+
_position = 0;
454452
}
455453

456454
private const byte EmptyArrayByte = 128;
@@ -459,6 +457,6 @@ public void Reset()
459457

460458
public override readonly string ToString()
461459
{
462-
return $"[{nameof(RlpStream)}|{Position}/{Length}]";
460+
return $"[{nameof(RlpStream)}|{_position}/{Data.Length}]";
463461
}
464462
}

src/Nethermind/Nethermind.Trie/TrieNode.Decoder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ private static void WriteChildrenRlpBranchRlp(ITrieNodeResolver tree, ref TreePa
368368
if (data is null)
369369
{
370370
int length = rlpStream.PeekNextRlpLength();
371-
Span<byte> nextItem = rlpStream.Data.AsSpan(rlpStream.Position, length);
371+
ReadOnlySpan<byte> nextItem = rlpStream.Data.Slice(rlpStream.Position, length);
372372
nextItem.CopyTo(destination.Slice(position, nextItem.Length));
373373
position += nextItem.Length;
374374
rlpStream.SkipBytes(length);

src/Nethermind/Nethermind.Trie/TrieNode.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1242,7 +1242,7 @@ private void SeekChildNotNull(ref ValueRlpStream rlpStream, int itemToSetOn)
12421242
default:
12431243
{
12441244
rlpStream.Position--;
1245-
Span<byte> fullRlp = rlpStream.PeekNextItem();
1245+
ReadOnlySpan<byte> fullRlp = rlpStream.PeekNextItem();
12461246
TrieNode child = new(NodeType.Unknown, fullRlp.ToArray());
12471247
data = childOrRef = child;
12481248
break;
@@ -1309,7 +1309,7 @@ private void ResolveAllChildBranch(ITrieNodeResolver tree, ref TreePath path, Tr
13091309
}
13101310
default:
13111311
{
1312-
Span<byte> fullRlp = rlpStream.PeekNextItem();
1312+
ReadOnlySpan<byte> fullRlp = rlpStream.PeekNextItem();
13131313
TrieNode child = new(NodeType.Unknown, fullRlp.ToArray());
13141314
rlpStream.SkipItem();
13151315
output[i] = child;

0 commit comments

Comments
 (0)