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

Enable linker to trim code paths for never instantiated derived types? #1794

Open
stephentoub opened this issue Feb 1, 2021 · 0 comments
Open

Comments

@stephentoub
Copy link
Member

In the core libraries, some types differentiate behaviors based on whether the instance is the base class in which the code is written or a derived type.

For example, the System.Random type's default ctor (which is the most commonly used one in production code) now checks to see whether this.GetType() == typeof(Random) or whether it's actually derived, using a different algorithm for compatibility if it's derived:
https://github.com/dotnet/runtime/blob/67cca7a82062e1581edb2bced6a1428015a74eef/src/libraries/System.Private.CoreLib/src/System/Random.cs#L23-L27
But that means the linker currently sees both implementations as being used and won't trim out the one for derived types. If the linker could prove that no Random-derived types were in the whole program or were never instantiated, it could strip out the relevant branch and associated tree of code.

Similar patterns emerge in other types. For example, streams like MemoryStream that need to be compatible as new virtual methods are added will often do a type check to prefer the optimized implementation when it's not a derived type:
https://github.com/dotnet/runtime/blob/67cca7a82062e1581edb2bced6a1428015a74eef/src/libraries/System.Private.CoreLib/src/System/IO/MemoryStream.cs#L483-L495

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants