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

Parallel Trie Branch KeyGeneration #7048

Merged
merged 7 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Serialization.Rlp/Rlp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,8 @@ public static int Encode(Span<byte> buffer, int position, Hash256 hash)
ThrowArgumentOutOfRangeException();
}

Unsafe.Add(ref MemoryMarshal.GetReference(buffer), position) = 160;
Unsafe.As<byte, ValueHash256>(ref Unsafe.Add(ref MemoryMarshal.GetReference(buffer), position + 1)) = hash.ValueHash256;
Unsafe.Add(ref MemoryMarshal.GetReference(buffer), (nuint)position) = 160;
Unsafe.As<byte, ValueHash256>(ref Unsafe.Add(ref MemoryMarshal.GetReference(buffer), (nuint)position + 1)) = hash.ValueHash256;
benaadams marked this conversation as resolved.
Show resolved Hide resolved
return newPosition;

[DoesNotReturn]
Expand Down
102 changes: 100 additions & 2 deletions src/Nethermind/Nethermind.Trie/TrieNode.Decoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Nethermind.Core.Buffers;
using Nethermind.Core.Crypto;
using Nethermind.Serialization.Rlp;
Expand Down Expand Up @@ -125,12 +127,12 @@ private static void ThrowNullKey(TrieNode node)
throw new TrieException($"Hex prefix of a leaf node is null at node {node.Keccak}");
}

public static CappedArray<byte> RlpEncodeBranch(TrieNode item, ITrieNodeResolver tree, ref TreePath path, ICappedArrayPool? pool)
public static CappedArray<byte> RlpEncodeBranch(TrieNode item, ITrieNodeResolver tree, ref TreePath path, ICappedArrayPool? pool, bool canBeParallel)
{
Metrics.TreeNodeRlpEncodings++;

int valueRlpLength = AllowBranchValues ? Rlp.LengthOf(item.Value.AsSpan()) : 1;
int contentLength = valueRlpLength + GetChildrenRlpLengthForBranch(tree, ref path, item, pool);
int contentLength = valueRlpLength + (canBeParallel ? GetChildrenRlpLengthForBranchParallel(tree, ref path, item, pool) : GetChildrenRlpLengthForBranch(tree, ref path, item, pool));
int sequenceLength = Rlp.LengthOfSequence(contentLength);
CappedArray<byte> result = pool.SafeRentBuffer(sequenceLength);
Span<byte> resultSpan = result.AsSpan();
Expand Down Expand Up @@ -163,6 +165,55 @@ private static int GetChildrenRlpLengthForBranch(ITrieNodeResolver tree, ref Tre
}
}

private static int GetChildrenRlpLengthForBranchParallel(ITrieNodeResolver tree, ref TreePath path, TrieNode item, ICappedArrayPool? bufferPool)
{
item.EnsureInitialized();
// Tail call optimized.
if (item.HasRlp)
{
return GetChildrenRlpLengthForBranchRlpParallel(tree, path, item, bufferPool);
}
else
{
return GetChildrenRlpLengthForBranchNonRlpParallel(tree, path, item, bufferPool);
}
benaadams marked this conversation as resolved.
Show resolved Hide resolved
}

private static int GetChildrenRlpLengthForBranchNonRlpParallel(ITrieNodeResolver tree, TreePath rootPath, TrieNode item, ICappedArrayPool bufferPool)
{
int totalLength = 0;
Parallel.For(0, BranchesCount,
() => 0,
(i, state, local) =>
benaadams marked this conversation as resolved.
Show resolved Hide resolved
benaadams marked this conversation as resolved.
Show resolved Hide resolved
{
object data = item._data[i];
benaadams marked this conversation as resolved.
Show resolved Hide resolved
if (ReferenceEquals(data, _nullNode) || data is null)
{
local++;
}
else if (data is Hash256)
{
local += Rlp.LengthOfKeccakRlp;
}
else
{
TreePath path = rootPath;
path.AppendMut(i);
TrieNode childNode = (TrieNode)data;
childNode.ResolveKey(tree, ref path, isRoot: false, bufferPool: bufferPool);
path.TruncateOne();
local += childNode.Keccak is null ? childNode.FullRlp.Length : Rlp.LengthOfKeccakRlp;
}

return local;
},
local =>
{
Interlocked.Add(ref totalLength, local);
});
return totalLength;
}

private static int GetChildrenRlpLengthForBranchNonRlp(ITrieNodeResolver tree, ref TreePath path, TrieNode item, ICappedArrayPool bufferPool)
{
int totalLength = 0;
Expand All @@ -189,6 +240,53 @@ private static int GetChildrenRlpLengthForBranchNonRlp(ITrieNodeResolver tree, r
return totalLength;
}

private static int GetChildrenRlpLengthForBranchRlpParallel(ITrieNodeResolver tree, TreePath rootPath, TrieNode item, ICappedArrayPool? bufferPool)
{
int totalLength = 0;
Parallel.For(0, BranchesCount,
() => 0,
(i, state, local) =>
{
ValueRlpStream rlpStream = item.RlpStream;
item.SeekChild(ref rlpStream, i);
object data = item._data[i];
if (data is null)
{
int length = rlpStream.PeekNextRlpLength();
local += length;
rlpStream.SkipBytes(length);
}
else
{
if (ReferenceEquals(data, _nullNode) || data is null)
benaadams marked this conversation as resolved.
Show resolved Hide resolved
{
local++;
}
else if (data is Hash256)
{
local += Rlp.LengthOfKeccakRlp;
}
else
{
TreePath path = rootPath;
path.AppendMut(i);
Debug.Assert(data is TrieNode, "Data is not TrieNode");
TrieNode childNode = Unsafe.As<TrieNode>(data);
childNode.ResolveKey(tree, ref path, isRoot: false, bufferPool: bufferPool);
path.TruncateOne();
local += childNode.Keccak is null ? childNode.FullRlp.Length : Rlp.LengthOfKeccakRlp;
}
}

return local;
},
local =>
{
Interlocked.Add(ref totalLength, local);
});
return totalLength;
}

private static int GetChildrenRlpLengthForBranchRlp(ITrieNodeResolver tree, ref TreePath path, TrieNode item, ICappedArrayPool? bufferPool)
{
int totalLength = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.Trie/TrieNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ public void ResolveKey(ITrieNodeResolver tree, ref TreePath path, bool isRoot, I
{
ref readonly CappedArray<byte> oldRlp = ref rlp is not null ? ref rlp.Data : ref CappedArray<byte>.Empty;
CappedArray<byte> fullRlp = NodeType == NodeType.Branch ?
TrieNodeDecoder.RlpEncodeBranch(this, tree, ref path, bufferPool) :
TrieNodeDecoder.RlpEncodeBranch(this, tree, ref path, bufferPool, canBeParallel: isRoot) :
RlpEncode(tree, ref path, bufferPool);

if (fullRlp.IsNotNullOrEmpty)
Expand All @@ -536,7 +536,7 @@ internal CappedArray<byte> RlpEncode(ITrieNodeResolver tree, ref TreePath path,
{
return NodeType switch
{
NodeType.Branch => TrieNodeDecoder.RlpEncodeBranch(this, tree, ref path, bufferPool),
NodeType.Branch => TrieNodeDecoder.RlpEncodeBranch(this, tree, ref path, bufferPool, canBeParallel: false),
NodeType.Extension => TrieNodeDecoder.EncodeExtension(this, tree, ref path, bufferPool),
NodeType.Leaf => TrieNodeDecoder.EncodeLeaf(this, bufferPool),
_ => ThrowUnhandledNodeType(this)
Expand Down