dotnet / dotnet/fsharp

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

Open
#16,166 1 comment 3 reactions 0 assignees View on GitHub
Area-Compiler-Optimization Bug Impact-Medium
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 11h
Merged PRs (30d)
131

Description

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:
```fsharp
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](https://github.com/dotnet/fsharp/assets/3923587/ced19a9f-7bab-4c07-9741-03b16c977c27)

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

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.