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

Propagate trie data via readonly refs #6626

Merged
merged 16 commits into from
Jan 29, 2024
Merged
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