Skip to content

Commit

Permalink
Perf | Optimize async method allocations (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wraith2 authored and cheenamalhotra committed Dec 5, 2019
1 parent d38d554 commit 2a8f998
Show file tree
Hide file tree
Showing 2 changed files with 500 additions and 259 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public sealed partial class SqlCommand : DbCommand, ICloneable
private static readonly DiagnosticListener _diagnosticListener = new DiagnosticListener(SqlClientDiagnosticListenerExtensions.DiagnosticListenerName);
private bool _parentOperationStarted = false;

internal static readonly Action<object> s_cancelIgnoreFailure = CancelIgnoreFailureCallback;

// Prepare
// Against 7.0 Serve a prepare/unprepare requires an extra roundtrip to the server.
//
Expand Down Expand Up @@ -2137,7 +2139,7 @@ public override Task<int> ExecuteNonQueryAsync(CancellationToken cancellationTok
source.SetCanceled();
return source.Task;
}
registration = cancellationToken.Register(s => ((SqlCommand)s).CancelIgnoreFailure(), this);
registration = cancellationToken.Register(s_cancelIgnoreFailure, this);
}

Task<int> returnedTask = source.Task;
Expand Down Expand Up @@ -2225,7 +2227,7 @@ protected override Task<DbDataReader> ExecuteDbDataReaderAsync(CommandBehavior b
source.SetCanceled();
return source.Task;
}
registration = cancellationToken.Register(s => ((SqlCommand)s).CancelIgnoreFailure(), this);
registration = cancellationToken.Register(s_cancelIgnoreFailure, this);
}

Task<SqlDataReader> returnedTask = source.Task;
Expand Down Expand Up @@ -2373,7 +2375,7 @@ public Task<XmlReader> ExecuteXmlReaderAsync(CancellationToken cancellationToken
source.SetCanceled();
return source.Task;
}
registration = cancellationToken.Register(s => ((SqlCommand)s).CancelIgnoreFailure(), this);
registration = cancellationToken.Register(s_cancelIgnoreFailure, this);
}

Task<XmlReader> returnedTask = source.Task;
Expand Down Expand Up @@ -5947,6 +5949,11 @@ internal void CompletePendingReadWithFailure(int errorCode, bool resetForcePendi
}
}
#endif
internal static void CancelIgnoreFailureCallback(object state)
{
SqlCommand command = (SqlCommand)state;
command.CancelIgnoreFailure();
}

internal void CancelIgnoreFailure()
{
Expand Down

0 comments on commit 2a8f998

Please sign in to comment.