You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Consider StartImmediateAsTask instead of StartAsTask to prevent a thread hop #135
In PR #114 (adding support for Async in Bind), @bartelinkraised the question whether it would be better to use StartImmediateAsTask instead of StartAsTask.
Specifically, it is about this part:
let mutableawaiter=
Async
.StartAsTask(asyncSource, cancellationToken = sm.Data.cancellationToken)
.GetAwaiter()
member inlinethis.Bind(computation:Async<'TResult1>,continuation:('TResult1 -> TaskCode<'TOverall, 'TResult2>)):TaskCode<'TOverall,'TResult2>=
this.Bind(Async.StartAsTask computation, continuation)
For comparison, see what was done in async2, which was poised to be the new async based on resumable code, like task is, but unlike task cold-started like the current async.
So, we have two scenarios:
use StartAsTask to get similar semantics as the task CE, but this causes a thread hop
use StartImmediateAsTask, which uses the current context
I'm not aware of any downside of using the latter. It definitely saves a thread switch in the general case (note that the thread pool may not always cause a physical thread switch).
That comment by @dsyme basically suggests not to use StartAsStask. Tbh, I didn't know it started in the thread pool, we have Async.Start already, I figured, as a task, it behaves like task: stay in the same context.
Thanks for bringing this up @bartelink. It required some digging, and writing a context-switching test case, but I'm now convinced this is a bad situation to be in (tbh, as Don mentioned already, these methods should be differently named). Will write a PR to fix it.
Activity
abelbraaksma commentedon Dec 19, 2022
This seems relevant as well: dotnet/fsharp#11043 (comment)
That comment by @dsyme basically suggests not to use
StartAsStask. Tbh, I didn't know it started in the thread pool, we haveAsync.Startalready, I figured, as a task, it behaves liketask: stay in the same context.I figured, maybe just report as a bug to F#: dotnet/fsharp#14490
abelbraaksma commentedon Dec 19, 2022
Thanks for bringing this up @bartelink. It required some digging, and writing a context-switching test case, but I'm now convinced this is a bad situation to be in (tbh, as Don mentioned already, these methods should be differently named). Will write a PR to fix it.
Bump version to 4.2.1
abelbraaksma commentedon Dec 28, 2022
The thread hop in F# Core
taskhas been fixed. We should follow suit. See dotnet/fsharp#14499.