Use ValueStringBuilder instead of StringBuilderCache
authorJan Kotas <jkotas@microsoft.com>
Fri, 9 Mar 2018 20:45:12 +0000 (12:45 -0800)
committerJan Kotas <jkotas@microsoft.com>
Sat, 10 Mar 2018 01:05:16 +0000 (17:05 -0800)
Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
src/mscorlib/shared/System/IO/PathInternal.cs

index 789be91..059f0cf 100644 (file)
@@ -119,13 +119,15 @@ namespace System.IO
             Debug.Assert(skip >= 0);
             bool flippedSeparator = false;
 
+            Span<char> initialBuffer = stackalloc char[260 /* PathInternal.MaxShortPath */];
+            ValueStringBuilder sb = new ValueStringBuilder(initialBuffer);
+
             // Remove "//", "/./", and "/../" from the path by copying each character to the output, 
             // except the ones we're removing, such that the builder contains the normalized path 
             // at the end.
-            StringBuilder sb = StringBuilderCache.Acquire(path.Length);
             if (skip > 0)
             {
-                sb.Append(path, 0, skip);
+                sb.Append(path.AsSpan().Slice(0, skip));
             }
 
             for (int i = skip; i < path.Length; i++)
@@ -186,16 +188,14 @@ namespace System.IO
                 sb.Append(c);
             }
 
-            if (flippedSeparator || sb.Length != path.Length)
-            {
-                return StringBuilderCache.GetStringAndRelease(sb);
-            }
-            else
+            // If we haven't changed the source path, return the original
+            if (!flippedSeparator && sb.Length == path.Length)
             {
-                // We haven't changed the source path, return the original
-                StringBuilderCache.Release(sb);
+                sb.Dispose();
                 return path;
             }
+
+            return sb.ToString();
         }
     }
 }