diff --git a/src/coreclr/tools/Common/Compiler/ObjectWriter/StringTableBuilder.cs b/src/coreclr/tools/Common/Compiler/ObjectWriter/StringTableBuilder.cs index db5a9a9406e004..d643c98ec06441 100644 --- a/src/coreclr/tools/Common/Compiler/ObjectWriter/StringTableBuilder.cs +++ b/src/coreclr/tools/Common/Compiler/ObjectWriter/StringTableBuilder.cs @@ -7,7 +7,6 @@ using System.Diagnostics; using System.IO; using System.Text; -using System.Linq; using Internal.Text; namespace ILCompiler.ObjectWriter @@ -15,8 +14,8 @@ namespace ILCompiler.ObjectWriter internal class StringTableBuilder { private readonly MemoryStream _stream = new(); - private readonly SortedSet _reservedStrings = new(); - private Dictionary _stringToOffset = new(); + private readonly HashSet _reservedStrings = new(); + private readonly Dictionary _stringToOffset = new(); public void Write(Stream stream) { @@ -43,9 +42,18 @@ public void ReserveString(Utf8String text) private void FlushReservedStrings() { - Utf8String[] reservedStrings = _reservedStrings.ToArray(); + if (_reservedStrings.Count == 0) + { + return; + } + + Utf8String[] reservedStrings = new Utf8String[_reservedStrings.Count]; + _reservedStrings.CopyTo(reservedStrings); + + // Establish a deterministic order before the in-place suffix sort. + Array.Sort(reservedStrings); - // Pre-sort the string based on their matching suffix + // Sort strings so matching suffixes are adjacent. MultiKeySort(reservedStrings, 0); // Add the strings to string table