-
Notifications
You must be signed in to change notification settings - Fork 785
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
Optimize simple range mappings: [for n in start..finish -> f n]
, &c.
#16832
Merged
T-Gro
merged 13 commits into
dotnet:main
from
brianrourkeboll:computed-collections-for-n-in-range
Mar 19, 2024
Merged
Optimize simple range mappings: [for n in start..finish -> f n]
, &c.
#16832
T-Gro
merged 13 commits into
dotnet:main
from
brianrourkeboll:computed-collections-for-n-in-range
Mar 19, 2024
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
❗ Release notes required
|
…-collections-for-n-in-range
…-collections-for-n-in-range
* Optimize simple mappings containing sequential expressions: ```fsharp [for n in start..finish do f (); …; yield n] ```
brianrourkeboll
commented
Mar 8, 2024
T-Gro
reviewed
Mar 11, 2024
T-Gro
reviewed
Mar 11, 2024
tests/FSharp.Compiler.ComponentTests/EmittedIL/ComputedCollections/ForNInRangeArrays.fs.il.bsl
Show resolved
Hide resolved
T-Gro
reviewed
Mar 11, 2024
T-Gro
approved these changes
Mar 12, 2024
psfinaki
approved these changes
Mar 12, 2024
This was referenced Mar 24, 2024
2 tasks
2 tasks
2 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Apply the optimizations from Better integral range lowering:
start..finish
,start..step..finish
#16650 to lists and arrays constructed from simple, direct range mappings, i.e. —[for n in start..finish -> n]
[for n in start..finish -> f n]
[for n in start..step..finish -> n]
[for n in start..step..finish -> f n]
[|for n in start..finish -> n|]
[|for n in start..finish -> f n|]
[|for n in start..step..finish -> n|]
[|for n in start..step..finish -> f n|]
— where
f
stands for any arbitrary transformation ofn
.This also applies to the syntax
[for n in start..finish do yield n]
, etc., when there is exactly oneyield
, and to[for n in start..finish do f (); …; yield n]
, for any depth of sequential expressions, as long as there is still exactly oneyield
.The generated loop IL for
[for n in start..finish -> n]
, etc., is identical to that generated for[start..finish]
.Benchmarks
The performance improvement looks to be the same as that in #16650 (i.e., up to 8×), as would be expected, since it's the same count and initialization loop code. I can run a more thorough suite if desired.
Checklist
Notes
This PR also optimizes simple mappings comprised of one or more sequential expressions like
[for n in start..finish do printfn "abc"; yield n]
, where there is exactly oneyield
(5d52462). We could theoretically go even further and detect a statically-known number ofyield
s > 1 and factor that back into the total iteration count, but that seems like a step too far for now.