Skip to content

Commit

Permalink
Propagate trie data via readonly refs (#6626)
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams authored Jan 29, 2024
1 parent c9a1bfa commit 61ff62e
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 163 deletions.
9 changes: 7 additions & 2 deletions src/Nethermind/Nethermind.Core/Buffers/CappedArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public readonly struct CappedArray<T>
private readonly static CappedArray<T> _empty = new CappedArray<T>(Array.Empty<T>());
public static ref readonly CappedArray<T> Null => ref _null;
public static ref readonly CappedArray<T> Empty => ref _empty;
public static object NullBoxed { get; } = _null;
public static object EmptyBoxed { get; } = _empty;

private readonly T[]? _array;
private readonly int _length;
Expand Down Expand Up @@ -82,8 +84,11 @@ public readonly Span<T> AsSpan(int start, int length)

public readonly T[]? ToArray()
{
if (_array is null) return null;
if (_length == _array?.Length) return _array;
T[]? array = _array;

if (array is null) return null;
if (array.Length == 0) return Array.Empty<T>();
if (_length == array.Length) return array;
return AsSpan().ToArray();
}
}
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.State/PartialStorageProviderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected PartialStorageProviderBase(ILogManager? logManager)
/// <returns>Value at cell</returns>
public ReadOnlySpan<byte> Get(in StorageCell storageCell)
{
return GetCurrentValue(storageCell);
return GetCurrentValue(in storageCell);
}

/// <summary>
Expand All @@ -53,7 +53,7 @@ public ReadOnlySpan<byte> Get(in StorageCell storageCell)
/// <param name="newValue">Value to store</param>
public void Set(in StorageCell storageCell, byte[] newValue)
{
PushUpdate(storageCell, newValue);
PushUpdate(in storageCell, newValue);
}

/// <summary>
Expand Down
10 changes: 5 additions & 5 deletions src/Nethermind/Nethermind.Trie.Test/TrieNodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -520,14 +520,14 @@ public void Can_encode_branch_with_unresolved_children()
public void Size_of_a_heavy_leaf_is_correct()
{
Context ctx = new();
Assert.That(ctx.HeavyLeaf.GetMemorySize(false), Is.EqualTo(224));
Assert.That(ctx.HeavyLeaf.GetMemorySize(false), Is.EqualTo(248));
}

[Test]
public void Size_of_a_tiny_leaf_is_correct()
{
Context ctx = new();
Assert.That(ctx.TiniestLeaf.GetMemorySize(false), Is.EqualTo(144));
Assert.That(ctx.TiniestLeaf.GetMemorySize(false), Is.EqualTo(168));
}

[Test]
Expand All @@ -541,7 +541,7 @@ public void Size_of_a_branch_is_correct()
node.SetChild(i, ctx.AccountLeaf);
}

Assert.That(node.GetMemorySize(true), Is.EqualTo(3664));
Assert.That(node.GetMemorySize(true), Is.EqualTo(4048));
Assert.That(node.GetMemorySize(false), Is.EqualTo(208));
}

Expand All @@ -564,7 +564,7 @@ public void Size_of_unknown_node_is_correct()
trieNode.Key = new byte[] { 1 };
trieNode.SetChild(0, ctx.TiniestLeaf);

Assert.That(trieNode.GetMemorySize(true), Is.EqualTo(264));
Assert.That(trieNode.GetMemorySize(true), Is.EqualTo(288));
Assert.That(trieNode.GetMemorySize(false), Is.EqualTo(120));
}

Expand Down Expand Up @@ -603,7 +603,7 @@ public void Size_of_leaf_with_value()
{
TrieNode trieNode = new(NodeType.Leaf);
trieNode.Value = new byte[7];
trieNode.GetMemorySize(false).Should().Be(128);
trieNode.GetMemorySize(false).Should().Be(152);
}

[Test]
Expand Down
Loading

0 comments on commit 61ff62e

Please sign in to comment.