Skip to content

Commit 8e8fe74

Browse files
authored
Perf | Cache common callbacks with a single state parameter (#378)
1 parent 17eb766 commit 8e8fe74

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ private static readonly ConcurrentDictionary<string, IList<string>> _ColumnEncry
9090
capacity: 1,
9191
comparer: StringComparer.OrdinalIgnoreCase);
9292

93+
private static readonly Action<object> s_openAsyncCancel = OpenAsyncCancel;
94+
9395
/// <include file='..\..\..\..\..\..\..\doc\snippets\Microsoft.Data.SqlClient\SqlConnection.xml' path='docs/members[@name="SqlConnection"]/ColumnEncryptionKeyCacheTtl/*' />
9496
public static TimeSpan ColumnEncryptionKeyCacheTtl { get; set; } = TimeSpan.FromHours(2);
9597

@@ -1281,7 +1283,7 @@ public override Task OpenAsync(CancellationToken cancellationToken)
12811283
CancellationTokenRegistration registration = new CancellationTokenRegistration();
12821284
if (cancellationToken.CanBeCanceled)
12831285
{
1284-
registration = cancellationToken.Register(s => ((TaskCompletionSource<DbConnectionInternal>)s).TrySetCanceled(), completion);
1286+
registration = cancellationToken.Register(s_openAsyncCancel, completion);
12851287
}
12861288
OpenAsyncRetry retry = new OpenAsyncRetry(this, completion, result, registration);
12871289
_currentCompletion = new Tuple<TaskCompletionSource<DbConnectionInternal>, Task>(completion, result.Task);
@@ -1302,6 +1304,11 @@ public override Task OpenAsync(CancellationToken cancellationToken)
13021304
}
13031305
}
13041306

1307+
private static void OpenAsyncCancel(object state)
1308+
{
1309+
((TaskCompletionSource<DbConnectionInternal>)state).TrySetCanceled();
1310+
}
1311+
13051312
/// <include file='..\..\..\..\..\..\..\doc\snippets\Microsoft.Data.SqlClient\SqlConnection.xml' path='docs/members[@name="SqlConnection"]/GetSchema2/*' />
13061313
public override DataTable GetSchema()
13071314
{

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ internal enum SnapshottedStateFlags : byte
3535

3636
private const int AttentionTimeoutSeconds = 5;
3737

38+
private static readonly ContextCallback s_readAdyncCallbackComplete = ReadAsyncCallbackComplete;
39+
3840
// Ticks to consider a connection "good" after a successful I/O (10,000 ticks = 1 ms)
3941
// The resolution of the timer is typically in the range 10 to 16 milliseconds according to msdn.
4042
// We choose a value that is smaller than the likely timer resolution, but
@@ -2824,7 +2826,7 @@ public void ReadAsyncCallback(IntPtr key, PacketHandle packet, uint error)
28242826
{
28252827
if (_executionContext != null)
28262828
{
2827-
ExecutionContext.Run(_executionContext, (state) => source.TrySetResult(null), null);
2829+
ExecutionContext.Run(_executionContext, s_readAdyncCallbackComplete, source);
28282830
}
28292831
else
28302832
{
@@ -2848,6 +2850,12 @@ public void ReadAsyncCallback(IntPtr key, PacketHandle packet, uint error)
28482850
}
28492851
}
28502852

2853+
private static void ReadAsyncCallbackComplete(object state)
2854+
{
2855+
TaskCompletionSource<object> source = (TaskCompletionSource<object>)state;
2856+
source.TrySetResult(null);
2857+
}
2858+
28512859
protected abstract bool CheckPacket(PacketHandle packet, TaskCompletionSource<object> source);
28522860

28532861
private void ReadAsyncCallbackCaptureException(TaskCompletionSource<object> source)

0 commit comments

Comments
 (0)