Devirtualizable default encoding (dotnet/coreclr#10783)
authorBen Adams <thundercat@illyriad.co.uk>
Fri, 7 Apr 2017 02:05:18 +0000 (03:05 +0100)
committerJan Kotas <jkotas@microsoft.com>
Fri, 7 Apr 2017 02:05:18 +0000 (19:05 -0700)
Commit migrated from https://github.com/dotnet/coreclr/commit/97daa74ccbb052878135dec2e11ddac6b0219566

src/coreclr/src/mscorlib/src/System/Text/Encoding.cs
src/coreclr/src/mscorlib/src/System/Text/UTF8Encoding.cs

index aab9b4a..9217b88 100644 (file)
@@ -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
index 078fb46..41f0b99 100644 (file)
@@ -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.