From: Ben Adams Date: Fri, 7 Apr 2017 02:05:18 +0000 (+0100) Subject: Devirtualizable default encoding (dotnet/coreclr#10783) X-Git-Tag: submit/tizen/20210909.063632~11030^2~7384 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=9e929be731344385433aa4851b867f5de6f96dcb;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Devirtualizable default encoding (dotnet/coreclr#10783) Commit migrated from https://github.com/dotnet/coreclr/commit/97daa74ccbb052878135dec2e11ddac6b0219566 --- diff --git a/src/coreclr/src/mscorlib/src/System/Text/Encoding.cs b/src/coreclr/src/mscorlib/src/System/Text/Encoding.cs index aab9b4a..9217b88 100644 --- a/src/coreclr/src/mscorlib/src/System/Text/Encoding.cs +++ b/src/coreclr/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/coreclr/src/mscorlib/src/System/Text/UTF8Encoding.cs b/src/coreclr/src/mscorlib/src/System/Text/UTF8Encoding.cs index 078fb46..41f0b99 100644 --- a/src/coreclr/src/mscorlib/src/System/Text/UTF8Encoding.cs +++ b/src/coreclr/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.