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: Static delegates #1060

Merged
merged 5 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -14,7 +14,7 @@ internal class SNILoadHandle
public static readonly SNILoadHandle SingletonInstance = new SNILoadHandle();

public readonly EncryptionOptions _encryptionOption = EncryptionOptions.OFF;
public ThreadLocal<SNIError> _lastError = new ThreadLocal<SNIError>(() => { return new SNIError(SNIProviders.INVALID_PROV, 0, TdsEnums.SNI_SUCCESS, string.Empty); });
public ThreadLocal<SNIError> _lastError = new ThreadLocal<SNIError>(static () => new SNIError(SNIProviders.INVALID_PROV, 0, TdsEnums.SNI_SUCCESS, string.Empty));

private readonly uint _status = TdsEnums.SNI_SUCCESS;

Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2139,7 +2139,7 @@ internal Task<T> RegisterForConnectionCloseNotification<T>(Task<T> outerTask, ob
{
// Connection exists, schedule removal, will be added to ref collection after calling ValidateAndReconnect
return outerTask.ContinueWith(
continuationFunction: (task, state) =>
continuationFunction: static (task, state) =>
{
Tuple<SqlConnection, object> parameters = (Tuple<SqlConnection, object>)state;
SqlConnection connection = parameters.Item1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ internal static Task CreateContinuationTask(Task task, Action onSuccess, Action<
TaskCompletionSource<object> completion = new TaskCompletionSource<object>();
ContinueTaskWithState(task, completion,
state: Tuple.Create(onSuccess, onFailure, completion),
onSuccess: (state) =>
onSuccess: static (object state) =>
{
var parameters = (Tuple<Action, Action<Exception>, TaskCompletionSource<object>>)state;
Action success = parameters.Item1;
TaskCompletionSource<object> taskCompletionSource = parameters.Item3;
success();
taskCompletionSource.SetResult(null);
},
onFailure: (exception, state) =>
onFailure: static (Exception exception, object state) =>
{
var parameters = (Tuple<Action, Action<Exception>, TaskCompletionSource<object>>)state;
Action<Exception> failure = parameters.Item2;
Expand All @@ -64,7 +64,7 @@ internal static Task CreateContinuationTaskWithState(Task task, object state, Ac
{
var completion = new TaskCompletionSource<object>();
ContinueTaskWithState(task, completion, state,
onSuccess: (continueState) =>
onSuccess: (object continueState) =>
Wraith2 marked this conversation as resolved.
Show resolved Hide resolved
{
onSuccess(continueState);
completion.SetResult(null);
Expand All @@ -81,12 +81,12 @@ internal static Task CreateContinuationTask<T1, T2>(Task task, Action<T1, T2> on
}

internal static void ContinueTask(Task task,
TaskCompletionSource<object> completion,
Action onSuccess,
Action<Exception> onFailure = null,
Action onCancellation = null,
Func<Exception, Exception> exceptionConverter = null
)
TaskCompletionSource<object> completion,
Action onSuccess,
Action<Exception> onFailure = null,
Action onCancellation = null,
Func<Exception, Exception> exceptionConverter = null
)
{
task.ContinueWith(
tsk =>
Expand Down Expand Up @@ -145,7 +145,7 @@ internal static void ContinueTaskWithState(Task task,
)
{
task.ContinueWith(
tsk =>
(Task tsk, object state2) =>
JRahnama marked this conversation as resolved.
Show resolved Hide resolved
{
if (tsk.Exception != null)
{
Expand All @@ -156,7 +156,7 @@ internal static void ContinueTaskWithState(Task task,
}
try
{
onFailure?.Invoke(exc, state);
onFailure?.Invoke(exc, state2);
}
finally
{
Expand All @@ -167,7 +167,7 @@ internal static void ContinueTaskWithState(Task task,
{
try
{
onCancellation?.Invoke(state);
onCancellation?.Invoke(state2);
}
finally
{
Expand All @@ -178,14 +178,16 @@ internal static void ContinueTaskWithState(Task task,
{
try
{
onSuccess(state);
onSuccess(state2);
}
catch (Exception e)
{
completion.SetException(e);
}
}
}, TaskScheduler.Default
},
state: state,
scheduler: TaskScheduler.Default
);
}

Expand All @@ -205,25 +207,42 @@ internal static void WaitForCompletion(Task task, int timeout, Action onTimeout
}
if (!task.IsCompleted)
{
task.ContinueWith(t => { var ignored = t.Exception; }); //Ensure the task does not leave an unobserved exception
if (onTimeout != null)
{
onTimeout();
}
task.ContinueWith(static t => { var ignored = t.Exception; }); //Ensure the task does not leave an unobserved exception
onTimeout?.Invoke();
}
}

internal static void SetTimeoutException(TaskCompletionSource<object> completion, int timeout, Func<Exception> exc, CancellationToken ctoken)
internal static void SetTimeoutException(TaskCompletionSource<object> completion, int timeout, Func<Exception> onFailure, CancellationToken ctoken)
{
if (timeout > 0)
{
Task.Delay(timeout * 1000, ctoken).ContinueWith((tsk) =>
{
if (!tsk.IsCanceled && !completion.Task.IsCompleted)
Task.Delay(timeout * 1000, ctoken).ContinueWith(
(Task task) =>
{
completion.TrySetException(exc());
if (!task.IsCanceled && !completion.Task.IsCompleted)
{
completion.TrySetException(onFailure());
}
}
});
);
}
}

internal static void SetTimeoutExceptionWithState(TaskCompletionSource<object> completion, int timeout, object state, Func<object,Exception> onFailure, CancellationToken cancellationToken)
{
if (timeout > 0)
{
Task.Delay(timeout * 1000, cancellationToken).ContinueWith(
(Task task, object state) =>
{
if (!task.IsCanceled && !completion.Task.IsCompleted)
{
completion.TrySetException(onFailure(state));
}
},
state: state,
cancellationToken: CancellationToken.None
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8833,7 +8833,7 @@ internal Task TdsExecuteSQLBatch(string text, int timeout, SqlNotificationReques
bool taskReleaseConnectionLock = releaseConnectionLock;
releaseConnectionLock = false;
return executeTask.ContinueWith(
(task, state) =>
static (Task task, object state) =>
{
Debug.Assert(!task.IsCanceled, "Task should not be canceled");
var parameters = (Tuple<TdsParser, TdsParserStateObject, SqlInternalConnectionTds>)state;
Expand Down Expand Up @@ -9059,7 +9059,7 @@ internal Task TdsExecuteRPC(SqlCommand cmd, _SqlRPC[] rpcArray, int timeout, boo
if (releaseConnectionLock)
{
task.ContinueWith(
(_, state) => ((SqlInternalConnectionTds)state)._parserLock.Release(),
static (Task _, object state) => ((SqlInternalConnectionTds)state)._parserLock.Release(),
state: _connHandler,
TaskScheduler.Default
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,16 @@ internal Task ExecuteFlush()
}
else
{
return AsyncHelper.CreateContinuationTask(writePacketTask, () => { HasPendingData = true; _messageStatus = 0; });
return AsyncHelper.CreateContinuationTaskWithState(
task: writePacketTask,
state: this,
onSuccess: static (object state) =>
{
TdsParserStateObject stateObject = (TdsParserStateObject)state;
stateObject.HasPendingData = true;
stateObject._messageStatus = 0;
}
);
}
}
}
Expand Down
Loading