Skip to content

Commit

Permalink
Perf: Static delegates (#1060)
Browse files Browse the repository at this point in the history
* netcore static lambda rework

* minor netcore api cleanup

* netfx static lambda rework

* netcore add strongbox to avoid boxing

* fixup 2 anonymous typed lambdas to be explicit
  • Loading branch information
Wraith2 committed Jun 11, 2021
1 parent 1e8ce97 commit f17bba2
Show file tree
Hide file tree
Showing 13 changed files with 561 additions and 363 deletions.
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 @@ -2152,7 +2152,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) =>
{
onSuccess(continueState);
completion.SetResult(null);
Expand All @@ -81,12 +81,12 @@ internal static Task CreateContinuationTaskWithState(Task task, object state, Ac
}

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 Task CreateContinuationTaskWithState(Task task, object state, Ac
)
{
task.ContinueWith(
tsk =>
(Task tsk, object state2) =>
{
if (tsk.Exception != null)
{
Expand All @@ -156,7 +156,7 @@ internal static Task CreateContinuationTaskWithState(Task task, object state, Ac
}
try
{
onFailure?.Invoke(exc, state);
onFailure?.Invoke(exc, state2);
}
finally
{
Expand All @@ -167,7 +167,7 @@ internal static Task CreateContinuationTaskWithState(Task task, object state, Ac
{
try
{
onCancellation?.Invoke(state);
onCancellation?.Invoke(state2);
}
finally
{
Expand All @@ -178,14 +178,16 @@ internal static Task CreateContinuationTaskWithState(Task task, object state, Ac
{
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 @@ -8842,7 +8842,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 @@ -9068,7 +9068,7 @@ internal Task TdsExecuteSQLBatch(string text, int timeout, SqlNotificationReques
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 @@ -1016,7 +1016,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

0 comments on commit f17bba2

Please sign in to comment.