You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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
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:
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
The generic context is an extra parameter that specifies the instantiation information - think of this as an exact instantiation MethodDesc.
The text was updated successfully, but these errors were encountered: