-
Notifications
You must be signed in to change notification settings - Fork 434
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Progress * Capped array * Unit test passed * Should work now * Tracking all buffer * Separate the pool from the trie resolver * Cleanup * fix wrong calculate root * Micro optimize * Cleanup and receipts root also * More tests * Directly store array if possible * Cleanup * Test pool * Whitespace * Minor optimization on peek * Fix peek * Skip and read position without property * Reduced memory usage * Fix missed tests * Ensure non nullable * More unit tests * Cleanup * More cleanup * Minor optimization * More cleanup * Fix minor mistake
- Loading branch information
Showing
34 changed files
with
551 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/Nethermind/Nethermind.Core.Test/Buffers/CappedArrayTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using System.Linq; | ||
using FluentAssertions; | ||
using Nethermind.Core.Buffers; | ||
using NUnit.Framework; | ||
|
||
namespace Nethermind.Core.Test.Buffers; | ||
|
||
public class CappedArrayTests | ||
{ | ||
[Test] | ||
public void WhenGivenNullArray_IsNull_ShouldReturnTrue() | ||
{ | ||
CappedArray<byte> array = new(null); | ||
array.IsNull.Should().BeTrue(); | ||
} | ||
|
||
[Test] | ||
public void WhenGivenNullArray_AsSpan_ShouldReturnEmpty() | ||
{ | ||
CappedArray<byte> array = new(null); | ||
array.AsSpan().IsEmpty.Should().BeTrue(); | ||
array.AsSpan().Length.Should().Be(0); | ||
array.Length.Should().Be(0); | ||
} | ||
|
||
[Test] | ||
public void WhenGivenArray_AndLengthIsSame_ToArray_ShouldReturnSameArray() | ||
{ | ||
int[] baseArray = Enumerable.Range(0, 10).ToArray(); | ||
CappedArray<int> array = new(baseArray); | ||
array.IsUncapped.Should().BeTrue(); | ||
array.IsNull.Should().BeFalse(); | ||
array.IsNotNull.Should().BeTrue(); | ||
array.Length.Should().Be(10); | ||
array.ToArray().Should().BeSameAs(baseArray); | ||
} | ||
|
||
[Test] | ||
public void WhenGivenArray_AndLengthIsLess_ToArray_ShouldBeCapped() | ||
{ | ||
int[] baseArray = Enumerable.Range(0, 10).ToArray(); | ||
CappedArray<int> array = new(baseArray, 5); | ||
array.IsUncapped.Should().BeFalse(); | ||
array.IsNull.Should().BeFalse(); | ||
array.IsNotNull.Should().BeTrue(); | ||
array.Length.Should().Be(5); | ||
array.ToArray().Should().BeEquivalentTo(baseArray[..5]); | ||
array.AsSpan().Length.Should().Be(5); | ||
array.AsSpan().ToArray().Should().BeEquivalentTo(baseArray[..5]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using System; | ||
using Nethermind.Core.Crypto; | ||
using Nethermind.Core.Extensions; | ||
|
||
namespace Nethermind.Core.Buffers; | ||
|
||
/// <summary> | ||
/// Basically like ArraySegment, but only contain length, which reduces it size from 16byte to 12byte. Useful for | ||
/// polling memory where memory pool usually can't return exactly the same size of data. To conserve space, The | ||
/// underlying array can be null and this struct is meant to be non nullable, checking the `IsNull` property to check | ||
/// if it represent null. | ||
/// </summary> | ||
public struct CappedArray<T> | ||
{ | ||
private T[]? _array = null; | ||
private int _length = 0; | ||
|
||
public CappedArray(T[]? array, int length) | ||
{ | ||
_array = array; | ||
_length = length; | ||
} | ||
|
||
public CappedArray(T[]? array) | ||
{ | ||
if (array != null) | ||
{ | ||
_array = array; | ||
_length = array.Length; | ||
} | ||
} | ||
|
||
public static implicit operator ReadOnlySpan<T>(CappedArray<T> array) | ||
{ | ||
return array.AsSpan(); | ||
} | ||
|
||
public static implicit operator CappedArray<T>(T[]? array) | ||
{ | ||
if (array == null) return new CappedArray<T>(null); | ||
return new CappedArray<T>(array); | ||
} | ||
|
||
public int Length | ||
{ | ||
get => _length; | ||
set => _length = value; | ||
} | ||
|
||
public T[]? Array => _array; | ||
public bool IsUncapped => _length == _array?.Length; | ||
public bool IsNull => _array is null; | ||
public bool IsNotNull => _array is not null; | ||
|
||
public Span<T> AsSpan() | ||
{ | ||
return _array.AsSpan()[..Length]; | ||
} | ||
|
||
public T[]? ToArray() | ||
{ | ||
if (_array is null) return null; | ||
if (_length == _array?.Length) return _array; | ||
return AsSpan().ToArray(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Nethermind/Nethermind.Core/Buffers/ICappedArrayPool.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
namespace Nethermind.Core.Buffers; | ||
|
||
public interface ICappedArrayPool | ||
{ | ||
CappedArray<byte> Rent(int size); | ||
|
||
void Return(CappedArray<byte> buffer); | ||
} | ||
|
||
public static class BufferPoolExtensions | ||
{ | ||
public static CappedArray<byte> SafeRentBuffer(this ICappedArrayPool? pool, int size) | ||
{ | ||
if (pool == null) return new CappedArray<byte>(new byte[size]); | ||
return pool.Rent(size); | ||
} | ||
|
||
public static void SafeReturnBuffer(this ICappedArrayPool? pool, CappedArray<byte> buffer) | ||
{ | ||
pool?.Return(buffer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.