dotnet / dotnet/fsharp

Integral range optimizations in resumable code computation expressions

Open
#17,253 0 comments 2 reactions 0 assignees View on GitHub
Area-Compiler-Optimization Feature Improvement
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 11h
Merged PRs (30d)
131

Description

#13573 would have included one optimization which #16650, et seq., did not, namely the optimization of `for`-loops over integral ranges with steps other than `1` and `-1` in resumable code computation expressions (like `task`). I figured I might as well record it here (cc @psfinaki).

It seems like it might be relatively straightforward to add this optimization now by wiring up the `IntegralRange` and `mkOptimizedRangeLoop` constructs exposed in #16650.

1. Supplement the existing match on `IntegerForLoopExpr` with another match on `CompiledForEachExpr` and `IntegralRange`:

https://github.com/dotnet/fsharp/blob/5fd6800a984c59267c406305c15e9f7efb1e8519/src/Compiler/Optimize/LowerStateMachines.fs#L485-L486

2. There is an existing translation using `mkIntegerForLoop`; add a new one that uses `mkOptimizedRangeLoop`:

https://github.com/dotnet/fsharp/blob/5fd6800a984c59267c406305c15e9f7efb1e8519/src/Compiler/Optimize/LowerStateMachines.fs#L692

However, the existing optimization for `for`-loops over `int32` ranges with steps of `1` or `-1` doesn't always seem to kick in — in fact, I haven't been able to get it to kick in at all, even when using the syntax `for n = start to finish do …` instead of `for n in start..finish do …`. So figuring out why that is, and what the TAST actually looks like by the time it gets to `LowerStateMachines.fs`, might end up being the harder problem to solve.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.