1

I understand that the ArrayPool class was implemented to pool arrays only.

Can I use the ArrayPool implementation to pool objects (not arrays)? I don't see why not, except that it is a bit weird that it might return an array larger than what I requested.

I was thinking about something like this:

public class Worker
{
    ArrayPool<Channel> channels;

    public Worker() 
    {
        channels = ArrayPool<Channel>.Shared;
    }

    public async Task ExecuteSomething(string message)
    {
        // It gets a bit weird here...
        var rentedChannels = channels.Rent(1);
        await rentedChannels[0].DoWork();
        channels.Return(rentedChannels);
    }
}

For convenience, here is the link to the implementation of ArrayPool<T>.

El Mac
  • 3,256
  • 7
  • 38
  • 58
  • `ArrayPool` is an abstract class. Your sample code wouldn't compile. You implement it however you want. – Daniel Mann Aug 29 '19 at 21:38
  • Like @DanielMann, you did not implement it correctly. Also, for objects, you need to use a different type. See the following https://stackoverflow.com/questions/2510975/c-sharp-object-pooling-pattern-implementation – William Xifaras Oct 22 '19 at 16:50

1 Answers1

0

It seems what you are looking for is perhaps the ObjectPool<T>

See example https://learn.microsoft.com/en-us/aspnet/core/performance/objectpool?view=aspnetcore-3.0

Hope it helps!

Stephen Lautier
  • 3,065
  • 3
  • 17
  • 20