Skip to content
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

Compiler eagerly imports IL type defs from unopened namespaces consisting of a single name #16166

Open
auduchinok opened this issue Oct 24, 2023 · 1 comment
Labels
Area-Compiler-Optimization The F# optimizer, release code gen etc. Bug Impact-Medium (Internal MS Team use only) Describes an issue with moderate impact on existing code.
Milestone

Comments

@auduchinok
Copy link
Member

auduchinok commented Oct 24, 2023

When the compiler reads IL metadata, it reads type defs lazily, so types aren't read and kept in memory unless they are referenced somewhere or their containing namespace is imported. Instead of immediately importing types, special placeholders (ILPreTypeDef) are created.

It turns out that if a namespace consist of a single name, then this optimization isn't applied, and its types are eagerly imported. Moreover, this happens during a background builder creation, before any file in a project is analyzed.

This should affect the startup time and memory consumption of the compiler.

I've tested it on a referenced assembly with a single type T, and it depends on the T namespace:

  • Ns.T is imported eagerly
  • Ns1.Ns2.T is not imported

The following source code was used in the tests, no namespaces were imported:

module Module

let t: T = null

Here's a screenshot of the stack trace from the test with Ns.T type:

Screenshot 2023-10-23 at 21 20 38

This problem reproduces for other types, so types from namespaces like System are always imported on startup:

Screenshot 2023-10-24 at 15 10 15
@github-actions github-actions bot added this to the Backlog milestone Oct 24, 2023
@auduchinok auduchinok changed the title Compiler imports IL type defs from unopened namespaces consisting of one name Compiler eagerly imports IL type defs from unopened namespaces consisting of a single name Oct 24, 2023
@auduchinok
Copy link
Member Author

auduchinok commented Oct 24, 2023

There's another case: when a type is referenced, all other types in the same namespace are eagerly created too.
For example, Lazy is referenced during TcGlobals creation:

let lazy_tcr = findSysTyconRef sys "Lazy`1"

This forces reading/creating of all other types in the System namespace:

Screenshot 2023-10-24 at 15 26 30

This should affect the analysis time and memory consumption of the compiler.

@abonie abonie added Impact-Medium (Internal MS Team use only) Describes an issue with moderate impact on existing code. Area-Compiler-Optimization The F# optimizer, release code gen etc. and removed Needs-Triage labels Oct 30, 2023
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. Bug Impact-Medium (Internal MS Team use only) Describes an issue with moderate impact on existing code.
Projects
Status: New
Development

No branches or pull requests

2 participants