Open universorum opened 10 months ago
Tagging subscribers to this area: @dotnet/area-system-buffers See info in area-owners.md if you want to be subscribed.
Author: | universorum |
---|---|
Assignees: | - |
Labels: | `api-suggestion`, `area-System.Buffers` |
Milestone: | - |
This proposal looks good to me as-is.
@jkotas Can you think of any risks worth citing on this?
MemoryPool<T>.Rent
cannot be abstract. It needs to be virtual with default implementation to avoid breaking existing concrete user implementations inheriting from this type.bool
argument controls whether the buffer gets cleared when it is returned back to the pool, not when it is rented from the pool as the API signature may suggest.clearArray
flag to avoid wasting cycles by clearing space in the pooled buffer that was never used. One example from many: https://github.com/dotnet/runtime/blob/48aaca56dd4add823c6fbf0fd14a8f625d339b33/src/libraries/System.Linq/src/System/Linq/SegmentedArrayBuilder.cs#L76-L79
Background and motivation
We already have
ArrayPool<T>.Return(T[] array, boolclearArray = false)
to require the ArrayPool to clear all data in the array before other callers can reuse it. But MemoryPool does not have a similar API to require MemoryPool to clear data. Now, we need to wrap the array into memory and return it, or write 0 to the array to make the same effect.In the case of
MemoryPool<T>.Shared.Rent
, we want to return the memory automatically. So the only thing we can do is to explicitly require it in the MemoryPool.Shared.Rent method.API Proposal
API Usage
Alternative Designs
No response
Risks
No response