Skip to content

Conversation

@majocha
Copy link
Contributor

@majocha majocha commented Sep 9, 2025

OK so this is 100% Copilot generated, to keep it consistent, here comes Copilot generated description:

This pull request introduces a more efficient and targeted way to check if a local variable is used within an expression or decision tree during optimization in the F# compiler. The main improvement is the addition of a "cheap occurs check" (ExprUsesLocal and DecisionTreeUsesLocal) that avoids constructing full free-variable sets unless necessary, which should reduce unnecessary computation and improve performance in the optimizer. Several usages of the previous, more expensive free-variable analysis have been replaced with this new approach.

Optimization and Performance Improvements

  • Added new functions ExprUsesLocal and DecisionTreeUsesLocal to perform quick, conservative checks for variable usage within expressions and decision trees, avoiding full free-variable set construction unless required.
  • Updated logic in TryEliminateBinding and related optimizer routines to use the new occurrence checks instead of always calculating free-variable sets, which should improve performance and maintain correctness.

These changes collectively improve the efficiency of the optimizer, especially in large codebases, by reducing the computational cost of unused variable elimination and related analyses.## Description

This partially addresses #12421

Checklist

  • Test cases added

  • Performance benchmarks added in case of performance changes

  • Release notes entry updated:

    Please make sure to add an entry with short succinct description of the change as well as link to this pull request to the respective release notes file, if applicable.

    Release notes files:

    • If anything under src/Compiler has been changed, please make sure to make an entry in docs/release-notes/.FSharp.Compiler.Service/<version>.md, where <version> is usually "highest" one, e.g. 42.8.200
    • If language feature was added (i.e. LanguageFeatures.fsi was changed), please add it to docs/release-notes/.Language/preview.md
    • If a change to FSharp.Core was made, please make sure to edit docs/release-notes/.FSharp.Core/<version>.md where version is "highest" one, e.g. 8.0.200.

    Information about the release notes entries format can be found in the documentation.
    Example:

    If you believe that release notes are not necessary for this PR, please add NO_RELEASE_NOTES label to the pull request.

@github-actions
Copy link
Contributor

github-actions bot commented Sep 9, 2025

❗ Release notes required

@majocha,

Caution

No release notes found for the changed paths (see table below).

Please make sure to add an entry with an informative description of the change as well as link to this pull request, issue and language suggestion if applicable. Release notes for this repository are based on Keep A Changelog format.

The following format is recommended for this repository:

* <Informative description>. ([PR #XXXXX](https://github.com/dotnet/fsharp/pull/XXXXX))

See examples in the files, listed in the table below or in th full documentation at https://fsharp.github.io/fsharp-compiler-docs/release-notes/About.html.

If you believe that release notes are not necessary for this PR, please add NO_RELEASE_NOTES label to the pull request.

You can open this PR in browser to add release notes: open in github.dev

Change path Release notes path Description
src/Compiler docs/release-notes/.FSharp.Compiler.Service/10.0.100.md No release notes found or release notes format is not correct

Comment on lines +45 to +52
let ``let-chains of nested apps compile under --optimize+`` depth =
let src = Gen.nestedLetApps depth
FSharp src
|> withOptions [ "--optimize+"; "--times" ]
|> asExe
|> ignoreWarnings
|> compile
|> shouldSucceed
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running this test on main to compare shows that the fix indeed works quite well. 5000 depth gives around 15 seconds Optimize phase on main, compared to below one sec here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although this is an edge case. Hard to tell how much this will improve real life build times.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Build fsc in Release, measure how long it takes, then recompile using the new artifacts.

@majocha
Copy link
Contributor Author

majocha commented Sep 10, 2025

Yeah, I don't see an improvement IRL that would stand out above the noise. I don't think the non obvious gain justifies the complication.

@majocha majocha closed this Sep 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants