Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -584,12 +584,6 @@
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\SqlNotificationType.cs">
<Link>Microsoft\Data\SqlClient\SqlNotificationType.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\SqlObjectPool.cs">
<Link>Microsoft\Data\SqlClient\SqlObjectPool.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\SqlObjectPools.cs">
<Link>Microsoft\Data\SqlClient\SqlObjectPools.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\SqlParameter.cs">
<Link>Microsoft\Data\SqlClient\SqlParameter.cs</Link>
</Compile>
Expand Down Expand Up @@ -647,6 +641,12 @@
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\SSPI\SSPIContextProvider.cs">
<Link>Microsoft\Data\SqlClient\SSPI\SSPIContextProvider.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\Utilities\ObjectPool.cs">
<Link>Microsoft\Data\SqlClient\Utilities\ObjectPool.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\Utilities\ObjectPools.cs">
<Link>Microsoft\Data\SqlClient\Utilities\ObjectPools.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\TdsEnums.cs">
<Link>Microsoft\Data\SqlClient\TdsEnums.cs</Link>
</Compile>
Expand Down Expand Up @@ -683,6 +683,9 @@
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\VirtualSecureModeEnclaveProviderBase.cs">
<Link>Microsoft\Data\SqlClient\VirtualSecureModeEnclaveProviderBase.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlDbTypeExtensions.cs">
<Link>Microsoft\Data\SqlDbTypeExtensions.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlTypes\SQLResource.cs">
<Link>Microsoft\Data\SQLTypes\SQLResource.cs</Link>
</Compile>
Expand All @@ -699,10 +702,7 @@
<Link>Resources\ResDescriptionAttribute.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)System\Diagnostics\CodeAnalysis.cs">
<Link>Common\System\Diagnostics\CodeAnalysis.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlDbTypeExtensions.cs">
<Link>Microsoft\Data\SqlDbTypeExtensions.cs</Link>
<Link>System\Diagnostics\CodeAnalysis.cs</Link>
</Compile>

<Compile Include="Common\System\NotImplemented.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using Microsoft.Data.SqlClient.Utilities;

namespace Microsoft.Data.SqlClient.SNI
{
Expand All @@ -16,11 +17,11 @@ internal abstract class SNIPhysicalHandle : SNIHandle
#if DEBUG
private static int s_packetId;
#endif
private SqlObjectPool<SNIPacket> _pool;
private ObjectPool<SNIPacket> _pool;

protected SNIPhysicalHandle(int poolSize = DefaultPoolSize)
{
_pool = new SqlObjectPool<SNIPacket>(poolSize);
_pool = new ObjectPool<SNIPacket>(poolSize);
}

public override SNIPacket RentPacket(int headerSize, int dataSize)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,12 +350,6 @@
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\Sql\SqlDataSourceEnumerator.cs">
<Link>Microsoft\Data\Sql\SqlDataSourceEnumerator.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\SqlObjectPool.cs">
<Link>Microsoft\Data\SqlClient\SqlObjectPool.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\SqlObjectPools.cs">
<Link>Microsoft\Data\SqlClient\SqlObjectPools.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\AAsyncCallContext.cs">
<Link>Microsoft\Data\SqlClient\AAsyncCallContext.cs</Link>
Expand All @@ -381,9 +375,6 @@
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\AlwaysEncryptedKeyConverter.cs">
<Link>Microsoft\Data\SqlClient\AlwaysEncryptedKeyConverter.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\ArrayBufferWriter.cs">
<Link>Microsoft\Data\SqlClient\ArrayBufferWriter.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\AzureAttestationBasedEnclaveProvider.cs">
<Link>Microsoft\Data\SqlClient\AzureAttestationBasedEnclaveProvider.cs</Link>
</Compile>
Expand Down Expand Up @@ -837,6 +828,15 @@
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\TdsValueSetter.cs">
<Link>Microsoft\Data\SqlClient\TdsValueSetter.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\Utilities\BufferWriterExtensions.netfx.cs">
<Link>Microsoft\Data\SqlClient\Utilities\BufferWriterExtensions.netfx.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\Utilities\ObjectPool.cs">
<Link>Microsoft\Data\SqlClient\Utilities\ObjectPool.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\Utilities\ObjectPools.cs">
<Link>Microsoft\Data\SqlClient\Utilities\ObjectPools.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\VirtualSecureModeEnclaveProvider.cs">
<Link>Microsoft\Data\SqlClient\VirtualSecureModeEnclaveProvider.cs</Link>
</Compile>
Expand All @@ -861,6 +861,9 @@
<Compile Include="$(CommonSourceRoot)Resources\ResDescriptionAttribute.cs">
<Link>Resources\ResDescriptionAttribute.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)System\Buffers\ArrayBufferWriter.netfx.cs">
<Link>System\Buffers\ArrayBufferWriter.netfx.cs</Link>
</Compile>
<Compile Include="$(CommonSourceRoot)System\IO\StreamExtensions.netfx.cs">
<Link>System\IO\StreamExtensions.netfx.cs</Link>
</Compile>
Expand All @@ -870,7 +873,6 @@
<Compile Include="Common\Microsoft\Data\Common\NameValuePermission.cs" />
<Compile Include="Microsoft\Data\Common\DbConnectionOptions.cs" />
<Compile Include="Microsoft\Data\Common\DbConnectionString.cs" />
<Compile Include="Microsoft\Data\SqlClient\BufferWriterExtensions.cs" />
<Compile Include="Microsoft\Data\SqlClient\Server\SmiConnection.cs" />
<Compile Include="Microsoft\Data\SqlClient\Server\SmiContext.cs" />
<Compile Include="Microsoft\Data\SqlClient\Server\SmiContextFactory.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Text;
using Interop.Windows.Sni;
using Microsoft.Data.Common;
using Microsoft.Data.SqlClient.Utilities;

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

try
{
Expand All @@ -277,7 +278,7 @@ internal static unsafe uint SniOpenSyncEx(
}
finally
{
SqlObjectPools.BufferWriter.Return(writer);
ObjectPools.BufferWriter.Return(writer);
}
}
}
Expand Down Expand Up @@ -445,7 +446,7 @@ internal static unsafe uint SniSecGenClientContext(
ref uint sendLength,
string serverUserName)
{
var serverWriter = SqlObjectPools.BufferWriter.Rent();
var serverWriter = ObjectPools.BufferWriter.Rent();

try
{
Expand All @@ -470,7 +471,7 @@ internal static unsafe uint SniSecGenClientContext(
}
finally
{
SqlObjectPools.BufferWriter.Return(serverWriter);
ObjectPools.BufferWriter.Return(serverWriter);
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Buffers;
using System.Diagnostics;
using Microsoft.Data.SqlClient.Utilities;

#nullable enable

Expand Down Expand Up @@ -29,7 +30,7 @@ internal void ProcessSSPI(int receivedLength)
}

// allocate send buffer and initialize length
var writer = SqlObjectPools.BufferWriter.Rent();
var writer = ObjectPools.BufferWriter.Rent();

try
{
Expand All @@ -42,7 +43,7 @@ internal void ProcessSSPI(int receivedLength)
}
finally
{
SqlObjectPools.BufferWriter.Return(writer);
ObjectPools.BufferWriter.Return(writer);
}
}
finally
Expand Down Expand Up @@ -167,7 +168,7 @@ internal void TdsLogin(
{
if (rec.useSSPI)
{
sspiWriter = SqlObjectPools.BufferWriter.Rent();
sspiWriter = ObjectPools.BufferWriter.Rent();

// Call helper function for SSPI data and actual length.
// Since we don't have SSPI data from the server, send null for the
Expand Down Expand Up @@ -209,7 +210,7 @@ internal void TdsLogin(
{
if (sspiWriter is not null)
{
SqlObjectPools.BufferWriter.Return(sspiWriter);
ObjectPools.BufferWriter.Return(sspiWriter);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
using System;
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#if NETFRAMEWORK

using System;
using System.Buffers;
using System.Text;

namespace Microsoft.Data.SqlClient
namespace Microsoft.Data.SqlClient.Utilities
{
internal static class BufferWriterExtensions
{
Expand All @@ -24,3 +30,5 @@ internal static long GetBytes(this Encoding encoding, string str, IBufferWriter<
}
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
using System.Diagnostics;
using System.Threading;

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

private T _firstItem;

public SqlObjectPool(int maximumRetained, Func<T> onCreate = null, Action<T> onReturned = null)
public ObjectPool(int maximumRetained, Func<T> onCreate = null, Action<T> onReturned = null)
{
// -1 due to _firstItem
_items = new ObjectWrapper[maximumRetained - 1];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Buffers;

namespace Microsoft.Data.SqlClient.Utilities
{
/// <summary>
/// This is a collection of general object pools that can be reused as needed.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem general. It's a pool of ArrayBufferWriters. Should the class name be ArrayBufferWriterPools ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the theory is that the class will have more object pools added to it as we go. I'm not sure what other pools might be added, but I think that's the idea.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see. This class is basically a namespace to hold static instances of a variety of ObjectPool specializations.

More of a "collection of specific ObjectPool instances that can be reused..."

/// </summary>
internal static class ObjectPools
{
private static readonly Lazy<ObjectPool<ArrayBufferWriter<byte>>> s_bufferWriterPool =
new(() => new ObjectPool<ArrayBufferWriter<byte>>(
maximumRetained: 20,
onCreate: () => new ArrayBufferWriter<byte>(),
onReturned: bufferWriter => bufferWriter.Clear()));

internal static ObjectPool<ArrayBufferWriter<byte>> BufferWriter => s_bufferWriterPool.Value;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@

#if NETFRAMEWORK

using System;
using System.Buffers;
using System.Diagnostics;
using System.Globalization;

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



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