Skip to content

Commit b39aa77

Browse files
committed
Added ObjectPool
1 parent 3b86e43 commit b39aa77

File tree

6 files changed

+110
-0
lines changed

6 files changed

+110
-0
lines changed

ObjectPool/ObjectPool.cs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Collections.Concurrent;
2+
3+
public class ObjectPool<T>
4+
{
5+
private readonly ConcurrentBag<T> _objects;
6+
private readonly Func<T> _objectGenerator;
7+
8+
/// <summary>
9+
/// Initializes the ObjectPool.
10+
/// </summary>
11+
/// <param name="objectGenerator">We need a generator function to create an object if our pool is empty.</param>
12+
public ObjectPool(Func<T> objectGenerator)
13+
{
14+
_objectGenerator = objectGenerator ?? throw new ArgumentNullException(nameof(objectGenerator));
15+
_objects = new ConcurrentBag<T>();
16+
}
17+
18+
public T Rent() => _objects.TryTake(out T item) ? item : _objectGenerator();
19+
20+
public void Return(T item) => _objects.Add(item);
21+
}

ObjectPool/ObjectPool.csproj

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
12+
</ItemGroup>
13+
14+
</Project>

ObjectPool/ObjectPool.sln

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ObjectPool", "ObjectPool.csproj", "{0954C875-44A3-415E-844D-21593DB67D41}"
4+
EndProject
5+
Global
6+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7+
Debug|Any CPU = Debug|Any CPU
8+
Release|Any CPU = Release|Any CPU
9+
EndGlobalSection
10+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
11+
{0954C875-44A3-415E-844D-21593DB67D41}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
12+
{0954C875-44A3-415E-844D-21593DB67D41}.Debug|Any CPU.Build.0 = Debug|Any CPU
13+
{0954C875-44A3-415E-844D-21593DB67D41}.Release|Any CPU.ActiveCfg = Release|Any CPU
14+
{0954C875-44A3-415E-844D-21593DB67D41}.Release|Any CPU.Build.0 = Release|Any CPU
15+
EndGlobalSection
16+
EndGlobal

ObjectPool/Program.cs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using BenchmarkDotNet.Attributes;
2+
using BenchmarkDotNet.Running;
3+
4+
BenchmarkRunner.Run<PoolBenchmark>();
5+
6+
[MemoryDiagnoser]
7+
public class PoolBenchmark
8+
{
9+
private const int LoopCount = 10;
10+
11+
[Benchmark(Baseline = true)]
12+
public int CreateNewObjects()
13+
{
14+
var sum = 0;
15+
for (var i = 0; i < LoopCount; i++)
16+
{
17+
var obj = new MyHeavyClass();
18+
sum += obj.Length;
19+
}
20+
21+
return sum;
22+
}
23+
24+
[Benchmark]
25+
public int ObjectPool()
26+
{
27+
var pool = new ObjectPool<MyHeavyClass>(() => new MyHeavyClass());
28+
var sum = 0;
29+
for (var i = 0; i < LoopCount; i++)
30+
{
31+
var obj = pool.Rent();
32+
sum += obj.Length;
33+
pool.Return(obj);
34+
}
35+
36+
return sum;
37+
}
38+
}
39+
40+
public class MyHeavyClass
41+
{
42+
private readonly double[] _someArray;
43+
44+
public MyHeavyClass()
45+
{
46+
_someArray = new double[5000];
47+
Array.Fill(_someArray, 10);
48+
}
49+
50+
public int Length => _someArray.Length;
51+
}

ObjectPool/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# ObjectPool - Rent and return some instances
2+
3+
Just imagine a car pool: There is a dealer which bought the car and lent's it to you. After a while you will return this car where you got it from. Much like that works an **ObjectPool** in C#. You can rent an expensive object from the pool and when you are done with it, you just return it. Sounds beautiful, doesn't it?
4+
5+
Let's explore the advantages and disadvantages of a **ObjectPool**s and how they work.
6+
7+
Found [here](https://steven-giesel.com/blogPost/389e5983-9726-448d-a693-1f7379165c4d)

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Contains all of my examples from various blog posts. You can find a comprehensiv
44

55
| BlogPost | Publish Date |
66
| ---------------------------------------------------------------------------------------- | ------------ |
7+
| [ObjectPool - Rent and return some instances](ObjectPool/) | 02.06.2022 |
78
| [Blazor with an RSS Feed](BlazorRSSFeed/) | 30.05.2022 |
89
| [How to unit test a RavenDB](RavenDBUnitTest/) | 25.05.2022 |
910
| [Blazor with CancellationToken support](BlazorCancellation/) | 18.05.2022 |

0 commit comments

Comments
 (0)