Skip to content

Commit

Permalink
Merge pull request #3573 from greymistcube/chore/fullnode-optimization
Browse files Browse the repository at this point in the history
⚡ ♻️ Slightly optimized `FullNode`
  • Loading branch information
greymistcube authored Dec 27, 2023
2 parents 7d27880 + ec9a0fb commit faa7b7f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ be compatible with this version, specifically, those that ran with
`MaxGasPrice` and `GasLimit`. [[#3567]]
- (Libplanet.Store) Changed `ShortNode` to no longer inherit `BaseNode`.
`ShortNode.Value` is no longer nullable. [[#3572]]
- (Libplanet.Store) Removed `FullNode()` and added `FullNode.Empty`.
[[#3573]]
- (Libplanet.Store) Slightly optimized `ITrie` performance. [[#3573]]

[#3559]: https://github.com/planetarium/libplanet/pull/3559
[#3560]: https://github.com/planetarium/libplanet/pull/3560
Expand All @@ -40,6 +43,7 @@ be compatible with this version, specifically, those that ran with
[#3564]: https://github.com/planetarium/libplanet/pull/3564
[#3567]: https://github.com/planetarium/libplanet/pull/3567
[#3572]: https://github.com/planetarium/libplanet/pull/3572
[#3573]: https://github.com/planetarium/libplanet/pull/3573


Version 3.9.2
Expand Down
4 changes: 2 additions & 2 deletions Libplanet.Store/Trie/MerkleTrie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ private INode InsertToValueNode(ValueNode valueNode, PathCursor cursor, ValueNod
{
if (cursor.RemainingAnyNibbles)
{
return new FullNode()
return FullNode.Empty
.SetChild(FullNode.ChildrenCount - 1, valueNode)
.SetChild(cursor.NextNibble, InsertToNullNode(cursor.Next(1), value));
}
Expand Down Expand Up @@ -370,7 +370,7 @@ private INode InsertToShortNode(ShortNode shortNode, in PathCursor cursor, Value
}
else
{
FullNode fullNode = new FullNode();
FullNode fullNode = FullNode.Empty;
byte newChildIndex = shortNode.Key[commonNibbles.Length];
Nibbles newShortNodeKey = shortNode.Key.Skip(commonNibbles.Length + 1);

Expand Down
8 changes: 3 additions & 5 deletions Libplanet.Store/Trie/Nodes/FullNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public sealed class FullNode : BaseNode, IEquatable<FullNode>
// Children 0x10 + Value 0x1
public const byte ChildrenCount = 0x11;

public static readonly FullNode Empty =
new FullNode(new INode?[ChildrenCount].ToImmutableArray());

public FullNode(ImmutableArray<INode?> children)
: base(children[ChildrenCount - 1])
{
Expand All @@ -22,11 +25,6 @@ public FullNode(ImmutableArray<INode?> children)
Children = children;
}

public FullNode()
: this(new INode?[ChildrenCount].ToImmutableArray())
{
}

public ImmutableArray<INode?> Children { get; }

public FullNode SetChild(int index, INode childNode)
Expand Down
2 changes: 1 addition & 1 deletion Libplanet.Tests/Store/Trie/Nodes/NodeDecoderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void OnlyDecodeAllowedNodeType()
IValue shortNodeEncoded = new ShortNode(
Nibbles.FromHex("b4"),
new ValueNode(new Text("bar"))).ToBencodex();
IValue fullNodeEncoded = new FullNode()
IValue fullNodeEncoded = FullNode.Empty
.SetChild(4, new ValueNode(new Text("4")))
.SetChild(10, new ValueNode(new Text("c")))
.ToBencodex();
Expand Down

0 comments on commit faa7b7f

Please sign in to comment.