Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Support Encoding devirtualization #9276

Merged
merged 1 commit into from
Mar 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/mscorlib/src/System/Text/ASCIIEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ namespace System.Text
[Serializable]
public class ASCIIEncoding : Encoding
{
// Allow for devirtualization (see https://github.com/dotnet/coreclr/pull/9230)
[Serializable]
internal sealed class ASCIIEncodingSealed : ASCIIEncoding { }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: private?


// Used by Encoding.ASCII for lazy initialization
// The initialization code will not be run until a static member of the class is referenced
internal static readonly ASCIIEncoding s_default = new ASCIIEncoding();
internal static readonly ASCIIEncodingSealed s_default = new ASCIIEncodingSealed();

public ASCIIEncoding() : base(Encoding.CodePageASCII)
{
Expand Down
9 changes: 8 additions & 1 deletion src/mscorlib/src/System/Text/UTF8Encoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,16 @@ public class UTF8Encoding : Encoding

private const int UTF8_CODEPAGE = 65001;

// Allow for devirtualization (see https://github.com/dotnet/coreclr/pull/9230)
[Serializable]
internal sealed class UTF8EncodingSealed : UTF8Encoding
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: private?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its exposed via Encoding as

Encoding UTF8 => UTF8Encoding.s_default;

so it needs to be able to see the class; else s_default being that type will be a compile error; if the s_default field was UTF8Encoding it might be too much of a stretch to infer it was actually UTF8EncodingSealed as the type would have to store what it had actually been initialised to?

Don't know if the devirtualization extends to inspecting what a readonly static actually is?

{
public UTF8EncodingSealed() : base(encoderShouldEmitUTF8Identifier: true) { }
}

// Used by Encoding.UTF8 for lazy initialization
// The initialization code will not be run until a static member of the class is referenced
internal static readonly UTF8Encoding s_default = new UTF8Encoding(encoderShouldEmitUTF8Identifier: true);
internal static readonly UTF8EncodingSealed s_default = new UTF8EncodingSealed();

// Yes, the idea of emitting U+FEFF as a UTF-8 identifier has made it into
// the standard.
Expand Down