Can MemoryPool<byte>.Rent(int minBufferSize ) capable of returning a IMemoryOwner<byte> bigger than asked?

1k Views Asked by At

I understand that the internal buffer used by IMemoryOwner<byte>.Memory can be larger than asked. But, is IMemoryOwner<byte>.Memory.Length defined with what I asked or with the size of the internal buffer ? The document seems not accurate enough.

1

There are 1 best solutions below

5
canton7 On BEST ANSWER

Let's take a look.

MemoryPool<T>.Rent is abstract, so we'll go looking for an implementation. ArrayMemoryPool<T>.Rent looks like a good, representative candidate. That implementation looks like this:

public sealed override IMemoryOwner<T> Rent(int minimumBufferSize = -1)
{
    if (minimumBufferSize == -1)
        minimumBufferSize = 1 + (4095 / Unsafe.SizeOf<T>());
    else if (((uint)minimumBufferSize) > MaximumBufferSize)
        ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.minimumBufferSize);

    return new ArrayMemoryPoolBuffer(minimumBufferSize);
}

Let's chase that into ArrayMemoryPoolBuffer:

private sealed class ArrayMemoryPoolBuffer : IMemoryOwner<T>
{
    private T[]? _array;

    public ArrayMemoryPoolBuffer(int size)
    {
        _array = ArrayPool<T>.Shared.Rent(size);
    }

    public Memory<T> Memory
    {
        get
        {
            T[]? array = _array;
            if (array == null)
            {
                ThrowHelper.ThrowObjectDisposedException_ArrayMemoryPoolBuffer();
            }

            return new Memory<T>(array);
        }
    }

    public void Dispose()
    {
        T[]? array = _array;
        if (array != null)
        {
            _array = null;
            ArrayPool<T>.Shared.Return(array);
        }
    }
}

The new Memory<T>(array) means that Memory<T>.Length will just be the size of the underlying array: we don't have any logic to have a smaller Memory<T> wrapping a larger array. So let's see if ArrayPool<T>.Shared.Rent gives us back an array of the correct size...

Again this is abstract, but we can find an implementation at ConfigurableArrayPool<T>. The meat of that method is:

int index = Utilities.SelectBucketIndex(minimumLength);
if (index < _buckets.Length)
{
    // Search for an array starting at the 'index' bucket. If the bucket is empty, bump up to the
    // next higher bucket and try that one, but only try at most a few buckets.
    const int MaxBucketsToTry = 2;
    int i = index;
    do
    {
        // Attempt to rent from the bucket.  If we get a buffer from it, return it.
        buffer = _buckets[i].Rent();
        if (buffer != null)
        {
            if (log.IsEnabled())
            {
                log.BufferRented(buffer.GetHashCode(), buffer.Length, Id, _buckets[i].Id);
            }
            return buffer;
        }
    }
    while (++i < _buckets.Length && i != index + MaxBucketsToTry);

    // The pool was exhausted for this buffer size.  Allocate a new buffer with a size corresponding
    // to the appropriate bucket.
    buffer = new T[_buckets[index]._bufferLength];
}
else
{
    // The request was for a size too large for the pool.  Allocate an array of exactly the requested length.
    // When it's returned to the pool, we'll simply throw it away.
    buffer = new T[minimumLength];
}

We can see that the pool has multiple buckets, with each bucket containing arrays of a specified size. This method is finding the bucket of arrays which are just larger than the requested size; if that bucket is empty it goes looking at larger buckets. If it fails to find anything, it creates a new array with the bucket size; only if the requested size is larger than the pool can manage does it create an array of the exact requested size.

So it's very unlikely that the array underlying the Memory<T> has the requested size, and the construction of the Memory<T> does nothing to pretend that the Memory<T> is smaller than its underlying array.

The conclusion is that IMemoryOwner<byte>.Memory.Length can indeed be larger than requested.