Skip to content

Commit

Permalink
Merge pull request #3634 from greymistcube/optimize/copystates
Browse files Browse the repository at this point in the history
⚡ Optimize `TrieStateStore.CopyStates()`
  • Loading branch information
greymistcube authored Jan 26, 2024
2 parents 1eb547e + e2f6e75 commit 02a6bed
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ Version 3.9.6

To be released.

- (Libplanet.Store) Optimized `TrieStateStore.CopyStates()` to greatly
reduce the amount of memory used. [[#3634]]

[#3634]: https://github.com/planetarium/libplanet/pull/3634


Version 3.9.5
-------------
Expand Down
16 changes: 8 additions & 8 deletions Libplanet.Store/Trie/MerkleTrie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,18 +201,18 @@ internal IEnumerable<HashDigest<SHA256>> IterateHashNodes()
yield break;
}

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

while (queue.Count > 0)
while (stack.Count > 0)
{
INode node = queue.Dequeue();
INode node = stack.Pop();
if (node is HashNode dequeuedHashNode)
{
var storedKey = new KeyBytes(dequeuedHashNode.HashDigest.ByteArray);
var storedValue = KeyValueStore.Get(storedKey);
var intermediateEncoding = _codec.Decode(storedValue);
queue.Enqueue(
stack.Push(
NodeDecoder.Decode(
intermediateEncoding,
NodeDecoder.HashEmbeddedNodeType) ??
Expand All @@ -229,21 +229,21 @@ internal IEnumerable<HashDigest<SHA256>> IterateHashNodes()
INode? child = fullNode.Children[index];
if (child is HashNode childHashNode)
{
queue.Enqueue(childHashNode);
stack.Push(childHashNode);
}
}

if (fullNode.Value is HashNode fullNodeValueHashNode)
{
queue.Enqueue(fullNodeValueHashNode);
stack.Push(fullNodeValueHashNode);
}

break;

case ShortNode shortNode:
if (shortNode.Value is HashNode shortNodeValueHashNode)
{
queue.Enqueue(shortNodeValueHashNode);
stack.Push(shortNodeValueHashNode);
}

break;
Expand Down

0 comments on commit 02a6bed

Please sign in to comment.