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

Batch overloads using an array pool #856

Merged
merged 35 commits into from
Nov 13, 2022
Merged
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
bdaabf0
Add "Batch" overloads to get pooled buckets
atifaziz Oct 20, 2022
4d8464b
Refactor to return buckets cursor
atifaziz Oct 20, 2022
123a12e
Rename batch bucket into more general list view
atifaziz Oct 27, 2022
b49d27d
Add test to assert in-place updates
atifaziz Oct 27, 2022
ff023a2
Use test pool to assert rentals/returns
atifaziz Oct 27, 2022
9fb0a91
Make helper static
atifaziz Oct 27, 2022
c2d98fa
Fix code formatting
atifaziz Oct 27, 2022
851cde0
Test last return to the pool
atifaziz Oct 27, 2022
fa81c48
Fix disposal bugs
atifaziz Oct 27, 2022
02c1c1c
Test disposal of source
atifaziz Oct 27, 2022
8701b88
Review name and virtual members
atifaziz Oct 29, 2022
b2c3e02
Add overload with current list query that returns a sequence
atifaziz Oct 29, 2022
5d45266
Keep just the array pool version
atifaziz Oct 29, 2022
0a4a3a7
Fix type parameter doc comment
atifaziz Oct 29, 2022
aec464c
Split current list from its provider
atifaziz Oct 29, 2022
3175ce9
Retain just the simpler overload publicly
atifaziz Oct 29, 2022
07e7ce6
Add simpler overload
atifaziz Oct 29, 2022
1e70366
Add test to assert in-place updates
atifaziz Oct 29, 2022
55daf66
Fix current list provider implementation name
atifaziz Oct 29, 2022
cca8965
Call query selector before iterating source
atifaziz Oct 29, 2022
ff226ef
Rename query to bucket
atifaziz Oct 29, 2022
70bb45b
Rename query to bucket projection
atifaziz Oct 29, 2022
e707297
Revise conditional constants
atifaziz Oct 29, 2022
d61ec03
Remove extra blank line
atifaziz Oct 29, 2022
61dbc84
Remove "AsSpan" from current list
atifaziz Oct 29, 2022
41fcf74
Update overload count in read-me doc
atifaziz Oct 30, 2022
b555c3d
Reuse "Enumerable.Empty" enumerator
atifaziz Oct 31, 2022
be1f763
Remove "AsSpan" from current list abstract
atifaziz Oct 31, 2022
d1cd8cc
Fix doc about bucket streaming/buffering
atifaziz Oct 31, 2022
0fc8901
Rename list in current list types to buffer
atifaziz Oct 31, 2022
03b8112
Merge remote-tracking branch 'upstream/master' into batch-pool
atifaziz Nov 3, 2022
43f7582
Expand "bucketProjectionSelector" doc
atifaziz Nov 12, 2022
2356f60
Merge remote-tracking branch 'upstream/master' into batch-pool
atifaziz Nov 12, 2022
bdf76a7
Reword remarks note to read clearer.
atifaziz Nov 12, 2022
ec82d1b
Reword remarks note to read clearer.
atifaziz Nov 12, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use test pool to assert rentals/returns
  • Loading branch information
atifaziz committed Oct 27, 2022
commit ff023a224f5668c00f2ef19cfe4243ecd7bb28ef
80 changes: 78 additions & 2 deletions MoreLinq.Test/BatchTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,89 @@ from n in result
public class BatchPooledArrayTest : BatchPoolTest
{
protected override IListView<T> Batch<T>(IEnumerable<T> source, int size) =>
source.Batch(size, ArrayPool<T>.Create());
source.Batch(size, new TestArrayPool<T>());
}

public class BatchPooledMemoryTest : BatchPoolTest
{
protected override IListView<T> Batch<T>(IEnumerable<T> source, int size) =>
source.Batch(size, MemoryPool<T>.Shared);
source.Batch(size, new TestMemoryPool<T>(new TestArrayPool<T>()));

sealed class TestMemoryPool<T> : MemoryPool<T>
{
readonly ArrayPool<T> _pool;

public TestMemoryPool(ArrayPool<T> pool) => _pool = pool;

protected override void Dispose(bool disposing) { } // NOP

public override IMemoryOwner<T> Rent(int minBufferSize = -1) =>
minBufferSize >= 0
? new MemoryOwner(_pool, _pool.Rent(minBufferSize))
: throw new NotSupportedException();

public override int MaxBufferSize =>
// https://github.com/dotnet/runtime/blob/v7.0.0-rc.2.22472.3/src/libraries/System.Memory/src/System/Buffers/ArrayMemoryPool.cs#L10
2_147_483_591;

sealed class MemoryOwner : IMemoryOwner<T>
{
ArrayPool<T> _pool;
T[] _rental;

public MemoryOwner(ArrayPool<T> pool, T[] rental) =>
(_pool, _rental) = (pool, rental);

public Memory<T> Memory => _rental is { } rental ? new Memory<T>(rental)
: throw new ObjectDisposedException(null);

public void Dispose()
{
if (_rental is { } array && _pool is { } pool)
{
_rental = null;
_pool = null;
pool.Return(array);
}
}
}
}
}

/// <summary>
/// An <see cref="ArrayPool{T}"/> implementation for testing purposes that holds only
/// one array in the pool.
/// </summary>

sealed class TestArrayPool<T> : ArrayPool<T>
{
T[] _pooledArray;
T[] _rentedArray;

public override T[] Rent(int minimumLength)
{
if (_pooledArray is null && _rentedArray is null)
_pooledArray = new T[minimumLength * 2];

if (_pooledArray is null)
throw new InvalidOperationException("The pool is exhausted.");

(_pooledArray, _rentedArray) = (null, _pooledArray);

return _rentedArray;
}

public override void Return(T[] array, bool clearArray = false)
{
if (_rentedArray is null)
throw new InvalidOperationException("Cannot return when nothing has been rented from this pool.");

if (array != _rentedArray)
throw new InvalidOperationException("Cannot return what has not been rented from this pool.");

_pooledArray = array;
_rentedArray = null;
}
}
}

Expand Down