-
Notifications
You must be signed in to change notification settings - Fork 796
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
While loop pattern in Async builder allocates a lot #8668
Comments
The comment in the code appears to be lying 🙂 https://github.com/dotnet/fsharp/blob/master/src/fsharp/FSharp.Core/async.fs#L588 cc @dsyme |
Note that synchronous loops can be made efficient like this: async {
let mutable i = 0
do
while i < "some length" do
i <- i + 1
return i
} |
This is also an issue in Resumable CEs such as IcedTasks. [<Benchmark>]
member x.CancellableTask() =
let cTask = cancellableTask {
let mutable i = 0
while i < x.Length do
i <- i + 1
return i
}
cTask CancellationToken.None
[<Benchmark>]
member x.CancellableTask_syncDoBlockTrick() =
let cTask = cancellableTask {
let mutable i = 0
do
while i < x.Length do
i <- i + 1
return i
}
cTask CancellationToken.None
[<Benchmark>]
member x.CancellableTask_async() =
let cTask = cancellableTask {
let mutable i = 0
while i < x.Length do
do! Task.Yield()
i <- i + 1
return i
}
cTask CancellationToken.None |
So I think I'm actually running into #12839 (comment) however I'm not seeing the [Benchmark(147, "C:\\Users\\jimmy\\Repositories\\public\\TheAngryByrd\\IcedTasks\\benchmarks\\FSharpBenchmarks\\LoopBenchmarks.fs")]
public Task<int> CancellableTask()
{
\u0024LoopBenchmarks.cTask\u0040149 cTask149 = new \u0024LoopBenchmarks.cTask\u0040149();
ref \u0024LoopBenchmarks.cTask\u0040149 local1 = ref cTask149;
local1.x = this;
\u0024LoopBenchmarks.cTask\u0040149 sm = local1;
FSharpFunc<CancellationToken, Task<int>> fsharpFunc = (FSharpFunc<CancellationToken, Task<int>>) new \u0024LoopBenchmarks.cTask\u0040149\u002D1(sm);
CancellationToken none = CancellationToken.None;
ref CancellationToken local2 = ref none;
return fsharpFunc.Invoke(local2);
}
[Benchmark(163, "C:\\Users\\jimmy\\Repositories\\public\\TheAngryByrd\\IcedTasks\\benchmarks\\FSharpBenchmarks\\LoopBenchmarks.fs")]
public Task<int> CancellableTask2()
{
ResumableCode<CancellableTasks.CancellableTaskStateMachineData<int>, int> resumableCode = new ResumableCode<CancellableTasks.CancellableTaskStateMachineData<int>, int>((object) new \u0024LoopBenchmarks.CancellableTask2\u0040165(this), __methodptr(Invoke));
if (false)
return ((FSharpFunc<CancellationToken, Task<int>>) null).Invoke(CancellationToken.None);
throw new Exception("sorry lol");
} |
Repro steps
The problem is in this code pattern:
Benchmark code:
Result:
Essentially the problem is that the loop internally turns into this:
Expected behavior
Allocations shouldn't depend(?) on the number of loops.
Actual behavior
Allocations depend on the number of loops.
Known workarounds
Using recursion:
Related information
Using
TaskBuilder.fs
helps, but not that much:https://gist.github.com/grishace/83f540cb299867e94145551931fcbcb1
.NET Core 3.1
The text was updated successfully, but these errors were encountered: