Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Cache common callbacks with a single state parameter #378

Merged
merged 1 commit into from
Feb 6, 2020
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 @@ -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