Skip to content

Commit 7f30536

Browse files
authored
Merge | BufferWriterExtensions (#3264)
* Move ArrayBufferWriter and BufferWriterExtensions to a utilities folder in common project * Moving to Utilities namespace * SCOPE CREEP
1 parent aa35edc commit 7f30536

File tree

10 files changed

+78
-73
lines changed

10 files changed

+78
-73
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -584,12 +584,6 @@
584584
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\SqlNotificationType.cs">
585585
<Link>Microsoft\Data\SqlClient\SqlNotificationType.cs</Link>
586586
</Compile>
587-
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\SqlObjectPool.cs">
588-
<Link>Microsoft\Data\SqlClient\SqlObjectPool.cs</Link>
589-
</Compile>
590-
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\SqlObjectPools.cs">
591-
<Link>Microsoft\Data\SqlClient\SqlObjectPools.cs</Link>
592-
</Compile>
593587
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\SqlParameter.cs">
594588
<Link>Microsoft\Data\SqlClient\SqlParameter.cs</Link>
595589
</Compile>
@@ -647,6 +641,12 @@
647641
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\SSPI\SSPIContextProvider.cs">
648642
<Link>Microsoft\Data\SqlClient\SSPI\SSPIContextProvider.cs</Link>
649643
</Compile>
644+
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\Utilities\ObjectPool.cs">
645+
<Link>Microsoft\Data\SqlClient\Utilities\ObjectPool.cs</Link>
646+
</Compile>
647+
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\Utilities\ObjectPools.cs">
648+
<Link>Microsoft\Data\SqlClient\Utilities\ObjectPools.cs</Link>
649+
</Compile>
650650
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\TdsEnums.cs">
651651
<Link>Microsoft\Data\SqlClient\TdsEnums.cs</Link>
652652
</Compile>
@@ -683,6 +683,9 @@
683683
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\VirtualSecureModeEnclaveProviderBase.cs">
684684
<Link>Microsoft\Data\SqlClient\VirtualSecureModeEnclaveProviderBase.cs</Link>
685685
</Compile>
686+
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlDbTypeExtensions.cs">
687+
<Link>Microsoft\Data\SqlDbTypeExtensions.cs</Link>
688+
</Compile>
686689
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlTypes\SQLResource.cs">
687690
<Link>Microsoft\Data\SQLTypes\SQLResource.cs</Link>
688691
</Compile>
@@ -699,10 +702,7 @@
699702
<Link>Resources\ResDescriptionAttribute.cs</Link>
700703
</Compile>
701704
<Compile Include="$(CommonSourceRoot)System\Diagnostics\CodeAnalysis.cs">
702-
<Link>Common\System\Diagnostics\CodeAnalysis.cs</Link>
703-
</Compile>
704-
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlDbTypeExtensions.cs">
705-
<Link>Microsoft\Data\SqlDbTypeExtensions.cs</Link>
705+
<Link>System\Diagnostics\CodeAnalysis.cs</Link>
706706
</Compile>
707707

708708
<Compile Include="Common\System\NotImplemented.cs" />

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SNIPhysicalHandle.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Generic;
77
using System.Diagnostics;
88
using System.Threading;
9+
using Microsoft.Data.SqlClient.Utilities;
910

1011
namespace Microsoft.Data.SqlClient.SNI
1112
{
@@ -16,11 +17,11 @@ internal abstract class SNIPhysicalHandle : SNIHandle
1617
#if DEBUG
1718
private static int s_packetId;
1819
#endif
19-
private SqlObjectPool<SNIPacket> _pool;
20+
private ObjectPool<SNIPacket> _pool;
2021

2122
protected SNIPhysicalHandle(int poolSize = DefaultPoolSize)
2223
{
23-
_pool = new SqlObjectPool<SNIPacket>(poolSize);
24+
_pool = new ObjectPool<SNIPacket>(poolSize);
2425
}
2526

2627
public override SNIPacket RentPacket(int headerSize, int dataSize)

src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -350,12 +350,6 @@
350350
</Compile>
351351
<Compile Include="$(CommonSourceRoot)Microsoft\Data\Sql\SqlDataSourceEnumerator.cs">
352352
<Link>Microsoft\Data\Sql\SqlDataSourceEnumerator.cs</Link>
353-
</Compile>
354-
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\SqlObjectPool.cs">
355-
<Link>Microsoft\Data\SqlClient\SqlObjectPool.cs</Link>
356-
</Compile>
357-
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\SqlObjectPools.cs">
358-
<Link>Microsoft\Data\SqlClient\SqlObjectPools.cs</Link>
359353
</Compile>
360354
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\AAsyncCallContext.cs">
361355
<Link>Microsoft\Data\SqlClient\AAsyncCallContext.cs</Link>
@@ -381,9 +375,6 @@
381375
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\AlwaysEncryptedKeyConverter.cs">
382376
<Link>Microsoft\Data\SqlClient\AlwaysEncryptedKeyConverter.cs</Link>
383377
</Compile>
384-
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\ArrayBufferWriter.cs">
385-
<Link>Microsoft\Data\SqlClient\ArrayBufferWriter.cs</Link>
386-
</Compile>
387378
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\AzureAttestationBasedEnclaveProvider.cs">
388379
<Link>Microsoft\Data\SqlClient\AzureAttestationBasedEnclaveProvider.cs</Link>
389380
</Compile>
@@ -840,6 +831,15 @@
840831
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\TdsValueSetter.cs">
841832
<Link>Microsoft\Data\SqlClient\TdsValueSetter.cs</Link>
842833
</Compile>
834+
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\Utilities\BufferWriterExtensions.netfx.cs">
835+
<Link>Microsoft\Data\SqlClient\Utilities\BufferWriterExtensions.netfx.cs</Link>
836+
</Compile>
837+
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\Utilities\ObjectPool.cs">
838+
<Link>Microsoft\Data\SqlClient\Utilities\ObjectPool.cs</Link>
839+
</Compile>
840+
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\Utilities\ObjectPools.cs">
841+
<Link>Microsoft\Data\SqlClient\Utilities\ObjectPools.cs</Link>
842+
</Compile>
843843
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\VirtualSecureModeEnclaveProvider.cs">
844844
<Link>Microsoft\Data\SqlClient\VirtualSecureModeEnclaveProvider.cs</Link>
845845
</Compile>
@@ -864,6 +864,9 @@
864864
<Compile Include="$(CommonSourceRoot)Resources\ResDescriptionAttribute.cs">
865865
<Link>Resources\ResDescriptionAttribute.cs</Link>
866866
</Compile>
867+
<Compile Include="$(CommonSourceRoot)System\Buffers\ArrayBufferWriter.netfx.cs">
868+
<Link>System\Buffers\ArrayBufferWriter.netfx.cs</Link>
869+
</Compile>
867870
<Compile Include="$(CommonSourceRoot)System\IO\StreamExtensions.netfx.cs">
868871
<Link>System\IO\StreamExtensions.netfx.cs</Link>
869872
</Compile>
@@ -872,7 +875,6 @@
872875
<Compile Include="Common\Microsoft\Data\Common\NameValuePermission.cs" />
873876
<Compile Include="Microsoft\Data\Common\DbConnectionOptions.cs" />
874877
<Compile Include="Microsoft\Data\Common\DbConnectionString.cs" />
875-
<Compile Include="Microsoft\Data\SqlClient\BufferWriterExtensions.cs" />
876878
<Compile Include="Microsoft\Data\SqlClient\Server\SmiConnection.cs" />
877879
<Compile Include="Microsoft\Data\SqlClient\Server\SmiContext.cs" />
878880
<Compile Include="Microsoft\Data\SqlClient\Server\SmiContextFactory.cs" />

src/Microsoft.Data.SqlClient/src/Interop/Windows/Sni/SniNativeWrapper.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Text;
1010
using Interop.Windows.Sni;
1111
using Microsoft.Data.Common;
12+
using Microsoft.Data.SqlClient.Utilities;
1213

1314
#if NETFRAMEWORK
1415
using System.Runtime.CompilerServices;
@@ -259,7 +260,7 @@ internal static unsafe uint SniOpenSyncEx(
259260
else
260261
{
261262
// We have a value of the SPN, so we marshal that and send it to the native layer
262-
var writer = SqlObjectPools.BufferWriter.Rent();
263+
var writer = ObjectPools.BufferWriter.Rent();
263264

264265
try
265266
{
@@ -277,7 +278,7 @@ internal static unsafe uint SniOpenSyncEx(
277278
}
278279
finally
279280
{
280-
SqlObjectPools.BufferWriter.Return(writer);
281+
ObjectPools.BufferWriter.Return(writer);
281282
}
282283
}
283284
}
@@ -445,7 +446,7 @@ internal static unsafe uint SniSecGenClientContext(
445446
ref uint sendLength,
446447
string serverUserName)
447448
{
448-
var serverWriter = SqlObjectPools.BufferWriter.Rent();
449+
var serverWriter = ObjectPools.BufferWriter.Rent();
449450

450451
try
451452
{
@@ -470,7 +471,7 @@ internal static unsafe uint SniSecGenClientContext(
470471
}
471472
finally
472473
{
473-
SqlObjectPools.BufferWriter.Return(serverWriter);
474+
ObjectPools.BufferWriter.Return(serverWriter);
474475
}
475476
}
476477

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlObjectPools.cs

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

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParser.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Buffers;
33
using System.Diagnostics;
4+
using Microsoft.Data.SqlClient.Utilities;
45

56
#nullable enable
67

@@ -29,7 +30,7 @@ internal void ProcessSSPI(int receivedLength)
2930
}
3031

3132
// allocate send buffer and initialize length
32-
var writer = SqlObjectPools.BufferWriter.Rent();
33+
var writer = ObjectPools.BufferWriter.Rent();
3334

3435
try
3536
{
@@ -42,7 +43,7 @@ internal void ProcessSSPI(int receivedLength)
4243
}
4344
finally
4445
{
45-
SqlObjectPools.BufferWriter.Return(writer);
46+
ObjectPools.BufferWriter.Return(writer);
4647
}
4748
}
4849
finally
@@ -167,7 +168,7 @@ internal void TdsLogin(
167168
{
168169
if (rec.useSSPI)
169170
{
170-
sspiWriter = SqlObjectPools.BufferWriter.Rent();
171+
sspiWriter = ObjectPools.BufferWriter.Rent();
171172

172173
// Call helper function for SSPI data and actual length.
173174
// Since we don't have SSPI data from the server, send null for the
@@ -209,7 +210,7 @@ internal void TdsLogin(
209210
{
210211
if (sspiWriter is not null)
211212
{
212-
SqlObjectPools.BufferWriter.Return(sspiWriter);
213+
ObjectPools.BufferWriter.Return(sspiWriter);
213214
}
214215
}
215216

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/BufferWriterExtensions.cs renamed to src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Utilities/BufferWriterExtensions.netfx.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
using System;
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
#if NETFRAMEWORK
6+
7+
using System;
28
using System.Buffers;
39
using System.Text;
410

5-
namespace Microsoft.Data.SqlClient
11+
namespace Microsoft.Data.SqlClient.Utilities
612
{
713
internal static class BufferWriterExtensions
814
{
@@ -24,3 +30,5 @@ internal static long GetBytes(this Encoding encoding, string str, IBufferWriter<
2430
}
2531
}
2632
}
33+
34+
#endif

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlObjectPool.cs renamed to src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Utilities/ObjectPool.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66
using System.Diagnostics;
77
using System.Threading;
88

9-
namespace Microsoft.Data.SqlClient
9+
namespace Microsoft.Data.SqlClient.Utilities
1010
{
1111
// this is a very simple threadsafe pool derived from the aspnet/extensions default pool implementation
1212
// https://github.com/dotnet/extensions/blob/release/3.1/src/ObjectPool/src/DefaultObjectPool.cs
13-
internal sealed class SqlObjectPool<T> where T : class
13+
internal sealed class ObjectPool<T> where T : class
1414
{
1515
private readonly ObjectWrapper[] _items;
1616
private readonly Action<T> _onReturned;
1717
private readonly Func<T> _onCreate;
1818

1919
private T _firstItem;
2020

21-
public SqlObjectPool(int maximumRetained, Func<T> onCreate = null, Action<T> onReturned = null)
21+
public ObjectPool(int maximumRetained, Func<T> onCreate = null, Action<T> onReturned = null)
2222
{
2323
// -1 due to _firstItem
2424
_items = new ObjectWrapper[maximumRetained - 1];
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Buffers;
7+
8+
namespace Microsoft.Data.SqlClient.Utilities
9+
{
10+
/// <summary>
11+
/// This is a collection of general object pools that can be reused as needed.
12+
/// </summary>
13+
internal static class ObjectPools
14+
{
15+
private static readonly Lazy<ObjectPool<ArrayBufferWriter<byte>>> s_bufferWriterPool =
16+
new(() => new ObjectPool<ArrayBufferWriter<byte>>(
17+
maximumRetained: 20,
18+
onCreate: () => new ArrayBufferWriter<byte>(),
19+
onReturned: bufferWriter => bufferWriter.Clear()));
20+
21+
internal static ObjectPool<ArrayBufferWriter<byte>> BufferWriter => s_bufferWriterPool.Value;
22+
}
23+
24+
}

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ArrayBufferWriter.cs renamed to src/Microsoft.Data.SqlClient/src/System/Buffers/ArrayBufferWriter.netfx.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44

55
#if NETFRAMEWORK
66

7-
using System;
8-
using System.Buffers;
97
using System.Diagnostics;
108
using System.Globalization;
119

12-
namespace Microsoft.Data.SqlClient
10+
namespace System.Buffers
1311
{
1412
/// <summary>
1513
/// Internal implementation of <see cref="IBufferWriter{T}"/> for platforms that don't have it available.
1614
///
1715
/// NOTE: This file should not be edited as it is copied from dotnet/runtime and has no tests in this repo.
16+
/// It is only included for netfx version of the driver because this class was not introduced
17+
/// until netstandard 2.1.
1818
/// </summary>
1919
/// <seealso href="https://github.com/dotnet/runtime/blob/bc2bd2bd77ecd7a7979a3ef815e3ff36881a1b94/src/libraries/Common/src/System/Buffers/ArrayBufferWriter.cs"/>
20-
sealed class ArrayBufferWriter<T> : IBufferWriter<T>
20+
internal sealed class ArrayBufferWriter<T> : IBufferWriter<T>
2121
{
2222
// Copy of Array.MaxLength.
2323
// Used by projects targeting .NET Framework.
@@ -137,6 +137,8 @@ public void Advance(int count)
137137
_index += count;
138138
}
139139

140+
141+
140142
/// <summary>
141143
/// Returns a <see cref="Memory{T}"/> to write to that is at least the requested length (specified by <paramref name="sizeHint"/>).
142144
/// If no <paramref name="sizeHint"/> is provided (or it's equal to <code>0</code>), some non-empty buffer is returned.

0 commit comments

Comments
 (0)