Description
MemoryPool<T> offers some advantages over ArrayPool<T> (which it uses under the hood) when you need clearly declare who "owns" the item and is responsible for returning it to the pool. This is because you pass around IMemoryOwner<T> (which is IDisposable) which helps to make ownership clear.
However, it does have performance penalties compared to using the ArrayPool<T>:
- Allocating the IMemoryOwner<T> on the heap for each rental
- On older frameworks, it is frequently necessary to use MemoryMarshal to extract the backing array so it may be forwarded to other methods that don't have Span<T> overloads, such as Stream.Write.
In places where the extra advantages of clear memory ownership don't apply, we should use the ArrayPool<T> instead.
Attachments
For Gerrit Dashboard: NCBC-3261 | ||||||
---|---|---|---|---|---|---|
# | Subject | Branch | Project | Status | CR | V |
179553,4 | NCBC-3261: Where possible use ArrayPool instead of MemoryPool | master | couchbase-net-client | Status: MERGED | +2 | +1 |