From: Dan Moseley Date: Mon, 10 Apr 2017 23:20:05 +0000 (-0700) Subject: empty array (dotnet/coreclr#10841) X-Git-Tag: submit/tizen/20210909.063632~11030^2~7322 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=5d71670153f2d3a5cb3659f8f48b87ce1e627e2d;p=platform%2Fupstream%2Fdotnet%2Fruntime.git empty array (dotnet/coreclr#10841) Make EmptyArray private in favor of Array.Empty Commit migrated from https://github.com/dotnet/coreclr/commit/8ffb46cd635cd9419cc26315d9eec64b976cbfb0 --- diff --git a/src/coreclr/src/mscorlib/src/System/Array.cs b/src/coreclr/src/mscorlib/src/System/Array.cs index 6e7615d..05c4804 100644 --- a/src/coreclr/src/mscorlib/src/System/Array.cs +++ b/src/coreclr/src/mscorlib/src/System/Array.cs @@ -1012,6 +1012,11 @@ namespace System this.CopyTo(array, (int)index); } + private static class EmptyArray + { + internal static readonly T[] Value = new T[0]; + } + [Pure] public static T[] Empty() { @@ -2777,8 +2782,3 @@ namespace System } } -// Useful in number of places that return an empty byte array to avoid unnecessary memory allocation. -internal static class EmptyArray -{ - public static readonly T[] Value = new T[0]; -} diff --git a/src/coreclr/src/mscorlib/src/System/Collections/ArrayList.cs b/src/coreclr/src/mscorlib/src/System/Collections/ArrayList.cs index c95f9b9..cee7be7 100644 --- a/src/coreclr/src/mscorlib/src/System/Collections/ArrayList.cs +++ b/src/coreclr/src/mscorlib/src/System/Collections/ArrayList.cs @@ -46,7 +46,7 @@ namespace System.Collections private Object _syncRoot; private const int _defaultCapacity = 4; - private static readonly Object[] emptyArray = EmptyArray.Value; + private static readonly Object[] emptyArray = Array.Empty(); // Constructs a ArrayList. The list is initially empty and has a capacity // of zero. Upon adding the first element to the list the capacity is diff --git a/src/coreclr/src/mscorlib/src/System/Collections/EmptyReadOnlyDictionaryInternal.cs b/src/coreclr/src/mscorlib/src/System/Collections/EmptyReadOnlyDictionaryInternal.cs index c36f57c..63e0d47 100644 --- a/src/coreclr/src/mscorlib/src/System/Collections/EmptyReadOnlyDictionaryInternal.cs +++ b/src/coreclr/src/mscorlib/src/System/Collections/EmptyReadOnlyDictionaryInternal.cs @@ -114,7 +114,7 @@ namespace System.Collections { get { - return EmptyArray.Value; + return Array.Empty(); } } @@ -122,7 +122,7 @@ namespace System.Collections { get { - return EmptyArray.Value; + return Array.Empty(); } } diff --git a/src/coreclr/src/mscorlib/src/System/Globalization/CompareInfo.Unix.cs b/src/coreclr/src/mscorlib/src/System/Globalization/CompareInfo.Unix.cs index 09b871f..edc9a7f 100644 --- a/src/coreclr/src/mscorlib/src/System/Globalization/CompareInfo.Unix.cs +++ b/src/coreclr/src/mscorlib/src/System/Globalization/CompareInfo.Unix.cs @@ -298,7 +298,7 @@ namespace System.Globalization byte [] keyData; if (source.Length == 0) { - keyData = EmptyArray.Value; + keyData = Array.Empty(); } else { diff --git a/src/coreclr/src/mscorlib/src/System/IO/BinaryReader.cs b/src/coreclr/src/mscorlib/src/System/IO/BinaryReader.cs index cebba6e..54358d6 100644 --- a/src/coreclr/src/mscorlib/src/System/IO/BinaryReader.cs +++ b/src/coreclr/src/mscorlib/src/System/IO/BinaryReader.cs @@ -536,7 +536,7 @@ namespace System.IO if (count == 0) { - return EmptyArray.Value; + return Array.Empty(); } // SafeCritical: we own the chars buffer, and therefore can guarantee that the index and count are valid @@ -580,7 +580,7 @@ namespace System.IO if (count == 0) { - return EmptyArray.Value; + return Array.Empty(); } byte[] result = new byte[count]; diff --git a/src/coreclr/src/mscorlib/src/System/IO/MemoryStream.cs b/src/coreclr/src/mscorlib/src/System/IO/MemoryStream.cs index 80b3af0..3d5668d 100644 --- a/src/coreclr/src/mscorlib/src/System/IO/MemoryStream.cs +++ b/src/coreclr/src/mscorlib/src/System/IO/MemoryStream.cs @@ -68,7 +68,7 @@ namespace System.IO } Contract.EndContractBlock(); - _buffer = capacity != 0 ? new byte[capacity] : EmptyArray.Value; + _buffer = capacity != 0 ? new byte[capacity] : Array.Empty(); _capacity = capacity; _expandable = true; _writable = true; diff --git a/src/coreclr/src/mscorlib/src/System/Reflection/CustomAttribute.cs b/src/coreclr/src/mscorlib/src/System/Reflection/CustomAttribute.cs index 0c8271b..96eb45f 100644 --- a/src/coreclr/src/mscorlib/src/System/Reflection/CustomAttribute.cs +++ b/src/coreclr/src/mscorlib/src/System/Reflection/CustomAttribute.cs @@ -1279,7 +1279,7 @@ namespace System.Reflection Contract.Requires(caType != null); if (type.GetElementType() != null) - return (caType.IsValueType) ? EmptyArray.Value : CreateAttributeArrayHelper(caType, 0); + return (caType.IsValueType) ? Array.Empty() : CreateAttributeArrayHelper(caType, 0); if (type.IsGenericType && !type.IsGenericTypeDefinition) type = type.GetGenericTypeDefinition() as RuntimeType; diff --git a/src/coreclr/src/mscorlib/src/System/Reflection/Emit/DynamicMethod.cs b/src/coreclr/src/mscorlib/src/System/Reflection/Emit/DynamicMethod.cs index ef4d1ca..2d2d309 100644 --- a/src/coreclr/src/mscorlib/src/System/Reflection/Emit/DynamicMethod.cs +++ b/src/coreclr/src/mscorlib/src/System/Reflection/Emit/DynamicMethod.cs @@ -245,7 +245,7 @@ namespace System.Reflection.Emit return s_anonymouslyHostedDynamicMethodsModule; ConstructorInfo transparencyCtor = typeof(SecurityTransparentAttribute).GetConstructor(Type.EmptyTypes); - CustomAttributeBuilder transparencyAttribute = new CustomAttributeBuilder(transparencyCtor, EmptyArray.Value); + CustomAttributeBuilder transparencyAttribute = new CustomAttributeBuilder(transparencyCtor, Array.Empty()); List assemblyAttributes = new List(); assemblyAttributes.Add(transparencyAttribute); @@ -683,7 +683,7 @@ namespace System.Reflection.Emit if (attributeType.IsAssignableFrom(typeof(MethodImplAttribute))) return new Object[] { new MethodImplAttribute(GetMethodImplementationFlags()) }; else - return EmptyArray.Value; + return Array.Empty(); } public override Object[] GetCustomAttributes(bool inherit) @@ -770,12 +770,12 @@ namespace System.Reflection.Emit Object[] ICustomAttributeProvider.GetCustomAttributes(Type attributeType, bool inherit) { - return EmptyArray.Value; + return Array.Empty(); } Object[] ICustomAttributeProvider.GetCustomAttributes(bool inherit) { - return EmptyArray.Value; + return Array.Empty(); } bool ICustomAttributeProvider.IsDefined(Type attributeType, bool inherit) diff --git a/src/coreclr/src/mscorlib/src/System/Reflection/Emit/MethodBuilder.cs b/src/coreclr/src/mscorlib/src/System/Reflection/Emit/MethodBuilder.cs index 7388258..530a5ee 100644 --- a/src/coreclr/src/mscorlib/src/System/Reflection/Emit/MethodBuilder.cs +++ b/src/coreclr/src/mscorlib/src/System/Reflection/Emit/MethodBuilder.cs @@ -335,7 +335,7 @@ namespace System.Reflection.Emit internal override Type[] GetParameterTypes() { if (m_parameterTypes == null) - m_parameterTypes = EmptyArray.Value; + m_parameterTypes = Array.Empty(); return m_parameterTypes; } @@ -375,7 +375,7 @@ namespace System.Reflection.Emit internal SignatureHelper GetMethodSignature() { if (m_parameterTypes == null) - m_parameterTypes = EmptyArray.Value; + m_parameterTypes = Array.Empty(); m_signature = SignatureHelper.GetMethodSigHelper(m_module, m_callingConvention, m_inst != null ? m_inst.Length : 0, m_returnType == null ? typeof(void) : m_returnType, m_returnTypeRequiredCustomModifiers, m_returnTypeOptionalCustomModifiers, diff --git a/src/coreclr/src/mscorlib/src/System/Reflection/Emit/SymbolMethod.cs b/src/coreclr/src/mscorlib/src/System/Reflection/Emit/SymbolMethod.cs index 4c4cd15..42713b8 100644 --- a/src/coreclr/src/mscorlib/src/System/Reflection/Emit/SymbolMethod.cs +++ b/src/coreclr/src/mscorlib/src/System/Reflection/Emit/SymbolMethod.cs @@ -47,7 +47,7 @@ namespace System.Reflection.Emit } else { - m_parameterTypes = EmptyArray.Value; + m_parameterTypes = Array.Empty(); } m_module = mod; diff --git a/src/coreclr/src/mscorlib/src/System/Reflection/Emit/TypeBuilder.cs b/src/coreclr/src/mscorlib/src/System/Reflection/Emit/TypeBuilder.cs index bfd2c96..a98af2b 100644 --- a/src/coreclr/src/mscorlib/src/System/Reflection/Emit/TypeBuilder.cs +++ b/src/coreclr/src/mscorlib/src/System/Reflection/Emit/TypeBuilder.cs @@ -962,7 +962,7 @@ namespace System.Reflection.Emit if (m_typeInterfaces == null) { - return EmptyArray.Value; + return Array.Empty(); } return m_typeInterfaces.ToArray(); diff --git a/src/coreclr/src/mscorlib/src/System/Reflection/MdFieldInfo.cs b/src/coreclr/src/mscorlib/src/System/Reflection/MdFieldInfo.cs index 2ec6abd..41ee4d9 100644 --- a/src/coreclr/src/mscorlib/src/System/Reflection/MdFieldInfo.cs +++ b/src/coreclr/src/mscorlib/src/System/Reflection/MdFieldInfo.cs @@ -128,12 +128,12 @@ namespace System.Reflection public override Type[] GetRequiredCustomModifiers() { - return EmptyArray.Value; + return Array.Empty(); } public override Type[] GetOptionalCustomModifiers() { - return EmptyArray.Value; + return Array.Empty(); } #endregion diff --git a/src/coreclr/src/mscorlib/src/System/Reflection/RuntimeMethodInfo.cs b/src/coreclr/src/mscorlib/src/System/Reflection/RuntimeMethodInfo.cs index 372d4f8..b8a2341 100644 --- a/src/coreclr/src/mscorlib/src/System/Reflection/RuntimeMethodInfo.cs +++ b/src/coreclr/src/mscorlib/src/System/Reflection/RuntimeMethodInfo.cs @@ -723,7 +723,7 @@ namespace System.Reflection if (types == null) { - types = EmptyArray.Value; + types = Array.Empty(); } return types; } diff --git a/src/coreclr/src/mscorlib/src/System/Reflection/RuntimeParameterInfo.cs b/src/coreclr/src/mscorlib/src/System/Reflection/RuntimeParameterInfo.cs index a8d6240..addf68e75 100644 --- a/src/coreclr/src/mscorlib/src/System/Reflection/RuntimeParameterInfo.cs +++ b/src/coreclr/src/mscorlib/src/System/Reflection/RuntimeParameterInfo.cs @@ -480,7 +480,7 @@ namespace System.Reflection public override Object[] GetCustomAttributes(bool inherit) { if (MdToken.IsNullToken(m_tkParamDef)) - return EmptyArray.Value; + return Array.Empty(); return CustomAttribute.GetCustomAttributes(this, typeof(object) as RuntimeType); } @@ -492,7 +492,7 @@ namespace System.Reflection Contract.EndContractBlock(); if (MdToken.IsNullToken(m_tkParamDef)) - return EmptyArray.Value; + return Array.Empty(); RuntimeType attributeRuntimeType = attributeType.UnderlyingSystemType as RuntimeType; diff --git a/src/coreclr/src/mscorlib/src/System/RtType.cs b/src/coreclr/src/mscorlib/src/System/RtType.cs index 370cb0d..ef3ba29 100644 --- a/src/coreclr/src/mscorlib/src/System/RtType.cs +++ b/src/coreclr/src/mscorlib/src/System/RtType.cs @@ -114,7 +114,7 @@ namespace System public T[] ToArray() { if (_count == 0) - return EmptyArray.Value; + return Array.Empty(); if (_count == 1) return new T[1] { _item }; @@ -730,7 +730,7 @@ namespace System { if (ReflectedType.IsGenericParameter) { - return EmptyArray.Value; + return Array.Empty(); } ListBuilder list = new ListBuilder(); @@ -1087,7 +1087,7 @@ namespace System // For example, TypeDescs do not have metadata tokens if (MdToken.IsNullToken(tkEnclosingType)) - return EmptyArray.Value; + return Array.Empty(); ListBuilder list = new ListBuilder(); @@ -3672,7 +3672,7 @@ namespace System Type[] types = GetRootElementType().GetTypeHandleInternal().GetInstantiationPublic(); if (types == null) - types = EmptyArray.Value; + types = Array.Empty(); return types; } @@ -3785,7 +3785,7 @@ namespace System Type[] constraints = new RuntimeTypeHandle(this).GetConstraints(); if (constraints == null) - constraints = EmptyArray.Value; + constraints = Array.Empty(); return constraints; } @@ -3953,7 +3953,7 @@ namespace System } if (members == null) - members = EmptyArray.Value; + members = Array.Empty(); return members; } @@ -4384,7 +4384,7 @@ namespace System finalists = new MethodInfo[] { finalist }; if (providedArgs == null) - providedArgs = EmptyArray.Value; + providedArgs = Array.Empty(); Object state = null; @@ -4608,7 +4608,7 @@ namespace System Object server = null; if (args == null) - args = EmptyArray.Value; + args = Array.Empty(); int argCnt = args.Length; diff --git a/src/coreclr/src/mscorlib/src/System/String.Manipulation.cs b/src/coreclr/src/mscorlib/src/System/String.Manipulation.cs index 4a59ec4..33e341c 100644 --- a/src/coreclr/src/mscorlib/src/System/String.Manipulation.cs +++ b/src/coreclr/src/mscorlib/src/System/String.Manipulation.cs @@ -1234,7 +1234,7 @@ namespace System if ((count == 0) || (omitEmptyEntries && this.Length == 0)) { - return EmptyArray.Value; + return Array.Empty(); } if (count == 1) @@ -1310,7 +1310,7 @@ namespace System if ((count == 0) || (omitEmptyEntries && this.Length == 0)) { - return EmptyArray.Value; + return Array.Empty(); } if (count == 1 || (singleSeparator && separator.Length == 0)) diff --git a/src/coreclr/src/mscorlib/src/System/Text/Encoding.cs b/src/coreclr/src/mscorlib/src/System/Text/Encoding.cs index 9217b88..1591234 100644 --- a/src/coreclr/src/mscorlib/src/System/Text/Encoding.cs +++ b/src/coreclr/src/mscorlib/src/System/Text/Encoding.cs @@ -505,7 +505,7 @@ namespace System.Text [Pure] public virtual byte[] GetPreamble() { - return EmptyArray.Value; + return Array.Empty(); } private void GetDataItem() @@ -1353,13 +1353,13 @@ namespace System.Text internal virtual char[] GetBestFitUnicodeToBytesData() { // Normally we don't have any best fit data. - return EmptyArray.Value; + return Array.Empty(); } internal virtual char[] GetBestFitBytesToUnicodeData() { // Normally we don't have any best fit data. - return EmptyArray.Value; + return Array.Empty(); } internal void ThrowBytesOverflow()