-
Notifications
You must be signed in to change notification settings - Fork 1
Add ArrayPool<T>.RentOwner() extension returning IMemoryOwner<T> #23
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
95229ef
feat: add ArrayPool.RentOwner() extension returning IMemoryOwner
Copilot fcfed9b
fix: guard ArrayPoolMemoryOwner against double disposal
Copilot c2cd023
Update ArrayPoolExtensionsTests.cs
Tyrrrz 71ce773
Update PowerKit.Tests/ArrayPoolExtensionsTests.cs
Tyrrrz f8156f2
fix: throw ObjectDisposedException from Memory getter after disposal;…
Copilot a9d443d
refactor: use FluentAssertions Throw pattern in RentOwner_MemoryAfter…
Copilot 8698835
Update ArrayPoolExtensionsTests.cs
Tyrrrz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,88 @@ | ||
| using System; | ||
| using System.Buffers; | ||
| using FluentAssertions; | ||
| using PowerKit.Extensions; | ||
| using Xunit; | ||
|
|
||
| namespace PowerKit.Tests; | ||
|
|
||
| public class ArrayPoolExtensionsTests | ||
| { | ||
| [Fact] | ||
| public void RentOwner_Test() | ||
| { | ||
| // Arrange | ||
| var pool = ArrayPool<byte>.Shared; | ||
|
|
||
| // Act | ||
| using var owner = pool.RentOwner(16); | ||
|
|
||
| // Assert | ||
| owner.Memory.Length.Should().Be(16); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void RentOwner_Dispose_Test() | ||
| { | ||
| // Arrange | ||
| var pool = new TrackingArrayPool(); | ||
|
|
||
| // Act & assert | ||
| using (var owner = pool.RentOwner(16)) | ||
| { | ||
| owner.Memory.Length.Should().Be(16); | ||
| pool.LastRentedArray.Should().NotBeNull(); | ||
| } | ||
|
|
||
| pool.ReturnCallCount.Should().Be(1); | ||
| pool.LastReturnedArray.Should().BeSameAs(pool.LastRentedArray); | ||
|
|
||
| // Should re-rent the returned array after the previous owner was disposed | ||
| using var owner2 = pool.RentOwner(16); | ||
| owner2.Memory.Length.Should().Be(16); | ||
| pool.LastRentedArray.Should().BeSameAs(pool.LastReturnedArray); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void RentOwner_MemoryAfterDispose_Test() | ||
| { | ||
| // Arrange | ||
| var pool = ArrayPool<byte>.Shared; | ||
| var owner = pool.RentOwner(16); | ||
|
|
||
| // Act | ||
| owner.Dispose(); | ||
|
|
||
| // Assert | ||
| Assert.Throws<ObjectDisposedException>(() => owner.Memory); | ||
| } | ||
|
|
||
| private sealed class TrackingArrayPool : ArrayPool<byte> | ||
| { | ||
| private byte[]? available; | ||
|
|
||
| public byte[]? LastRentedArray { get; private set; } | ||
|
|
||
| public byte[]? LastReturnedArray { get; private set; } | ||
|
|
||
| public int ReturnCallCount { get; private set; } | ||
|
|
||
| public override byte[] Rent(int minimumLength) | ||
| { | ||
| var array = this.available is { Length: >= 16 } | ||
| ? this.available | ||
| : new byte[16]; | ||
|
|
||
| this.available = null; | ||
| this.LastRentedArray = array; | ||
| return array; | ||
| } | ||
|
|
||
| public override void Return(byte[] array, bool clearArray = false) | ||
| { | ||
| this.ReturnCallCount++; | ||
| this.LastReturnedArray = array; | ||
| this.available = array; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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,44 @@ | ||
| using System; | ||
| using System.Buffers; | ||
| using System.Threading; | ||
|
|
||
| namespace PowerKit.Extensions; | ||
|
|
||
| internal static class ArrayPoolExtensions | ||
| { | ||
| extension<T>(ArrayPool<T> pool) | ||
| { | ||
| /// <summary> | ||
| /// Rents a buffer of at least <paramref name="minimumLength" /> elements from the pool | ||
| /// and wraps it in an <see cref="IMemoryOwner{T}" /> that returns the buffer to the pool | ||
| /// when disposed. | ||
| /// </summary> | ||
| public IMemoryOwner<T> RentOwner(int minimumLength = 1) => | ||
| new ArrayPoolMemoryOwner<T>(pool, pool.Rent(minimumLength), minimumLength); | ||
| } | ||
| } | ||
|
|
||
| file sealed class ArrayPoolMemoryOwner<T>(ArrayPool<T> pool, T[] buffer, int minimumLength) | ||
| : IMemoryOwner<T> | ||
| { | ||
| private int _disposed; | ||
|
|
||
| public Memory<T> Memory | ||
| { | ||
| get | ||
| { | ||
| ObjectDisposedException.ThrowIf(_disposed != 0, this); | ||
| return buffer.AsMemory(0, minimumLength); | ||
| } | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| if (Interlocked.Exchange(ref _disposed, 1) != 0) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| pool.Return(buffer); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.