Skip to content

Commit 444a1de

Browse files
committed
feature: Add a build flavor 'Minimal'
In an effort to use NetMQ with Unity3D, one problem was trying to satisfy the dependencies for `System.ServiceModel.Primitives`. It required also providing `System.ServiceModel.{Http,Duplex,NetTcp}` and perhaps more before I gave up. These dependencies were only noticed while Unity3D built the project. When using NetMQ in a regular dotnet project, I've had no problems with the `System.ServiceModel.Primitives` dependencies. I added an alternative implementation of `IBufferPool` using the `ArrayPool` from `System.Buffers` but it is unused. I instrumented the build system to have a "Flavor" that by default is set to "Standard", which changes nothing from before. For my usecase, however, I set the flavor to "Minimal". $ dotnet build -p:Flavor=Minimal This excludes "System.ServiceModel.Primitives" and defines a preprocessor flag of `FLAVOR_MINIMAL` that excludes the BufferManagerBufferPool class and uses the ArrayBufferPool instead.
1 parent 1264afb commit 444a1de

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/NetMQ/BufferPool.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
2+
#if ! FLAVOR_MINIMAL
23
using System.ServiceModel.Channels;
4+
#endif
35
using System.Threading;
46
using System.Buffers;
57

@@ -25,6 +27,10 @@ public interface IBufferPool : IDisposable
2527
void Return(byte[] buffer);
2628
}
2729

30+
/// <summary>
31+
/// This implementation of <see cref="IBufferPool"/> uses System.Buffer's ArrayPool
32+
/// class to manage a pool of buffers.
33+
/// </summary>
2834
public class ArrayBufferPool : IBufferPool
2935
{
3036
private readonly ArrayPool<byte> m_arrayPool;
@@ -84,6 +90,8 @@ protected virtual void Dispose(bool disposing)
8490
// We don't have a means of clearing ArrayPool like BufferManager does.
8591
}
8692
}
93+
94+
#if ! FLAVOR_MINIMAL
8795
/// <summary>
8896
/// This implementation of <see cref="IBufferPool"/> uses WCF's <see cref="BufferManager"/>
8997
/// class to manage a pool of buffers.
@@ -148,6 +156,7 @@ protected virtual void Dispose(bool disposing)
148156
m_bufferManager.Clear();
149157
}
150158
}
159+
#endif
151160

152161
/// <summary>
153162
/// This simple implementation of <see cref="IBufferPool"/> does no buffer pooling. Instead, it uses regular
@@ -194,6 +203,7 @@ protected virtual void Dispose(bool disposing)
194203
}
195204
}
196205

206+
197207
/// <summary>
198208
/// Contains a singleton instance of <see cref="IBufferPool"/> used for allocating byte arrays
199209
/// for <see cref="Msg"/> instances with type <see cref="MsgType.Pool"/>.
@@ -204,7 +214,7 @@ protected virtual void Dispose(bool disposing)
204214
/// <para/>
205215
/// The default implementation is <see cref="GCBufferPool"/>.
206216
/// <list type="bullet">
207-
/// <item>Call <see cref="SetBufferManagerBufferPool"/> to replace it with a <see cref="BufferManagerBufferPool"/>.</item>
217+
/// <item>Call <see cref="SetBufferManagerBufferPool"/> to replace it with a pooling implementation.</item>
208218
/// <item>Call <see cref="SetGCBufferPool"/> to reinstate the default <see cref="GCBufferPool"/>.</item>
209219
/// <item>Call <see cref="SetCustomBufferPool"/> to substitute a custom implementation for the allocation and
210220
/// deallocation of message buffers.</item>
@@ -223,15 +233,19 @@ public static void SetGCBufferPool()
223233
}
224234

225235
/// <summary>
226-
/// Set BufferPool to use the <see cref="BufferManagerBufferPool"/> to manage the buffer-pool.
236+
/// Set BufferPool to use a buffer pooling implementation to manage the buffer-pool.
227237
/// </summary>
228238
/// <param name="maxBufferPoolSize">the maximum size to allow for the buffer pool</param>
229239
/// <param name="maxBufferSize">the maximum size to allow for each individual buffer in the pool</param>
230240
/// <exception cref="InsufficientMemoryException">There was insufficient memory to create the requested buffer pool.</exception>
231241
/// <exception cref="ArgumentOutOfRangeException">Either maxBufferPoolSize or maxBufferSize was less than zero.</exception>
232242
public static void SetBufferManagerBufferPool(long maxBufferPoolSize, int maxBufferSize)
233243
{
244+
#if FLAVOR_MINIMAL
245+
SetCustomBufferPool(new ArrayBufferPool(maxBufferPoolSize, maxBufferSize));
246+
#else
234247
SetCustomBufferPool(new BufferManagerBufferPool(maxBufferPoolSize, maxBufferSize));
248+
#endif
235249
}
236250

237251
/// <summary>

src/NetMQ/NetMQ.csproj

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@
2222
<IncludeSymbols>true</IncludeSymbols>
2323
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
2424
<GenerateDocumentationFile>true</GenerateDocumentationFile>
25+
26+
<Flavor>Standard</Flavor>
27+
<!-- Set flavor to minimal to exclude dependencies where possible. Found to
28+
useful when building for use with Unity3D. Can change here in .csproj
29+
or by using the following command argument when building:
30+
31+
$ dotnet build -p:Flavor=Minimal
32+
-->
33+
<!-- <Flavor>Minimal</Flavor> -->
34+
</PropertyGroup>
35+
36+
<PropertyGroup Condition="'$(Flavor)'=='Minimal'">
37+
<DefineConstants>$(DefineConstants);FLAVOR_MINIMAL</DefineConstants>
2538
</PropertyGroup>
2639

2740
<PropertyGroup Condition=" '$(TargetFramework)' != 'netstandard2.1' ">
@@ -40,13 +53,13 @@
4053
</ItemGroup>
4154

4255
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
43-
<PackageReference Include="System.ServiceModel.Primitives" Version="4.9.0" />
56+
<PackageReference Include="System.ServiceModel.Primitives" Version="4.9.0" Condition="'$(Flavor)'!='Minimal'"/>
4457
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
4558
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
4659
</ItemGroup>
4760

4861
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.1' ">
49-
<PackageReference Include="System.ServiceModel.Primitives" Version="4.9.0" />
62+
<PackageReference Include="System.ServiceModel.Primitives" Version="4.9.0" Condition="'$(Flavor)'!='Minimal'"/>
5063
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
5164
</ItemGroup>
5265

0 commit comments

Comments
 (0)