Skip to content

Address runtime async TaskScheduler TODO#129161

Open
jakobbotsch wants to merge 1 commit into
dotnet:mainfrom
jakobbotsch:task-scheduler-todo
Open

Address runtime async TaskScheduler TODO#129161
jakobbotsch wants to merge 1 commit into
dotnet:mainfrom
jakobbotsch:task-scheduler-todo

Conversation

@jakobbotsch

Copy link
Copy Markdown
Member

Avoid an unnecessary allocation for the TaskScheduler case.

Avoid an unnecessary allocation for the TaskScheduler case.
Copilot AI review requested due to automatic review settings June 9, 2026 09:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the non-default TaskScheduler await-continuation path so CoreCLR’s runtime async continuation code can schedule/invoke the continuation without allocating a TaskSchedulerAwaitTaskContinuation instance, while preserving the existing inline-vs-queue behavior.

Changes:

  • Extracts the “inline or schedule onto non-default TaskScheduler” logic into TaskSchedulerAwaitTaskContinuation.RunOrScheduleAction(...).
  • Refactors AwaitTaskContinuation.CreateTask(...) to a protected static helper that takes an explicit ExecutionContext? argument.
  • Updates CoreCLR runtime async continuation sites to call the new helper directly (removing the prior TODO and per-continuation object allocation).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/TaskContinuation.cs Adds RunOrScheduleAction helper and refactors task creation to avoid needing an instance for the TaskScheduler path.
src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeAsyncTaskContinuation.cs Uses RunOrScheduleAction directly instead of allocating TaskSchedulerAwaitTaskContinuation.
src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncHelpers.CoreCLR.cs Uses RunOrScheduleAction directly in the captured-TaskScheduler continuation follow-up path.

bool inlineIfPossible = allowInlining &&
(TaskScheduler.InternalCurrent == scheduler || Thread.CurrentThread.IsThreadPoolThread);

// Create the continuation task task. If we're allowed to inline, try to do so.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants