Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Support custom awaitable types#2349

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
timcassell wants to merge18 commits intodotnet:master
base:master
Choose a base branch
Loading
fromtimcassell:custom-async-method-builder

Conversation

timcassell
Copy link
Collaborator

This is a prototype for supporting any type of awaitable, stemmed from the discussion in#2111. (It might even be able to supersede that PR, since I am seeing even betterTask benchmark results).

New interfaces are used for BDN to know how to make a type awaitable.

publicinterfaceIAwaitableAdapter<TAwaitable,TAwaiter>whereTAwaiter:ICriticalNotifyCompletion{publicTAwaiterGetAwaiter(refTAwaitableawaitable);publicboolGetIsCompleted(refTAwaiterawaiter);publicvoidGetResult(refTAwaiterawaiter);}publicinterfaceIAwaitableAdapter<TAwaitable,TAwaiter,TResult>whereTAwaiter:ICriticalNotifyCompletion{publicTAwaiterGetAwaiter(refTAwaitableawaitable);publicboolGetIsCompleted(refTAwaiterawaiter);publicTResultGetResult(refTAwaiterawaiter);}

An adapter type is added to the config to make the benchmark await it, along with an optional async method builder adapter (more on that below).

Example

[Config(typeof(Config))][MemoryDiagnoser(false)]publicclassBenchmarks{privateclassConfig:ManualConfig{publicConfig(){AddAsyncAdapter(typeof(YieldAwaitableAdapter));AddAsyncAdapter(typeof(PromiseAdapter),typeof(PromiseMethodBuilderAdapter));AddAsyncAdapter(typeof(PromiseAdapter<>),typeof(PromiseMethodBuilderAdapter));AddJob(Job.Default);}}[Benchmark]publicYieldAwaitablePureYield()=>Task.Yield();[Benchmark]publicasyncPromisePromiseVoid(){awaitTask.Yield();}[Benchmark]publicasyncPromise<long>PromiseLong(){awaitTask.Yield();returndefault;}[Benchmark]publicasyncTaskTaskVoid(){awaitTask.Yield();}[Benchmark]publicasyncTask<long>TaskLong(){awaitTask.Yield();returndefault;}}publicstructYieldAwaitableAdapter:IAwaitableAdapter<YieldAwaitable,YieldAwaitable.YieldAwaiter>{publicYieldAwaitable.YieldAwaiterGetAwaiter(refYieldAwaitableawaitable)=>awaitable.GetAwaiter();publicboolGetIsCompleted(refYieldAwaitable.YieldAwaiterawaiter)=>awaiter.IsCompleted;publicvoidGetResult(refYieldAwaitable.YieldAwaiterawaiter)=>awaiter.GetResult();}publicstructPromiseAdapter:IAwaitableAdapter<Promise,PromiseAwaiterVoid>{publicPromiseAwaiterVoidGetAwaiter(refPromiseawaitable)=>awaitable.GetAwaiter();publicboolGetIsCompleted(refPromiseAwaiterVoidawaiter)=>awaiter.IsCompleted;publicvoidGetResult(refPromiseAwaiterVoidawaiter)=>awaiter.GetResult();}publicstructPromiseAdapter<T>:IAwaitableAdapter<Promise<T>,PromiseAwaiter<T>,T>{publicPromiseAwaiter<T>GetAwaiter(refPromise<T>awaitable)=>awaitable.GetAwaiter();publicboolGetIsCompleted(refPromiseAwaiter<T>awaiter)=>awaiter.IsCompleted;publicTGetResult(refPromiseAwaiter<T>awaiter)=>awaiter.GetResult();}publicstructPromiseMethodBuilderAdapter:IAsyncMethodBuilderAdapter{privatestructEmptyStruct{}privatePromiseMethodBuilder<EmptyStruct>_builder;publicvoidCreateAsyncMethodBuilder()=>_builder=PromiseMethodBuilder<EmptyStruct>.Create();publicvoidStart<TStateMachine>(refTStateMachinestateMachine)whereTStateMachine:IAsyncStateMachine=>_builder.Start(refstateMachine);publicvoidAwaitOnCompleted<TAwaiter,TStateMachine>(refTAwaiterawaiter,refTStateMachinestateMachine)whereTAwaiter:ICriticalNotifyCompletionwhereTStateMachine:IAsyncStateMachine=>_builder.AwaitUnsafeOnCompleted(refawaiter,refstateMachine);publicvoidSetResult(){_builder.SetResult(default);_builder.Task.Forget();}publicvoidSetStateMachine(IAsyncStateMachinestateMachine)=>_builder.SetStateMachine(stateMachine);}

And the results:

MethodMeanErrorStdDevAllocated
PureYield669.9 ns13.33 ns19.54 ns-
PromiseVoid1,215.9 ns24.16 ns28.76 ns32 B
PromiseLong1,161.9 ns18.90 ns17.68 ns32 B
TaskVoid1,396.2 ns6.82 ns6.38 ns112 B
TaskLong1,443.0 ns23.52 ns22.00 ns112 B

Woah, we can actually measure the cost ofawait Task.Yield() now, which was previously impossible!

IAsyncMethodBuilderAdapter is an optional adapter type used to override the defaultAsyncTaskMethodBuilder, because some builders use more efficient await strategies for known awaiter types. For example, I measured ~100ns slowdown withPromise when using the defaultAsyncTaskMethodBuilder instead of using the customPromiseMethodBuilder.

Currently in this prototype, I only supported custom awaits with the config, no attribute support. If we want attribute support, I'm not sure how it should look like. Also, I haven't updated theInProcessEmitToolchain with this system yet, because I want some feedback first.

Also, I did test this with theInProcessNoEmitToolchain with NativeAOT, and it worked flawlessly.

cc@YegorStepanov

Force async unroll factor to 1.Support async IterationSetup/IterationCleanup.
# Conflicts:#tests/BenchmarkDotNet.IntegrationTests/InProcessTest.cs
…ead-new# Conflicts:#tests/BenchmarkDotNet.IntegrationTests/InProcessTest.cs
# Conflicts:#src/BenchmarkDotNet/Engines/IEngine.cs#src/BenchmarkDotNet/Toolchains/InProcess.Emit.Implementation/ConsumableTypeInfo.cs#src/BenchmarkDotNet/Toolchains/InProcess.Emit.Implementation/Emitters/RunnableEmitter.cs
# Conflicts:#src/BenchmarkDotNet/Toolchains/InProcess/BenchmarkAction.cs#src/BenchmarkDotNet/Toolchains/InProcess/BenchmarkActionFactory.cs#src/BenchmarkDotNet/Toolchains/InProcess/BenchmarkActionFactory_Implementations.cs#src/BenchmarkDotNet/Toolchains/InProcess/Emit/Implementation/Emitters/TaskConsumeEmitter.cs#src/BenchmarkDotNet/Toolchains/InProcess/InProcessRunner.cs#tests/BenchmarkDotNet.IntegrationTests/InProcessTest.cs
Use async consumers in toolchains (WIP).
Handle overhead without duplicating the state machine code.
Split AsyncBenchmarkRunner workload and overhead.
Use abstract base class without generics.Explicitly specify overhead awaitable/awaiter types.Use awaitable types in InProcessNoEmitToolchain.
Support multiple generic arguments in adapters.No need to pass awaitable type to config, only pass awaitable adapter type.
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

1 participant
@timcassell

[8]ページ先頭

©2009-2025 Movatter.jp