Skip to content

ActivePatterns: Compilation issues (memory and speed) #18425

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

Open
Thorium opened this issue Mar 28, 2025 · 6 comments
Open

ActivePatterns: Compilation issues (memory and speed) #18425

Thorium opened this issue Mar 28, 2025 · 6 comments
Assignees
Labels
Area-Compiler-Optimization The F# optimizer, release code gen etc. Feature Improvement
Milestone

Comments

@Thorium
Copy link
Contributor

Thorium commented Mar 28, 2025

I'm trying to compile a little simple F# file I made, using the latest released stable .NET.
Zip attached

ClassLibrary1.zip

Expected behaviour

Either compile or fail in a meaningful manner.
F# should be fast to compile a single file, right?

Actual behaviour

I have no idea what is going on.

Related information

Provide any related information (optional):

  • Operating system: Windows 11
  • .NET Runtime kind (.NET Core, .NET Framework, Mono): Net 9.0,.201
  • Editing Tools (e.g. Visual Studio Version, Visual Studio): Dotnet.exe or VS 2022 Professional 17.13.5.
@Thorium
Copy link
Contributor Author

Thorium commented Mar 28, 2025

This is somehow related to active-pattern usage:

match
| A
| B
| C
| D
...
| Y
| Z when (something) -> ... // out of memory

But if the when-conditions break the match often, (even with identical "something") then it works:

match
| A when (something) -> ...
| B when (something) -> ...
| C when (something) -> ...
| D when (something) -> ...
| E when (something) -> ...
| F when (something) -> ...
...
| Z when (something) -> ...

@Thorium Thorium changed the title Compilation issues (memory and speed) ActivePatterns: Compilation issues (memory and speed) Mar 28, 2025
@github-actions github-actions bot added this to the Backlog milestone Mar 28, 2025
@T-Gro
Copy link
Member

T-Gro commented Mar 31, 2025

Bisecting tells me the issue (I assume it is an infinite loop, but could also be something too slow and I was not patient enough to see it finish) is within

// A = "x" && A = "x" ->  A = "x"
    let ``remove duplicate condition`` (e:Expression) =

function's body.
The code above it, despite also having big patterns (e.g. ~150 clauses for distribute_complement) compile fine.

@Thorium
Copy link
Contributor Author

Thorium commented Mar 31, 2025

Yes. Although if you remove that code totally, and then remove the other duplicate intermediate "when"s, then it will fail as well:

Image

The annoying thing here is that you'd expect the code to be more efficient when removing the "unnecessary" whens.

@T-Gro
Copy link
Member

T-Gro commented Mar 31, 2025

The presence of a when can disable certain optimizations due to having the possibility of a side effect.

This --times:files.csv output indeed tells that there is something in optimizations for this file.
Worth mentioning that even the typechecking and TAST->IL transformations (=IlxGen) are super slow considering a single-file project here.

Image

(note that I have disabled all optional optimizations just to get the compilation done, there likely are other bottlenecks if the full optimizations would be running)

@abonie abonie added Feature Improvement Area-Compiler-Optimization The F# optimizer, release code gen etc. and removed Bug Needs-Triage labels Mar 31, 2025
@T-Gro
Copy link
Member

T-Gro commented Apr 28, 2025

There is definitely a missing reduction step to put those shared conditions together, which is also what might be slowing down the compilation's optimization phase.

It correctly unifies the type check / type cast as far as I can see, but it still multiplities the when clause contents instead of unifying it. Strange.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compiler-Optimization The F# optimizer, release code gen etc. Feature Improvement
Projects
Status: New
Development

No branches or pull requests

3 participants