-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[API Proposal]: ExactSizeMemoryPool<T> to rent out IMemoryOwner<T> with exact sizes #111203
Comments
Tagging subscribers to this area: @dotnet/area-system-buffers |
Similar APIs were also suggested in #30165 |
This sounds like you could just implement your own public class ExactMemoryPoolWrapper<T>(MemoryPool<T> inner) : MemoryPool<T>
{
public override int MaxBufferSize => inner.MaxBufferSize;
public override IMemoryOwner<T> Rent(int minBufferSize)
{
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(minBufferSize);
IMemoryOwner<T> temp = inner.Rent(minBufferSize);
return new MemoryOwner(temp, minBufferSize);
}
protected override void Dispose(bool disposing)
{
if (disposing)
inner.Dispose();
}
private sealed class MemoryOwner(IMemoryOwner<T> inner, int size) : IMemoryOwner<T>
{
public Memory<T> Memory => inner.Memory[..size];
public void Dispose()
{
inner.Dispose();
}
}
} |
From an implementation point of view, yes. But without an additional API, consumers of the Another issue is that it would not allow |
Background and motivation
MemoryPool<T>
rents outIMemoryOwner<T>
s with indeterminate sizes. In many cases this is a nuisance, especially in scenarios where we need to pass theIMemoryOwner
around to other components that consume the data. In such cases it necessitates passing around the length of valid data as well which complicates usage.Given that most (all?) implementations of
MemoryPool
can actually rent outIMemoryOwner
of exact sizes with minimal performance impact (by embedding a length in theIMemoryOwner
and slicing the memory to the exact length), it might be desirable to introduce anExactSizeMemoryPool<T>
to superclass these implementations. Consumers who prefer exact size pools can useExactSizeMemoryPool
instead while existing consumers ofMemoryPool
stay unaffected.API Proposal
Most existing
MemoryPool
implementations should be able to subclassExactSizeMemoryPool
instead, with a bit of work. Implementations can also optionally overrideRent
to retain the previous behavior (renting out potentially larger buffers) if desired. For example the defaultArrayPool
-backedMemoryPool<T>.Shared
could become something like this:API Usage
Usages of
MemoryPool<T>
would be superseded byExactSizeMemoryPool<T>
in cases where memories of exact sizes are desired (which is probably the majority of cases in the wild).Alternative Designs
Another possibility is to add
RentExact
functionality directly toMemoryPool<T>
, and it can rent out decoratorIMemoryOwner<T>
s that does the slicing, producing exact size buffers. This means that existingMemoryPool
implementations do not need modification at all. The disadvantage is that it will add a layer of indirection for all usages of the exact size buffers by default.Risks
No response
The text was updated successfully, but these errors were encountered: