Skip to content

Commit cd0bac7

Browse files
author
Oren (electricessence)
committed
Updates and extensions.
1 parent effb9ab commit cd0bac7

File tree

5 files changed

+122
-48
lines changed

5 files changed

+122
-48
lines changed

benchmarking/Benchmark.cs

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -26,65 +26,63 @@ public Benchmark(uint size, uint repeat, Func<IObjectPool<T>> poolFactory) : bas
2626

2727
protected override IEnumerable<TimedResult> TestOnceInternal()
2828
{
29-
using (var pool = Param())
30-
{
31-
if (pool == null) throw new NullReferenceException();
32-
//yield return TimedResult.Measure("Take From Empty (In Parallel)", () =>
33-
//{
34-
// Parallel.For(0, TestSize, i => _items[i] = pool.Take());
35-
//});
29+
using var pool = Param();
30+
if (pool == null) throw new NullReferenceException();
31+
//yield return TimedResult.Measure("Take From Empty (In Parallel)", () =>
32+
//{
33+
// Parallel.For(0, TestSize, i => _items[i] = pool.Take());
34+
//});
3635

37-
yield return TimedResult.Measure("Give To (In Parallel)", () =>
38-
{
39-
// ReSharper disable once AccessToDisposedClosure
40-
Parallel.For(0, TestSize, i => pool.Give(_items[i]));
36+
yield return TimedResult.Measure("Give To (In Parallel)", () =>
37+
{
38+
// ReSharper disable once AccessToDisposedClosure
39+
Parallel.For(0, TestSize, i => pool.Give(_items[i]));
4140
#if DEBUG
42-
var count = pool.Count;
43-
Debug.Assert(pool is OptimisticArrayObjectPool<T> || count == TestSize, $"Expected {TestSize}, acutal count: {count}");
41+
var count = pool.Count;
42+
Debug.Assert(pool is OptimisticArrayObjectPool<T> || count == TestSize, $"Expected {TestSize}, acutal count: {count}");
4443
#endif
45-
});
44+
});
4645

47-
yield return TimedResult.Measure("Mixed 90%-Take/10%-Give (In Parallel)", () =>
46+
yield return TimedResult.Measure("Mixed 90%-Take/10%-Give (In Parallel)", () =>
47+
{
48+
Parallel.For(0, TestSize, i =>
4849
{
49-
Parallel.For(0, TestSize, i =>
50-
{
51-
if (i % 10 == 0)
52-
pool.Give(_items[i]);
53-
else
54-
_items[i] = pool.Take();
55-
});
50+
if (i % 10 == 0)
51+
pool.Give(_items[i]);
52+
else
53+
_items[i] = pool.Take();
5654
});
55+
});
5756

58-
yield return TimedResult.Measure("Mixed 50%-Take/50%-Give (In Parallel)", () =>
57+
yield return TimedResult.Measure("Mixed 50%-Take/50%-Give (In Parallel)", () =>
58+
{
59+
Parallel.For(0, TestSize, i =>
5960
{
60-
Parallel.For(0, TestSize, i =>
61-
{
62-
if (i % 2 == 0)
63-
_items[i] = pool.Take();
64-
else
65-
pool.Give(_items[i]);
66-
});
61+
if (i % 2 == 0)
62+
_items[i] = pool.Take();
63+
else
64+
pool.Give(_items[i]);
6765
});
66+
});
6867

69-
yield return TimedResult.Measure("Mixed 10%-Take/90%-Give (In Parallel)", () =>
68+
yield return TimedResult.Measure("Mixed 10%-Take/90%-Give (In Parallel)", () =>
69+
{
70+
Parallel.For(0, TestSize, i =>
7071
{
71-
Parallel.For(0, TestSize, i =>
72-
{
73-
if (i % 10 == 0)
74-
_items[i] = pool.Take();
75-
else
76-
pool.Give(_items[i]);
77-
});
72+
if (i % 10 == 0)
73+
_items[i] = pool.Take();
74+
else
75+
pool.Give(_items[i]);
7876
});
77+
});
7978

80-
yield return TimedResult.Measure("Empty Pool (.TryTake())", () =>
79+
yield return TimedResult.Measure("Empty Pool (.TryTake())", () =>
80+
{
81+
while (pool.TryTake() != null)
8182
{
82-
while (pool.TryTake() != null)
83-
{
84-
// remaining++;
85-
}
86-
});
87-
}
83+
// remaining++;
84+
}
85+
});
8886
}
8987

9088
public static TimedResult[] Results(uint size, uint repeat, Func<IObjectPool<T>> poolFactory)

benchmarking/Open.Disposable.ObjectPools.Benchmarking.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>netcoreapp3.0</TargetFramework>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
66
<RootNamespace>Open.Disposable</RootNamespace>
77
<Version>1.0.0</Version>
88
</PropertyGroup>

source/IObjectPool.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Diagnostics.CodeAnalysis;
44
using System.Diagnostics.Contracts;
5+
using System.Threading.Tasks;
56

67
namespace Open.Disposable
78
{
@@ -96,5 +97,80 @@ public static void Give<T>(this IObjectPool<T> target, T item1, T item2, params
9697
target.Give(items);
9798
}
9899

100+
/// <summary>
101+
/// Provides an item from the pool and returns it when the action sucessfully completes.
102+
/// </summary>
103+
/// <typeparam name="T">The object type from the pool.</typeparam>
104+
/// <param name="source">The object pool.</param>
105+
/// <param name="action">The action to execute.</param>
106+
public static void Rent<T>(this IObjectPool<T> source, Action<T> action)
107+
where T : class
108+
{
109+
if (source is null) throw new ArgumentNullException(nameof(source));
110+
if (action is null) throw new ArgumentNullException(nameof(action));
111+
Contract.EndContractBlock();
112+
113+
var item = source.Take();
114+
action(item);
115+
source.Give(item);
116+
}
117+
118+
/// <summary>
119+
/// Provides an item from the pool and returns it when the action sucessfully completes.
120+
/// </summary>
121+
/// <typeparam name="T">The object type from the pool.</typeparam>
122+
/// <param name="source">The object pool.</param>
123+
/// <param name="action">The action to execute.</param>
124+
/// <returns>The value from the action.</returns>
125+
public static TResult Rent<T, TResult>(this IObjectPool<T> source, Func<T, TResult> action)
126+
where T : class
127+
{
128+
if (source is null) throw new ArgumentNullException(nameof(source));
129+
if (action is null) throw new ArgumentNullException(nameof(action));
130+
Contract.EndContractBlock();
131+
132+
var item = source.Take();
133+
var result = action(item);
134+
source.Give(item);
135+
return result;
136+
}
137+
138+
/// <summary>
139+
/// Provides an item from the pool and returns it when the action sucessfully completes.
140+
/// </summary>
141+
/// <typeparam name="T">The object type from the pool.</typeparam>
142+
/// <param name="source">The object pool.</param>
143+
/// <param name="action">The action to execute.</param>
144+
public static async ValueTask RentAsync<T>(this IObjectPool<T> source, Func<T, ValueTask> action)
145+
where T : class
146+
{
147+
if (source is null) throw new ArgumentNullException(nameof(source));
148+
if (action is null) throw new ArgumentNullException(nameof(action));
149+
Contract.EndContractBlock();
150+
151+
var item = source.Take();
152+
await action(item);
153+
source.Give(item);
154+
}
155+
156+
/// <summary>
157+
/// Provides an item from the pool and returns it when the action sucessfully completes.
158+
/// </summary>
159+
/// <typeparam name="T">The object type from the pool.</typeparam>
160+
/// <param name="source">The object pool.</param>
161+
/// <param name="action">The action to execute.</param>
162+
/// <returns>The value from the action.</returns>
163+
public static async ValueTask<TResult> RentAsync<T, TResult>(this IObjectPool<T> source, Func<T, ValueTask<TResult>> action)
164+
where T : class
165+
{
166+
if (source is null) throw new ArgumentNullException(nameof(source));
167+
if (action is null) throw new ArgumentNullException(nameof(action));
168+
Contract.EndContractBlock();
169+
170+
var item = source.Take();
171+
var result = await action(item);
172+
source.Give(item);
173+
return result;
174+
}
99175
}
100176
}

source/Open.Disposable.ObjectPools.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<RepositoryType>git</RepositoryType>
1919
<PackageTags>objectpool, dotnet, dotnetcore, cs, idisposable, threadsafe, thread-safe</PackageTags>
2020
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
21-
<Version>2.4.0</Version>
21+
<Version>2.4.1</Version>
2222
<PackageReleaseNotes></PackageReleaseNotes>
2323
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2424
<Nullable>enable</Nullable>

source/Recycler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static Recycler<T> CreateRecycler<T>(
7070
=> new Recycler<T>(pool, recycleFunction, limit);
7171

7272
public static void Recycle(IRecyclable r)
73-
=>(r ?? throw new ArgumentNullException(nameof(r))).Recycle();
73+
=> (r ?? throw new ArgumentNullException(nameof(r))).Recycle();
7474

7575
public static Recycler<T> CreateRecycler<T>(
7676
this IObjectPool<T> pool,

0 commit comments

Comments
 (0)