Skip to content

Commit 44fffec

Browse files
Added common shared pools.
1 parent 3df93ff commit 44fffec

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,5 @@ __pycache__/
298298
*.btm.cs
299299
*.odx.cs
300300
*.xsd.cs
301+
.vscode/launch.json
302+
.vscode/tasks.json

source/.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ dotnet_diagnostic.CA1303.severity = silent
88

99
# CA1051: Do not declare visible instance fields
1010
dotnet_diagnostic.CA1051.severity = silent
11+
12+
# CA1000: Do not declare static members on generic types
13+
dotnet_diagnostic.CA1000.severity = silent

source/Open.Disposable.ObjectPools.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Part of the "Open" set of libraries.
1616
<RepositoryUrl>https://github.com/Open-NET-Libraries/Open.Disposable.ObjectPools/</RepositoryUrl>
1717
<RepositoryType>git</RepositoryType>
1818
<PackageTags>objectpool;idisposable;thread safe</PackageTags>
19-
<Version>2.5.0</Version>
19+
<Version>2.6.0</Version>
2020
<PackageReleaseNotes></PackageReleaseNotes>
2121
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2222
<PublishRepositoryUrl>true</PublishRepositoryUrl>

source/SharedPools.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Open.Disposable
6+
{
7+
public static class ListPool<T>
8+
{
9+
/// <summary>
10+
/// A shared object pool for use with lists.
11+
/// The list is cleared after being returned.
12+
/// The max size of the pool is 128 which should suffice for most use cases.
13+
/// </summary>
14+
public static readonly ConcurrentQueueObjectPool<List<T>> Shared = Create();
15+
16+
/// <summary>
17+
/// Creates an object pool for use with lists.
18+
/// The list is cleared after being returned.
19+
/// </summary>
20+
public static ConcurrentQueueObjectPool<List<T>> Create(int capacity = Constants.DEFAULT_CAPACITY) => new(() => new List<T>(), h =>
21+
{
22+
h.Clear();
23+
if (h.Capacity > 16) h.Capacity = 16;
24+
}, null, capacity);
25+
}
26+
27+
public static class HashSetPool<T>
28+
{
29+
/// <summary>
30+
/// A shared object pool for use with hash-sets.
31+
/// The hash-set is cleared after being returned.
32+
/// The max size of the pool is 128 which should suffice for most use cases.
33+
/// </summary>
34+
public static readonly ConcurrentQueueObjectPool<HashSet<T>> Shared = Create();
35+
36+
/// <summary>
37+
/// Creates an object pool for use with hash-sets.
38+
/// The hash-set is cleared after being returned.
39+
/// </summary>
40+
public static ConcurrentQueueObjectPool<HashSet<T>> Create(int capacity = Constants.DEFAULT_CAPACITY) => new(() => new HashSet<T>(), h => h.Clear(), null, capacity);
41+
}
42+
43+
public static class DictionaryPool<TKey, TValue>
44+
{
45+
/// <summary>
46+
/// A shared object pool for use with dictionaries.
47+
/// The dictionary is cleared after being returned.
48+
/// The max size of the pool is 128 which should suffice for most use cases.
49+
/// </summary>
50+
public static readonly ConcurrentQueueObjectPool<Dictionary<TKey, TValue>> Shared = Create();
51+
52+
/// <summary>
53+
/// Creates an object pooll for use with dictionaries.
54+
/// The dictionary is cleared after being returned.
55+
/// </summary>
56+
public static ConcurrentQueueObjectPool<Dictionary<TKey, TValue>> Create(int capacity = Constants.DEFAULT_CAPACITY) => new(() => new Dictionary<TKey, TValue>(), h => h.Clear(), null, capacity);
57+
58+
}
59+
}

0 commit comments

Comments
 (0)