Skip to content

Instantly share code, notes, and snippets.

@idg10
Created June 7, 2023 09:57
Show Gist options
  • Save idg10/8ab81fa4c567702040a877c84c1e99ee to your computer and use it in GitHub Desktop.
Save idg10/8ab81fa4c567702040a877c84c1e99ee to your computer and use it in GitHub Desktop.
Benchmarking two different approaches to IAsyncEnumerable.Switch, forcing non-synchronous ValueTask completion
[MemoryDiagnoser]
public class SwitchWithBlockingSources
{
public readonly static IAsyncEnumerable<IAsyncEnumerable<int>> source = AsyncEnumerable
.Range(0, 1000)
.SelectAwait(async i =>
{
await Task.Yield();
return AsyncEnumerable
.Range(0, 1000)
.SelectAwait(async i =>
{
await Task.Yield();
return i;
});
});
[Benchmark]
public async ValueTask<int> IterativeSingleLogicalThread()
{
return await source.Switch().LastAsync();
}
[Benchmark]
public async ValueTask<int> ConcurrentLogicalThreads()
{
return await source.Switch2().LastAsync();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment