Skip to content

Commit

Permalink
Perf | Cache common callbacks with a single state parameter (#378)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wraith2 authored Feb 6, 2020
1 parent 17eb766 commit 8e8fe74
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ private static readonly ConcurrentDictionary<string, IList<string>> _ColumnEncry
capacity: 1,
comparer: StringComparer.OrdinalIgnoreCase);

private static readonly Action<object> s_openAsyncCancel = OpenAsyncCancel;

/// <include file='..\..\..\..\..\..\..\doc\snippets\Microsoft.Data.SqlClient\SqlConnection.xml' path='docs/members[@name="SqlConnection"]/ColumnEncryptionKeyCacheTtl/*' />
public static TimeSpan ColumnEncryptionKeyCacheTtl { get; set; } = TimeSpan.FromHours(2);

Expand Down Expand Up @@ -1281,7 +1283,7 @@ public override Task OpenAsync(CancellationToken cancellationToken)
CancellationTokenRegistration registration = new CancellationTokenRegistration();
if (cancellationToken.CanBeCanceled)
{
registration = cancellationToken.Register(s => ((TaskCompletionSource<DbConnectionInternal>)s).TrySetCanceled(), completion);
registration = cancellationToken.Register(s_openAsyncCancel, completion);
}
OpenAsyncRetry retry = new OpenAsyncRetry(this, completion, result, registration);
_currentCompletion = new Tuple<TaskCompletionSource<DbConnectionInternal>, Task>(completion, result.Task);
Expand All @@ -1302,6 +1304,11 @@ public override Task OpenAsync(CancellationToken cancellationToken)
}
}

private static void OpenAsyncCancel(object state)
{
((TaskCompletionSource<DbConnectionInternal>)state).TrySetCanceled();
}

/// <include file='..\..\..\..\..\..\..\doc\snippets\Microsoft.Data.SqlClient\SqlConnection.xml' path='docs/members[@name="SqlConnection"]/GetSchema2/*' />
public override DataTable GetSchema()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ internal enum SnapshottedStateFlags : byte

private const int AttentionTimeoutSeconds = 5;

private static readonly ContextCallback s_readAdyncCallbackComplete = ReadAsyncCallbackComplete;

// Ticks to consider a connection "good" after a successful I/O (10,000 ticks = 1 ms)
// The resolution of the timer is typically in the range 10 to 16 milliseconds according to msdn.
// We choose a value that is smaller than the likely timer resolution, but
Expand Down Expand Up @@ -2824,7 +2826,7 @@ public void ReadAsyncCallback(IntPtr key, PacketHandle packet, uint error)
{
if (_executionContext != null)
{
ExecutionContext.Run(_executionContext, (state) => source.TrySetResult(null), null);
ExecutionContext.Run(_executionContext, s_readAdyncCallbackComplete, source);
}
else
{
Expand All @@ -2848,6 +2850,12 @@ public void ReadAsyncCallback(IntPtr key, PacketHandle packet, uint error)
}
}

private static void ReadAsyncCallbackComplete(object state)
{
TaskCompletionSource<object> source = (TaskCompletionSource<object>)state;
source.TrySetResult(null);
}

protected abstract bool CheckPacket(PacketHandle packet, TaskCompletionSource<object> source);

private void ReadAsyncCallbackCaptureException(TaskCompletionSource<object> source)
Expand Down

0 comments on commit 8e8fe74

Please sign in to comment.