Use stackalloc expressions with builder ctors (dotnet/coreclr#27194)
authorStephen Toub <stoub@microsoft.com>
Tue, 15 Oct 2019 12:53:04 +0000 (08:53 -0400)
committerJan Kotas <jkotas@microsoft.com>
Tue, 15 Oct 2019 12:53:04 +0000 (05:53 -0700)
Commit migrated from https://github.com/dotnet/coreclr/commit/76f4c52b9c03487982a8f256f83ab676bbeff476

src/libraries/System.Private.CoreLib/src/System/Environment.Unix.cs
src/libraries/System.Private.CoreLib/src/System/Environment.Win32.cs
src/libraries/System.Private.CoreLib/src/System/Environment.Windows.cs
src/libraries/System.Private.CoreLib/src/System/IO/Path.Windows.cs
src/libraries/System.Private.CoreLib/src/System/IO/Path.cs
src/libraries/System.Private.CoreLib/src/System/IO/PathHelper.Windows.cs
src/libraries/System.Private.CoreLib/src/System/IO/PathInternal.Windows.cs
src/libraries/System.Private.CoreLib/src/System/IO/PathInternal.cs
src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs
src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.Unix.cs

index 208a999..d733feb 100644 (file)
@@ -25,8 +25,7 @@ namespace System
 
         private static string ExpandEnvironmentVariablesCore(string name)
         {
-            Span<char> initialBuffer = stackalloc char[128];
-            var result = new ValueStringBuilder(initialBuffer);
+            var result = new ValueStringBuilder(stackalloc char[128]);
 
             int lastPos = 0, pos;
             while (lastPos < name.Length && (pos = name.IndexOf('%', lastPos + 1)) >= 0)
index 80153c9..8f453c5 100644 (file)
@@ -131,8 +131,7 @@ namespace System
                 // https://support.microsoft.com/en-us/help/909264/naming-conventions-in-active-directory-for-computers-domains-sites-and
                 // https://msdn.microsoft.com/en-us/library/ms679635.aspx
 
-                Span<char> initialBuffer = stackalloc char[40];
-                var builder = new ValueStringBuilder(initialBuffer);
+                var builder = new ValueStringBuilder(stackalloc char[40]);
                 GetUserName(ref builder);
 
                 ReadOnlySpan<char> name = builder.AsSpan();
@@ -178,8 +177,7 @@ namespace System
 #endif
 
                 // See the comment in UserName
-                Span<char> initialBuffer = stackalloc char[40];
-                var builder = new ValueStringBuilder(initialBuffer);
+                var builder = new ValueStringBuilder(stackalloc char[40]);
                 GetUserName(ref builder);
 
                 ReadOnlySpan<char> name = builder.AsSpan();
@@ -196,8 +194,7 @@ namespace System
 
                 // Domain names aren't typically long.
                 // https://support.microsoft.com/en-us/help/909264/naming-conventions-in-active-directory-for-computers-domains-sites-and
-                Span<char> initialDomainNameBuffer = stackalloc char[64];
-                var domainBuilder = new ValueStringBuilder(initialDomainNameBuffer);
+                var domainBuilder = new ValueStringBuilder(stackalloc char[64]);
                 uint length = (uint)domainBuilder.Capacity;
 
                 // This API will fail to return the domain name without a buffer for the SID.
index e7c6c4d..17a4908 100644 (file)
@@ -14,8 +14,7 @@ namespace System
         {
             get
             {
-                Span<char> initialBuffer = stackalloc char[Interop.Kernel32.MAX_PATH];
-                var builder = new ValueStringBuilder(initialBuffer);
+                var builder = new ValueStringBuilder(stackalloc char[Interop.Kernel32.MAX_PATH]);
 
                 uint length;
                 while ((length = Interop.Kernel32.GetCurrentDirectory((uint)builder.Capacity, ref builder.GetPinnableReference())) > builder.Capacity)
@@ -65,8 +64,7 @@ namespace System
 
         private static string ExpandEnvironmentVariablesCore(string name)
         {
-            Span<char> initialBuffer = stackalloc char[128];
-            var builder = new ValueStringBuilder(initialBuffer);
+            var builder = new ValueStringBuilder(stackalloc char[128]);
 
             uint length;
             while ((length = Interop.Kernel32.ExpandEnvironmentStrings(name, ref builder.GetPinnableReference(), (uint)builder.Capacity)) > builder.Capacity)
@@ -108,8 +106,7 @@ namespace System
             get
             {
                 // Normally this will be C:\Windows\System32
-                Span<char> initialBuffer = stackalloc char[32];
-                var builder = new ValueStringBuilder(initialBuffer);
+                var builder = new ValueStringBuilder(stackalloc char[32]);
 
                 uint length;
                 while ((length = Interop.Kernel32.GetSystemDirectoryW(ref builder.GetPinnableReference(), (uint)builder.Capacity)) > builder.Capacity)
index 2987768..1e2bf76 100644 (file)
@@ -136,8 +136,7 @@ namespace System.IO
 
         public static string GetTempPath()
         {
-            Span<char> initialBuffer = stackalloc char[PathInternal.MaxShortPath];
-            var builder = new ValueStringBuilder(initialBuffer);
+            var builder = new ValueStringBuilder(stackalloc char[PathInternal.MaxShortPath]);
 
             GetTempPath(ref builder);
 
@@ -165,13 +164,11 @@ namespace System.IO
         // name on disk.
         public static string GetTempFileName()
         {
-            Span<char> initialTempPathBuffer = stackalloc char[PathInternal.MaxShortPath];
-            ValueStringBuilder tempPathBuilder = new ValueStringBuilder(initialTempPathBuffer);
+            var tempPathBuilder = new ValueStringBuilder(stackalloc char[PathInternal.MaxShortPath]);
 
             GetTempPath(ref tempPathBuilder);
 
-            Span<char> initialBuffer = stackalloc char[PathInternal.MaxShortPath];
-            var builder = new ValueStringBuilder(initialBuffer);
+            var builder = new ValueStringBuilder(stackalloc char[PathInternal.MaxShortPath]);
 
             uint result = Interop.Kernel32.GetTempFileNameW(
                 ref tempPathBuilder.GetPinnableReference(), "tmp", 0, ref builder.GetPinnableReference());
index 45ae627..f41d239 100644 (file)
@@ -384,8 +384,7 @@ namespace System.IO
                     maxSize++;
             }
 
-            Span<char> initialBuffer = stackalloc char[260];    // MaxShortPath on Windows
-            var builder = new ValueStringBuilder(initialBuffer);
+            var builder = new ValueStringBuilder(stackalloc char[260]); // MaxShortPath on Windows
             builder.EnsureCapacity(maxSize);
 
             for (int i = firstComponent; i < paths.Length; i++)
@@ -492,8 +491,7 @@ namespace System.IO
             }
             maxSize += paths.Length - 1;
 
-            Span<char> initialBuffer = stackalloc char[260];    // MaxShortPath on Windows
-            var builder = new ValueStringBuilder(initialBuffer);
+            var builder = new ValueStringBuilder(stackalloc char[260]); // MaxShortPath on Windows
             builder.EnsureCapacity(maxSize);
 
             for (int i = 0; i < paths.Length; i++)
index 5df082f..6a46727 100644 (file)
@@ -26,8 +26,7 @@ namespace System.IO
         /// <returns>Normalized path</returns>
         internal static string Normalize(string path)
         {
-            Span<char> initialBuffer = stackalloc char[PathInternal.MaxShortPath];
-            var builder = new ValueStringBuilder(initialBuffer);
+            var builder = new ValueStringBuilder(stackalloc char[PathInternal.MaxShortPath]);
 
             // Get the full path
             GetFullPathName(path.AsSpan(), ref builder);
@@ -51,8 +50,7 @@ namespace System.IO
         /// </remarks>
         internal static string Normalize(ref ValueStringBuilder path)
         {
-            Span<char> initialBuffer = stackalloc char[PathInternal.MaxShortPath];
-            var builder = new ValueStringBuilder(initialBuffer);
+            var builder = new ValueStringBuilder(stackalloc char[PathInternal.MaxShortPath]);
 
             // Get the full path
             GetFullPathName(path.AsSpan(terminate: true), ref builder);
index 83d5d91..300d126 100644 (file)
@@ -361,8 +361,7 @@ namespace System.IO
             if (normalized)
                 return path;
 
-            Span<char> initialBuffer = stackalloc char[MaxShortPath];
-            ValueStringBuilder builder = new ValueStringBuilder(initialBuffer);
+            var builder = new ValueStringBuilder(stackalloc char[MaxShortPath]);
 
             int start = 0;
             if (IsDirectorySeparator(path[start]))
index 1a32584..11118d4 100644 (file)
@@ -109,8 +109,7 @@ namespace System.IO
         /// <param name="rootLength">The length of the root of the given path</param>
         internal static string RemoveRelativeSegments(string path, int rootLength)
         {
-            Span<char> initialBuffer = stackalloc char[260 /* PathInternal.MaxShortPath */];
-            ValueStringBuilder sb = new ValueStringBuilder(initialBuffer);
+            var sb = new ValueStringBuilder(stackalloc char[260 /* PathInternal.MaxShortPath */]);
 
             if (RemoveRelativeSegments(path.AsSpan(), rootLength, ref sb))
             {
index 4b190ee..48ce4a5 100644 (file)
@@ -1120,8 +1120,7 @@ namespace System
             // Api behavior: if newValue is null, instances of oldValue are to be removed.
             newValue ??= string.Empty;
 
-            Span<int> initialSpan = stackalloc int[StackallocIntBufferSizeLimit];
-            var replacementIndices = new ValueListBuilder<int>(initialSpan);
+            var replacementIndices = new ValueListBuilder<int>(stackalloc int[StackallocIntBufferSizeLimit]);
 
             unsafe
             {
@@ -1272,8 +1271,7 @@ namespace System
                 return new string[] { this };
             }
 
-            Span<int> initialSpan = stackalloc int[StackallocIntBufferSizeLimit];
-            var sepListBuilder = new ValueListBuilder<int>(initialSpan);
+            var sepListBuilder = new ValueListBuilder<int>(stackalloc int[StackallocIntBufferSizeLimit]);
 
             MakeSeparatorList(separators, ref sepListBuilder);
             ReadOnlySpan<int> sepList = sepListBuilder.AsSpan();
@@ -1350,11 +1348,8 @@ namespace System
                 return SplitInternal(separator!, count, options);
             }
 
-            Span<int> sepListInitialSpan = stackalloc int[StackallocIntBufferSizeLimit];
-            var sepListBuilder = new ValueListBuilder<int>(sepListInitialSpan);
-
-            Span<int> lengthListInitialSpan = stackalloc int[StackallocIntBufferSizeLimit];
-            var lengthListBuilder = new ValueListBuilder<int>(lengthListInitialSpan);
+            var sepListBuilder = new ValueListBuilder<int>(stackalloc int[StackallocIntBufferSizeLimit]);
+            var lengthListBuilder = new ValueListBuilder<int>(stackalloc int[StackallocIntBufferSizeLimit]);
 
             MakeSeparatorList(separators!, ref sepListBuilder, ref lengthListBuilder);
             ReadOnlySpan<int> sepList = sepListBuilder.AsSpan();
@@ -1378,8 +1373,7 @@ namespace System
 
         private string[] SplitInternal(string separator, int count, StringSplitOptions options)
         {
-            Span<int> sepListInitialSpan = stackalloc int[StackallocIntBufferSizeLimit];
-            var sepListBuilder = new ValueListBuilder<int>(sepListInitialSpan);
+            var sepListBuilder = new ValueListBuilder<int>(stackalloc int[StackallocIntBufferSizeLimit]);
 
             MakeSeparatorList(separator, ref sepListBuilder);
             ReadOnlySpan<int> sepList = sepListBuilder.AsSpan();
index 8a935bf..6d18ec4 100644 (file)
@@ -410,8 +410,7 @@ namespace System
 
         private static string? GetDirectoryEntryFullPath(ref Interop.Sys.DirectoryEntry dirent, string currentPath)
         {
-            Span<char> nameBuffer = stackalloc char[Interop.Sys.DirectoryEntry.NameBufferSize];
-            ReadOnlySpan<char> direntName = dirent.GetName(nameBuffer);
+            ReadOnlySpan<char> direntName = dirent.GetName(stackalloc char[Interop.Sys.DirectoryEntry.NameBufferSize]);
 
             if ((direntName.Length == 1 && direntName[0] == '.') ||
                 (direntName.Length == 2 && direntName[0] == '.' && direntName[1] == '.'))