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

No sharing of generic instantiations using reference types #531

Open
1 of 3 tasks
drcjt opened this issue Sep 17, 2024 · 0 comments
Open
1 of 3 tasks

No sharing of generic instantiations using reference types #531

drcjt opened this issue Sep 17, 2024 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@drcjt
Copy link
Owner

drcjt commented Sep 17, 2024

For a generic class that has > 1 instantiation using reference types then separate copies of the methods will be compiled rather than sharing the same code.

Example where Person and Order are reference types:

var personList = new List<Person>();
Console.WriteLine(personList.Count);

var orderList = new List<Order>();
Console.WriteLine(orderList.Count);

In this case two copies of the code for List.Count will be included in the generated assembly code when the same code could be shared for both instantiations.

This will involve looking into the canonical forms used in the dotnet type system.

Quote from Michal Strehovsky:

"https://github.com/dotnet/coreclr/blob/775003a4c72f0acc37eab84628fcef541533ba4e/Documentation/botr/type-loader.md has a short paragraph about it. It has to do with sharing code (and potentially other data structures) of generic instantiations. Generic instantiations that map to the same canonical form can do the sharing between each other."

Also see unit tests in native aot ilc here - https://github.com/dotnet/corert/blob/c6af4cfc8b625851b91823d9be746c4f7abdc667/src/ILCompiler.TypeSystem/tests/CanonicalizationTests.cs#L12

Very good explanation of shared generics can be seen here - dotnet/corert#7248
Also see original shared generics in corert using cpp code gen here - dotnet/corert#6467

Design document on shared generics in dotnet:
https://github.com/dotnet/runtime/blob/main/docs/design/coreclr/botr/shared-generics.md

Post explaining "CLR Generics and Code Sharing" -> https://learn.microsoft.com/en-gb/archive/blogs/joelpob/clr-generics-and-code-sharing

  • Canonicalisation of code
  • Pass Generic Context to shared generic code
  • Use Generic Context in shared generic code

The generic context is an extra parameter that specifies the instantiation information - think of this as an exact instantiation MethodDesc.

@drcjt drcjt added the enhancement New feature or request label Sep 17, 2024
@drcjt drcjt self-assigned this Oct 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant