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

⚡ Optimized ITrie.IterateNodes() #3687

Merged
merged 3 commits into from
Feb 27, 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
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ To be released.

### Behavioral changes

- (Libplanet.Store) Optimized `ITrie.IterateNodes()` to greatly
reduce the amount of memory used. [[#3687]]

### Bug fixes

### Dependencies
Expand All @@ -45,6 +48,7 @@ To be released.
[#3644]: https://github.com/planetarium/libplanet/pull/3644
[#3651]: https://github.com/planetarium/libplanet/pull/3651
[#3669]: https://github.com/planetarium/libplanet/pull/3669
[#3687]: https://github.com/planetarium/libplanet/pull/3687


Version 4.0.6
Expand Down
35 changes: 13 additions & 22 deletions Libplanet.Store/Trie/MerkleTrie.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Security.Cryptography;
using Bencodex;
using Bencodex.Types;
using Libplanet.Common;
using Libplanet.Crypto;
using Libplanet.Store.Trie.Nodes;

namespace Libplanet.Store.Trie
Expand Down Expand Up @@ -138,53 +136,46 @@ public ITrie Remove(in KeyBytes key)
yield break;
}

var queue = new Queue<(Nibbles, INode)>();
queue.Enqueue((Nibbles.Empty, Root));
var stack = new Stack<(Nibbles, INode)>();
stack.Push((Nibbles.Empty, Root));

while (queue.Count > 0)
while (stack.Count > 0)
{
(Nibbles path, INode node) = queue.Dequeue();
(Nibbles path, INode node) = stack.Pop();
yield return (path, node);
switch (node)
{
case FullNode fullNode:
foreach (int index in Enumerable.Range(0, FullNode.ChildrenCount - 1))
{
INode? child = fullNode.Children[index];
if (!(child is null))
if (fullNode.Children[index] is { } childNode)
{
queue.Enqueue((path.Add((byte)index), child));
stack.Push((path.Add((byte)index), childNode));
}
}

if (!(fullNode.Value is null))
if (fullNode.Value is { } fullNodeValue)
{
queue.Enqueue((path, fullNode.Value));
stack.Push((path, fullNodeValue));
}

break;

case ShortNode shortNode:
if (!(shortNode.Value is null))
if (shortNode.Value is { } shortNodeValue)
{
queue.Enqueue((path.AddRange(shortNode.Key), shortNode.Value));
stack.Push((path.AddRange(shortNode.Key), shortNodeValue));
}

break;

case HashNode hashNode:
queue.Enqueue((path, UnhashNode(hashNode)));
stack.Push((path, UnhashNode(hashNode)));
break;
}
}
}

internal IEnumerable<HashDigest<SHA256>> IterateHashNodes()
{
return IterateNodes().Where(pair => pair.Node is HashNode)
.Select(pair => ((HashNode)pair.Node).HashDigest);
}

/// <summary>
/// Iterates over <see cref="KeyBytes"/> and <see cref="byte[]"/> pairs stored
/// necessary to fully represent this <see cref="ITrie"/>.
Expand All @@ -207,9 +198,9 @@ internal IEnumerable<HashDigest<SHA256>> IterateHashNodes()
while (stack.Count > 0)
{
INode node = stack.Pop();
if (node is HashNode dequeuedHashNode)
if (node is HashNode poppedHashNode)
{
var storedKey = new KeyBytes(dequeuedHashNode.HashDigest.ByteArray);
var storedKey = new KeyBytes(poppedHashNode.HashDigest.ByteArray);
var storedValue = KeyValueStore.Get(storedKey);
var intermediateEncoding = _codec.Decode(storedValue);
stack.Push(
Expand Down
Loading