Skip to content

Commit 7620933

Browse files
committed
improve pooling
1 parent bfaf2e7 commit 7620933

18 files changed

+540
-325
lines changed

ObjectPool.sln

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ VisualStudioVersion = 16.0.29509.3
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ObjectPool", "src\ObjectPool.csproj", "{AF5DE074-1BD0-4148-ABA0-CCF42F8523FC}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleConsole", "samples\SampleConsole\SampleConsole.csproj", "{E46FDC79-688D-4BC2-9BC5-EEC0C7DE1848}"
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleFrameworkConsole", "samples\SampleFrameworkConsole\SampleFrameworkConsole.csproj", "{CAD55CC3-64CB-48A0-90AE-32CB5E3ADF84}"
9+
EndProject
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleCoreConsole", "samples\SampleCoreConsole\SampleCoreConsole.csproj", "{45832EAE-20BD-4F18-AC56-1B3F952B80F7}"
911
EndProject
1012
Global
1113
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -17,10 +19,14 @@ Global
1719
{AF5DE074-1BD0-4148-ABA0-CCF42F8523FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
1820
{AF5DE074-1BD0-4148-ABA0-CCF42F8523FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
1921
{AF5DE074-1BD0-4148-ABA0-CCF42F8523FC}.Release|Any CPU.Build.0 = Release|Any CPU
20-
{E46FDC79-688D-4BC2-9BC5-EEC0C7DE1848}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21-
{E46FDC79-688D-4BC2-9BC5-EEC0C7DE1848}.Debug|Any CPU.Build.0 = Debug|Any CPU
22-
{E46FDC79-688D-4BC2-9BC5-EEC0C7DE1848}.Release|Any CPU.ActiveCfg = Release|Any CPU
23-
{E46FDC79-688D-4BC2-9BC5-EEC0C7DE1848}.Release|Any CPU.Build.0 = Release|Any CPU
22+
{CAD55CC3-64CB-48A0-90AE-32CB5E3ADF84}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{CAD55CC3-64CB-48A0-90AE-32CB5E3ADF84}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{CAD55CC3-64CB-48A0-90AE-32CB5E3ADF84}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{CAD55CC3-64CB-48A0-90AE-32CB5E3ADF84}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{45832EAE-20BD-4F18-AC56-1B3F952B80F7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{45832EAE-20BD-4F18-AC56-1B3F952B80F7}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{45832EAE-20BD-4F18-AC56-1B3F952B80F7}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{45832EAE-20BD-4F18-AC56-1B3F952B80F7}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE

samples/SampleConsole/Program.cs

Lines changed: 0 additions & 26 deletions
This file was deleted.

samples/SampleCoreConsole/Program.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using ObjectPool;
2+
using System;
3+
using System.Text;
4+
5+
namespace SampleConsole
6+
{
7+
class Program
8+
{
9+
10+
11+
public static IPool<StringBuilder> Pool { get; private set; }
12+
static void Main(string[] args)
13+
{
14+
15+
// var p = Microsoft.Extensions.ObjectPool.ObjectPool.Create<System.Threading.Tasks.Task>();
16+
var poolPolicy1 = new PoolPolicy<System.IO.MemoryStream>((poolInstance) => new System.IO.MemoryStream(), (jw) => jw.Position=0, 10, PooledItemInitialization.Return);
17+
var pool1 = new Pool<System.IO.MemoryStream>(poolPolicy1);
18+
19+
20+
var poolPolicy = new PoolPolicy<StringBuilder>((poolInstance) => new StringBuilder(), (jw) => jw.Clear(), 10, PooledItemInitialization.Return);
21+
//{
22+
// Factory = (poolInstance) => new StringBuilder(),
23+
// InitializationPolicy = PooledItemInitialization.Return,
24+
// MaximumPoolSize = 10,
25+
// ReinitializeObject = (jw) => jw.Clear()
26+
//};
27+
28+
Pool = new Pool<StringBuilder>(poolPolicy);
29+
30+
var sb= Pool.Get();
31+
var sb1 = Pool.Get();
32+
33+
var sb2 = Pool.Get();
34+
var sb3= Pool.Get();
35+
Pool.Return(sb1);
36+
var sb4 = Pool.Get();
37+
38+
for(var i = 0; i < 100; i++)
39+
{
40+
var z = Pool.Get();
41+
if (z.Length != 0)
42+
{
43+
throw new Exception();
44+
}
45+
z.Append("TEsting 12345");
46+
Pool.Return(z);
47+
}
48+
49+
}
50+
}
51+
}

samples/SampleConsole/SampleConsole.csproj renamed to samples/SampleCoreConsole/SampleCoreConsole.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
<TargetFramework>netcoreapp3.0</TargetFramework>
66
</PropertyGroup>
77

8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="3.0.0" />
10+
</ItemGroup>
11+
812
<ItemGroup>
913
<ProjectReference Include="..\..\src\ObjectPool.csproj" />
1014
</ItemGroup>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
5+
</startup>
6+
</configuration>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using ObjectPool;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace SampleFrameworkConsole
9+
{
10+
class Program
11+
{
12+
13+
public static IPool<StringBuilder> Pool { get; private set; }
14+
static void Main(string[] args)
15+
{
16+
17+
// var p = Microsoft.Extensions.ObjectPool.ObjectPool.Create<System.Threading.Tasks.Task>();
18+
var poolPolicy1 = new PoolPolicy<System.IO.MemoryStream>((poolInstance) => new System.IO.MemoryStream(), (jw) => jw.Position = 0, 10, PooledItemInitialization.Return);
19+
var pool1 = new Pool<System.IO.MemoryStream>(poolPolicy1);
20+
21+
22+
var poolPolicy = new PoolPolicy<StringBuilder>((poolInstance) => new StringBuilder(), (jw) => jw.Clear(), 10, PooledItemInitialization.Return);
23+
//{
24+
// Factory = (poolInstance) => new StringBuilder(),
25+
// InitializationPolicy = PooledItemInitialization.Return,
26+
// MaximumPoolSize = 10,
27+
// ReinitializeObject = (jw) => jw.Clear()
28+
//};
29+
30+
Pool = new Pool<StringBuilder>(poolPolicy);
31+
32+
var sb = Pool.Get();
33+
var sb1 = Pool.Get();
34+
35+
var sb2 = Pool.Get();
36+
var sb3 = Pool.Get();
37+
Pool.Return(sb1);
38+
var sb4 = Pool.Get();
39+
40+
for (var i = 0; i < 100; i++)
41+
{
42+
var z = Pool.Get();
43+
if (z.Length != 0)
44+
{
45+
throw new Exception();
46+
}
47+
z.Append("TEsting 12345");
48+
Pool.Return(z);
49+
}
50+
51+
using(var item = Pool.GetPooledObject())
52+
{
53+
item.Value.Append("Testl");
54+
}
55+
56+
//var poolPolicyTest = new PoolPolicy<StringBuilder>((p) => new PooledObject<StringBuilder>(p, new StringBuilder()), (jw) => jw.Clear(), 10, PooledItemInitialization.Return);
57+
//var poolTest = new Pool<PooledObject<StringBuilder>>(poolPolicyTest);
58+
//Retrieve an instance from the pool
59+
// using (var pooledItem =Pool.Get())
60+
//{
61+
// //pooledItem.Value is the object you actually want.
62+
// //If the pool is for tyhe type PooledObject<System.Text.StringBuilder> then
63+
// //you can access the string builder instance like this;
64+
// pooledItem.Value.Append("Some text to add to the builder");
65+
66+
// } // The item will automatically be returned to the pool here.
67+
68+
}
69+
}
70+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("SampleFrameworkConsole")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("SampleFrameworkConsole")]
13+
[assembly: AssemblyCopyright("Copyright © 2019")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("cad55cc3-64cb-48a0-90ae-32cb5e3adf84")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{CAD55CC3-64CB-48A0-90AE-32CB5E3ADF84}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>SampleFrameworkConsole</RootNamespace>
10+
<AssemblyName>SampleFrameworkConsole</AssemblyName>
11+
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
<Deterministic>true</Deterministic>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="System" />
37+
<Reference Include="System.Core" />
38+
<Reference Include="System.Xml.Linq" />
39+
<Reference Include="System.Data.DataSetExtensions" />
40+
<Reference Include="Microsoft.CSharp" />
41+
<Reference Include="System.Data" />
42+
<Reference Include="System.Net.Http" />
43+
<Reference Include="System.Xml" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<Compile Include="Program.cs" />
47+
<Compile Include="Properties\AssemblyInfo.cs" />
48+
</ItemGroup>
49+
<ItemGroup>
50+
<None Include="App.config" />
51+
</ItemGroup>
52+
<ItemGroup>
53+
<ProjectReference Include="..\..\src\ObjectPool.csproj">
54+
<Project>{af5de074-1bd0-4148-aba0-ccf42f8523fc}</Project>
55+
<Name>ObjectPool</Name>
56+
</ProjectReference>
57+
</ItemGroup>
58+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
59+
</Project>

src/DefaultPoolPolicy.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace ObjectPool
7+
{
8+
/// <summary>
9+
/// Provides configuration controlling how an object pool works.
10+
/// Will call empty constructor for creation of object.
11+
/// </summary>
12+
/// <typeparam name="T">The type of item being pooled.</typeparam>
13+
/// <seealso cref="PooledItemInitialization"/>
14+
/// <seealso cref="ObjectPool.Pool{T}"/>
15+
public class DefaultPoolPolicy<T> : IPoolPolicy<T> where T : class, new()
16+
{
17+
private readonly Action<T> _reinitializeObject = null;
18+
19+
public int MaximumPoolSize { get; }
20+
21+
public PooledItemInitialization InitializationPolicy { get; }
22+
23+
24+
public DefaultPoolPolicy(Action<T> reinitializeObject=null, int maxPoolSize = 10, PooledItemInitialization initializePolicy = PooledItemInitialization.Return)
25+
{
26+
_reinitializeObject = reinitializeObject;
27+
MaximumPoolSize = maxPoolSize > 0 ? maxPoolSize : Environment.ProcessorCount * 2;
28+
this.InitializationPolicy = initializePolicy;
29+
30+
}
31+
32+
33+
public T Create(IPool<T> pool)
34+
{
35+
return new T();
36+
}
37+
38+
public void Reinitialize(T obj)
39+
{
40+
_reinitializeObject?.Invoke(obj);
41+
}
42+
}
43+
}

src/GlobalSuppressions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// This file is used by Code Analysis to maintain SuppressMessage
2+
// attributes that are applied to this project.
3+
// Project-level suppressions either have no target or are given
4+
// a specific target and scoped to a namespace, type, member, etc.
5+
6+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "HAA0603:Delegate allocation from a method group", Justification = "<Pending>", Scope = "member", Target = "~M:ObjectPool.Pool`1.#ctor(ObjectPool.PoolPolicy{`0})")]

0 commit comments

Comments
 (0)