diff --git a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary`2+Builder.cs b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary`2+Builder.cs index 9e04ff1e4f72..1f6f76138a1e 100644 --- a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary`2+Builder.cs +++ b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary`2+Builder.cs @@ -345,9 +345,11 @@ void ICollection.CopyTo(Array array, int arrayIndex) Requires.Range(arrayIndex >= 0, "arrayIndex"); Requires.Range(array.Length >= arrayIndex + this.Count, "arrayIndex"); + int[] indices = new int[1]; // SetValue takes a params array; lifting out the implicit allocation from the loop foreach (var item in this) { - array.SetValue(new DictionaryEntry(item.Key, item.Value), arrayIndex++); + indices[0] = arrayIndex++; + array.SetValue(new DictionaryEntry(item.Key, item.Value), indices); } } diff --git a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary`2.cs b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary`2.cs index 735354477960..80fed706faad 100644 --- a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary`2.cs +++ b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableDictionary`2.cs @@ -780,9 +780,11 @@ void ICollection.CopyTo(Array array, int arrayIndex) Requires.Range(arrayIndex >= 0, "arrayIndex"); Requires.Range(array.Length >= arrayIndex + this.Count, "arrayIndex"); + int[] indices = new int[1]; // SetValue takes a params array; lifting out the implicit allocation from the loop foreach (var item in this) { - array.SetValue(new DictionaryEntry(item.Key, item.Value), arrayIndex++); + indices[0] = arrayIndex++; + array.SetValue(new DictionaryEntry(item.Key, item.Value), indices); } } diff --git a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet`1.cs b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet`1.cs index 7da599a246bc..29aecda55866 100644 --- a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet`1.cs +++ b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet`1.cs @@ -562,9 +562,11 @@ void ICollection.CopyTo(Array array, int arrayIndex) Requires.Range(arrayIndex >= 0, "arrayIndex"); Requires.Range(array.Length >= arrayIndex + this.Count, "arrayIndex"); + int[] indices = new int[1]; // SetValue takes a params array; lifting out the implicit allocation from the loop foreach (T item in this) { - array.SetValue(item, arrayIndex++); + indices[0] = arrayIndex++; + array.SetValue(item, indices); } } diff --git a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList`1.cs b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList`1.cs index 7747d4c2dc9e..7b301825f903 100644 --- a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList`1.cs +++ b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList`1.cs @@ -2590,9 +2590,11 @@ internal void CopyTo(Array array, int arrayIndex) Requires.Range(arrayIndex >= 0, "arrayIndex"); Requires.Range(array.Length >= arrayIndex + this.Count, "arrayIndex"); + int[] indices = new int[1]; // SetValue takes a params array; lifting out the implicit allocation from the loop foreach (var element in this) { - array.SetValue(element, arrayIndex++); + indices[0] = arrayIndex++; + array.SetValue(element, indices); } } diff --git a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary`2.cs b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary`2.cs index 28c8839231d4..d2bf082249de 100644 --- a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary`2.cs +++ b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedDictionary`2.cs @@ -1381,9 +1381,11 @@ internal void CopyTo(Array array, int arrayIndex, int dictionarySize) Requires.Range(arrayIndex >= 0, "arrayIndex"); Requires.Range(array.Length >= arrayIndex + dictionarySize, "arrayIndex"); + int[] indices = new int[1]; // SetValue takes a params array; lifting out the implicit allocation from the loop foreach (var item in this) { - array.SetValue(new DictionaryEntry(item.Key, item.Value), arrayIndex++); + indices[0] = arrayIndex++; + array.SetValue(new DictionaryEntry(item.Key, item.Value), indices); } } diff --git a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet`1.cs b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet`1.cs index 70cd88a2ad7a..e90659152ce2 100644 --- a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet`1.cs +++ b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet`1.cs @@ -1689,9 +1689,11 @@ internal void CopyTo(Array array, int arrayIndex) Requires.Range(arrayIndex >= 0, "arrayIndex"); Requires.Range(array.Length >= arrayIndex + this.Count, "arrayIndex"); + int[] indices = new int[1]; // SetValue takes a params array; lifting out the implicit allocation from the loop foreach (var item in this) { - array.SetValue(item, arrayIndex++); + indices[0] = arrayIndex++; + array.SetValue(item, indices); } } diff --git a/src/System.Collections.Immutable/src/System/Collections/Immutable/KeysOrValuesCollectionAccessor.cs b/src/System.Collections.Immutable/src/System/Collections/Immutable/KeysOrValuesCollectionAccessor.cs index 40440dc84ffc..d1cf8ef2c6f7 100644 --- a/src/System.Collections.Immutable/src/System/Collections/Immutable/KeysOrValuesCollectionAccessor.cs +++ b/src/System.Collections.Immutable/src/System/Collections/Immutable/KeysOrValuesCollectionAccessor.cs @@ -139,9 +139,11 @@ void ICollection.CopyTo(Array array, int arrayIndex) Requires.Range(arrayIndex >= 0, "arrayIndex"); Requires.Range(array.Length >= arrayIndex + this.Count, "arrayIndex"); + int[] indices = new int[1]; // SetValue takes a params array; lifting out the implicit allocation from the loop foreach (T item in this) { - array.SetValue(item, arrayIndex++); + indices[0] = arrayIndex++; + array.SetValue(item, indices); } } diff --git a/src/System.Reflection.Metadata/src/System/Reflection/Metadata/BlobReader.cs b/src/System.Reflection.Metadata/src/System/Reflection/Metadata/BlobReader.cs index 224e6e8b48cb..f70a316837eb 100644 --- a/src/System.Reflection.Metadata/src/System/Reflection/Metadata/BlobReader.cs +++ b/src/System.Reflection.Metadata/src/System/Reflection/Metadata/BlobReader.cs @@ -14,6 +14,9 @@ namespace System.Reflection.Metadata [DebuggerDisplay("{GetDebuggerDisplay(),nq}")] public unsafe struct BlobReader { + /// An array containing the '\0' character. + private static readonly char[] _nullCharArray = new char[1] { '\0' }; + internal const int InvalidCompressedInteger = Int32.MaxValue; private readonly MemoryBlock block; @@ -482,7 +485,7 @@ public string ReadSerializedString() { // Removal of trailing '\0' is a departure from the spec, but required // for compatibility with legacy compilers. - return ReadUTF8(length).TrimEnd('\0'); + return ReadUTF8(length).TrimEnd(_nullCharArray); } if (ReadByte() != 0xFF) diff --git a/src/System.Xml.Common/System/Xml/XmlRawWriter.cs b/src/System.Xml.Common/System/Xml/XmlRawWriter.cs index b93070a6c919..250d84d50947 100644 --- a/src/System.Xml.Common/System/Xml/XmlRawWriter.cs +++ b/src/System.Xml.Common/System/Xml/XmlRawWriter.cs @@ -148,7 +148,7 @@ public override void WriteCData(string text) // Forward call to WriteString(string). public override void WriteCharEntity(char ch) { - WriteString(new string(new char[] { ch })); + WriteString(new string(ch, 1)); } // Forward call to WriteString(string). diff --git a/src/System.Xml.XDocument/System/Xml/Linq/XLinq.cs b/src/System.Xml.XDocument/System/Xml/Linq/XLinq.cs index d3294d7513a9..c47dc14a7852 100644 --- a/src/System.Xml.XDocument/System/Xml/Linq/XLinq.cs +++ b/src/System.Xml.XDocument/System/Xml/Linq/XLinq.cs @@ -8779,6 +8779,8 @@ void AddString(string s) internal class XNodeReader : XmlReader, IXmlLineInfo { + private static readonly char[] WhitespaceChars = new char[] { ' ', '\t', '\n', '\r' }; + // The reader position is encoded by the tuple (source, parent). // Lazy text uses (instance, parent element). Attribute value // uses (instance, parent attribute). End element uses (instance, @@ -9200,7 +9202,7 @@ public override XmlSpace XmlSpace XAttribute a = e.Attribute(name); if (a != null) { - switch (a.Value.Trim(new char[] { ' ', '\t', '\n', '\r' })) + switch (a.Value.Trim(WhitespaceChars)) { case "preserve": return XmlSpace.Preserve; diff --git a/src/System.Xml.XPath/System/Xml/Cache/XPathDocumentBuilder.cs b/src/System.Xml.XPath/System/Xml/Cache/XPathDocumentBuilder.cs index 634c07828d65..866529b7f481 100644 --- a/src/System.Xml.XPath/System/Xml/Cache/XPathDocumentBuilder.cs +++ b/src/System.Xml.XPath/System/Xml/Cache/XPathDocumentBuilder.cs @@ -384,8 +384,7 @@ public override void WriteEntityRef(string name) /// public override void WriteCharEntity(char ch) { - char[] chars = { ch }; - WriteString(new string(chars), TextBlockType.Text); + WriteString(new string(ch, 1), TextBlockType.Text); } /// diff --git a/src/System.Xml.XmlDocument/src/System/Xml/Dom/XmlAttributeCollection.cs b/src/System.Xml.XmlDocument/src/System/Xml/Dom/XmlAttributeCollection.cs index 70e3e4f6c1c2..09c748b6a003 100644 --- a/src/System.Xml.XmlDocument/src/System/Xml/Dom/XmlAttributeCollection.cs +++ b/src/System.Xml.XmlDocument/src/System/Xml/Dom/XmlAttributeCollection.cs @@ -246,8 +246,12 @@ public void RemoveAll() void ICollection.CopyTo(Array array, int index) { + int[] indices = new int[1]; // SetValue takes a params array; lifting out the implicit allocation from the loop for (int i = 0, max = Count; i < max; i++, index++) - array.SetValue(nodes[i], index); + { + indices[0] = index; + array.SetValue(nodes[i], indices); + } } bool ICollection.IsSynchronized