From 97daa74ccbb052878135dec2e11ddac6b0219566 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Fri, 7 Apr 2017 03:05:18 +0100 Subject: [PATCH] Devirtualizable default encoding (#10783) --- src/mscorlib/src/System/Text/Encoding.cs | 24 +++++------------------- src/mscorlib/src/System/Text/UTF8Encoding.cs | 3 +-- 2 files changed, 6 insertions(+), 21 deletions(-) diff --git a/src/mscorlib/src/System/Text/Encoding.cs b/src/mscorlib/src/System/Text/Encoding.cs index aab9b4a..9217b88 100644 --- a/src/mscorlib/src/System/Text/Encoding.cs +++ b/src/mscorlib/src/System/Text/Encoding.cs @@ -85,7 +85,11 @@ namespace System.Text [Serializable] public abstract class Encoding : ICloneable { - private static Encoding defaultEncoding; + // For netcore we use UTF8 as default encoding since ANSI isn't available + private static readonly UTF8Encoding.UTF8EncodingSealed s_defaultEncoding = new UTF8Encoding.UTF8EncodingSealed(encoderShouldEmitUTF8Identifier: false); + + // Returns an encoding for the system's current ANSI code page. + public static Encoding Default => s_defaultEncoding; // // The following values are from mlang.idl. These values @@ -1223,24 +1227,6 @@ namespace System.Text return new DefaultDecoder(this); } - private static Encoding CreateDefaultEncoding() - { - // defaultEncoding should be null if we get here, but we can't - // assert that in case another thread beat us to the initialization - - Encoding enc; - - // For netcore we use UTF8 since ANSI isn't available - enc = new UTF8Encoding.UTF8EncodingSealed(encoderShouldEmitUTF8Identifier: false); - - // This method should only ever return one Encoding instance - return Interlocked.CompareExchange(ref defaultEncoding, enc, null) ?? enc; - } - - // Returns an encoding for the system's current ANSI code page. - - public static Encoding Default => defaultEncoding ?? CreateDefaultEncoding(); - // Returns an Encoder object for this encoding. The returned object // can be used to encode a sequence of characters into a sequence of bytes. // Contrary to the GetBytes family of methods, an Encoder can diff --git a/src/mscorlib/src/System/Text/UTF8Encoding.cs b/src/mscorlib/src/System/Text/UTF8Encoding.cs index 078fb46..41f0b99 100644 --- a/src/mscorlib/src/System/Text/UTF8Encoding.cs +++ b/src/mscorlib/src/System/Text/UTF8Encoding.cs @@ -57,13 +57,12 @@ namespace System.Text [Serializable] internal sealed class UTF8EncodingSealed : UTF8Encoding { - public UTF8EncodingSealed() : base(encoderShouldEmitUTF8Identifier: true) { } public UTF8EncodingSealed(bool encoderShouldEmitUTF8Identifier) : base(encoderShouldEmitUTF8Identifier) { } } // 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 UTF8EncodingSealed s_default = new UTF8EncodingSealed(); + internal static readonly UTF8EncodingSealed s_default = new UTF8EncodingSealed(encoderShouldEmitUTF8Identifier: true); // Yes, the idea of emitting U+FEFF as a UTF-8 identifier has made it into // the standard. -- 2.7.4