CodeProject.ObjectPool.MicrosoftExtensionsAdapter 5.0.3

A generic, concurrent, portable and flexible Object Pool for .NET. This package contains an adapter for Microsoft.Extensions.ObjectPool abstraction.

Install-Package CodeProject.ObjectPool.MicrosoftExtensionsAdapter -Version 5.0.3
dotnet add package CodeProject.ObjectPool.MicrosoftExtensionsAdapter --version 5.0.3
<PackageReference Include="CodeProject.ObjectPool.MicrosoftExtensionsAdapter" Version="5.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CodeProject.ObjectPool.MicrosoftExtensionsAdapter --version 5.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: CodeProject.ObjectPool.MicrosoftExtensionsAdapter, 5.0.3"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install CodeProject.ObjectPool.MicrosoftExtensionsAdapter as a Cake Addin
#addin nuget:?package=CodeProject.ObjectPool.MicrosoftExtensionsAdapter&version=5.0.3

// Install CodeProject.ObjectPool.MicrosoftExtensionsAdapter as a Cake Tool
#tool nuget:?package=CodeProject.ObjectPool.MicrosoftExtensionsAdapter&version=5.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Object Pool

License: MIT
Donate
standard-readme compliant
Gitlab pipeline status
Code coverage
NuGet version
NuGet downloads
Renovate enabled

A generic, concurrent, portable and flexible Object Pool for .NET,
completely based on the Code Project article of Ofir Makmal.

Library is feature complete and no further development is planned on this project,
except for routine maintenance and bug fixes.

Original source code has been modified, in order to introduce:

  • A Parameterized Object Pool, already drafted by Ofir Makmal in the comments of the article.
  • A Timed Object Pool, where objects are automatically removed after a period of inactivity.
  • Memory Stream and String Builder pools.
    • For use cases which might involve very large memory streams, I suggest to use the
      Microsoft.IO.RecyclableMemoryStream library
      from Microsoft. It eliminates Large Object Heap allocations, which this library does not do.

Moreover, a few unit tests have been added, in order to improve code reliability,
and a lot of other small changes have also been applied.

Of course, all modified source code is freely available in this repository.

Many thanks to Ofir Makmal for his great work.

Table of Contents

Install

NuGet package CodeProject.ObjectPool is available for download:

dotnet add package CodeProject.ObjectPool

An adapter for Microsoft.Extensions.ObjectPool is also available on NuGet:

dotnet add package CodeProject.ObjectPool.MicrosoftExtensionsAdapter

Usage

Quick and dirty example:

/// <summary>
///   Example usages of ObjectPool.
/// </summary>
internal static class Program
{
    /// <summary>
    ///   Example usages of ObjectPool.
    /// </summary>
    private static void Main()
    {
        // Creating a pool with a maximum size of 25, using custom Factory method to create and
        // instance of ExpensiveResource.
        var pool = new ObjectPool<ExpensiveResource>(25, () => new ExpensiveResource(/* resource specific initialization */));

        using (var resource = pool.GetObject())
        {
            // Using the resource...
            resource.DoStuff();
        } // Exiting the using scope will return the object back to the pool.

        // Creating a pool with wrapper object for managing external resources, that is, classes
        // which cannot inherit from PooledObject.
        var newPool = new ObjectPool<PooledObjectWrapper<ExternalExpensiveResource>>(() =>
            new PooledObjectWrapper<ExternalExpensiveResource>(CreateNewResource())
            {
                OnReleaseResources = ExternalResourceReleaseResource,
                OnResetState = ExternalResourceResetState
            });

        using (var wrapper = newPool.GetObject())
        {
            // wrapper.InternalResource contains the object that you pooled.
            wrapper.InternalResource.DoOtherStuff();
        } // Exiting the using scope will return the object back to the pool.

        // Creates a pool where objects which have not been used for over 2 seconds will be
        // cleaned up by a dedicated thread.
        var timedPool = new TimedObjectPool<ExpensiveResource>(TimeSpan.FromSeconds(2));

        using (var resource = timedPool.GetObject())
        {
            // Using the resource...
            resource.DoStuff();
        } // Exiting the using scope will return the object back to the pool and record last usage.

        Console.WriteLine($"Timed pool size after 0 seconds: {timedPool.ObjectsInPoolCount}"); // Should be 1
        Thread.Sleep(TimeSpan.FromSeconds(4));
        Console.WriteLine($"Timed pool size after 4 seconds: {timedPool.ObjectsInPoolCount}"); // Should be 0

        // Adapts a timed pool to Microsoft Extensions abstraction.
        var mPool = ObjectPoolAdapter.CreateForPooledObject(timedPool);

        // Sample usage of Microsoft pool.
        var mResource = mPool.Get();
        Debug.Assert(mResource is ExpensiveResource);
        mPool.Return(mResource);

        // Adapts a new pool to Microsoft Extensions abstraction. This example shows how to adapt
        // when object type does not extend PooledObject.
        var mPool2 = ObjectPoolAdapter.Create(new ObjectPool<PooledObjectWrapper<MemoryStream>>(
            () => PooledObjectWrapper.Create(new MemoryStream())));

        // Sample usage of second Microsoft pool.
        var mResource2 = mPool2.Get();
        Debug.Assert(mResource2 is MemoryStream);
        mPool2.Return(mResource2);

        Console.Read();
    }

    private static ExternalExpensiveResource CreateNewResource()
    {
        return new ExternalExpensiveResource();
    }

    public static void ExternalResourceResetState(ExternalExpensiveResource resource)
    {
        // External Resource reset state code.
    }

    public static void ExternalResourceReleaseResource(ExternalExpensiveResource resource)
    {
        // External Resource release code.
    }
}

internal sealed class ExpensiveResource : PooledObject
{
    public ExpensiveResource()
    {
        OnReleaseResources = () =>
        {
            // Called if the resource needs to be manually cleaned before the memory is reclaimed.
        };

        OnResetState = () =>
        {
            // Called if the resource needs resetting before it is getting back into the pool.
        };
    }

    public void DoStuff()
    {
        // Do some work here, for example.
    }
}

internal sealed class ExternalExpensiveResource
{
    public void DoOtherStuff()
    {
        // Do some work here, for example.
    }
}

Async support

Starting from v4, Object Pool supports async pooled object initialization.
Therefore, objects can be retrieved in two ways:

obj = pool.GetObject();
obj = await pool.GetObjectAsync();

Those methods depend on the factory method specified during pool initialization.
Because making async factories "sync" is usually a problem, which can lead to deadlocks,
we have the following situation:

Factory type GetObject GeObjectAsync
Not specified OK OK, uses a result task
Sync OK OK, uses a result task
Async KO, throws an exception OK

So, to sum it up:

  • If a sync factory is specified, both retrieval methods can be used.
  • If an async factory is specified, only the async retrieval can be used.

Maintainers

@pomma89.

Contributing

PRs accepted.

Small note: If editing the README, please conform to the standard-readme specification.

License

MIT © 2013-2021 Alessio Parma

Object Pool

License: MIT
Donate
standard-readme compliant
Gitlab pipeline status
Code coverage
NuGet version
NuGet downloads
Renovate enabled

A generic, concurrent, portable and flexible Object Pool for .NET,
completely based on the Code Project article of Ofir Makmal.

Library is feature complete and no further development is planned on this project,
except for routine maintenance and bug fixes.

Original source code has been modified, in order to introduce:

  • A Parameterized Object Pool, already drafted by Ofir Makmal in the comments of the article.
  • A Timed Object Pool, where objects are automatically removed after a period of inactivity.
  • Memory Stream and String Builder pools.
    • For use cases which might involve very large memory streams, I suggest to use the
      Microsoft.IO.RecyclableMemoryStream library
      from Microsoft. It eliminates Large Object Heap allocations, which this library does not do.

Moreover, a few unit tests have been added, in order to improve code reliability,
and a lot of other small changes have also been applied.

Of course, all modified source code is freely available in this repository.

Many thanks to Ofir Makmal for his great work.

Table of Contents

Install

NuGet package CodeProject.ObjectPool is available for download:

dotnet add package CodeProject.ObjectPool

An adapter for Microsoft.Extensions.ObjectPool is also available on NuGet:

dotnet add package CodeProject.ObjectPool.MicrosoftExtensionsAdapter

Usage

Quick and dirty example:

/// <summary>
///   Example usages of ObjectPool.
/// </summary>
internal static class Program
{
    /// <summary>
    ///   Example usages of ObjectPool.
    /// </summary>
    private static void Main()
    {
        // Creating a pool with a maximum size of 25, using custom Factory method to create and
        // instance of ExpensiveResource.
        var pool = new ObjectPool<ExpensiveResource>(25, () => new ExpensiveResource(/* resource specific initialization */));

        using (var resource = pool.GetObject())
        {
            // Using the resource...
            resource.DoStuff();
        } // Exiting the using scope will return the object back to the pool.

        // Creating a pool with wrapper object for managing external resources, that is, classes
        // which cannot inherit from PooledObject.
        var newPool = new ObjectPool<PooledObjectWrapper<ExternalExpensiveResource>>(() =>
            new PooledObjectWrapper<ExternalExpensiveResource>(CreateNewResource())
            {
                OnReleaseResources = ExternalResourceReleaseResource,
                OnResetState = ExternalResourceResetState
            });

        using (var wrapper = newPool.GetObject())
        {
            // wrapper.InternalResource contains the object that you pooled.
            wrapper.InternalResource.DoOtherStuff();
        } // Exiting the using scope will return the object back to the pool.

        // Creates a pool where objects which have not been used for over 2 seconds will be
        // cleaned up by a dedicated thread.
        var timedPool = new TimedObjectPool<ExpensiveResource>(TimeSpan.FromSeconds(2));

        using (var resource = timedPool.GetObject())
        {
            // Using the resource...
            resource.DoStuff();
        } // Exiting the using scope will return the object back to the pool and record last usage.

        Console.WriteLine($"Timed pool size after 0 seconds: {timedPool.ObjectsInPoolCount}"); // Should be 1
        Thread.Sleep(TimeSpan.FromSeconds(4));
        Console.WriteLine($"Timed pool size after 4 seconds: {timedPool.ObjectsInPoolCount}"); // Should be 0

        // Adapts a timed pool to Microsoft Extensions abstraction.
        var mPool = ObjectPoolAdapter.CreateForPooledObject(timedPool);

        // Sample usage of Microsoft pool.
        var mResource = mPool.Get();
        Debug.Assert(mResource is ExpensiveResource);
        mPool.Return(mResource);

        // Adapts a new pool to Microsoft Extensions abstraction. This example shows how to adapt
        // when object type does not extend PooledObject.
        var mPool2 = ObjectPoolAdapter.Create(new ObjectPool<PooledObjectWrapper<MemoryStream>>(
            () => PooledObjectWrapper.Create(new MemoryStream())));

        // Sample usage of second Microsoft pool.
        var mResource2 = mPool2.Get();
        Debug.Assert(mResource2 is MemoryStream);
        mPool2.Return(mResource2);

        Console.Read();
    }

    private static ExternalExpensiveResource CreateNewResource()
    {
        return new ExternalExpensiveResource();
    }

    public static void ExternalResourceResetState(ExternalExpensiveResource resource)
    {
        // External Resource reset state code.
    }

    public static void ExternalResourceReleaseResource(ExternalExpensiveResource resource)
    {
        // External Resource release code.
    }
}

internal sealed class ExpensiveResource : PooledObject
{
    public ExpensiveResource()
    {
        OnReleaseResources = () =>
        {
            // Called if the resource needs to be manually cleaned before the memory is reclaimed.
        };

        OnResetState = () =>
        {
            // Called if the resource needs resetting before it is getting back into the pool.
        };
    }

    public void DoStuff()
    {
        // Do some work here, for example.
    }
}

internal sealed class ExternalExpensiveResource
{
    public void DoOtherStuff()
    {
        // Do some work here, for example.
    }
}

Async support

Starting from v4, Object Pool supports async pooled object initialization.
Therefore, objects can be retrieved in two ways:

obj = pool.GetObject();
obj = await pool.GetObjectAsync();

Those methods depend on the factory method specified during pool initialization.
Because making async factories "sync" is usually a problem, which can lead to deadlocks,
we have the following situation:

Factory type GetObject GeObjectAsync
Not specified OK OK, uses a result task
Sync OK OK, uses a result task
Async KO, throws an exception OK

So, to sum it up:

  • If a sync factory is specified, both retrieval methods can be used.
  • If an async factory is specified, only the async retrieval can be used.

Maintainers

@pomma89.

Contributing

PRs accepted.

Small note: If editing the README, please conform to the standard-readme specification.

License

MIT © 2013-2021 Alessio Parma

Release Notes

https://gitlab.com/pomma89/object-pool/-/releases

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
5.0.3 39 6/13/2021
5.0.2 131 12/26/2020
5.0.1 177 11/14/2020
5.0.0 144 11/1/2020
4.0.2 974 2/23/2020
4.0.1 311 6/22/2019
3.2.4 286 6/13/2019
3.2.3 287 5/12/2019
3.2.2 4,478 10/28/2017
3.2.1 583 9/30/2017