Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Use ValueStringBuilder instead of StringBuidler{Cache} in multiple pl…
Browse files Browse the repository at this point in the history
…aces
  • Loading branch information
stephentoub committed Oct 26, 2019
1 parent 1cb29bd commit 22818e4
Show file tree
Hide file tree
Showing 12 changed files with 440 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,7 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Text\UTF7Encoding.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Text\UTF8Encoding.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Text\ValueStringBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Text\ValueStringBuilder.AppendFormat.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Text\Unicode\Utf16Utility.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Text\Unicode\Utf16Utility.Validation.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Text\Unicode\Utf8.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,25 @@ public static KeyValuePair<TKey, TValue> Create<TKey, TValue>(TKey key, TValue v
/// </summary>
internal static string PairToString(object? key, object? value)
{
StringBuilder s = StringBuilderCache.Acquire();
var s = new ValueStringBuilder(stackalloc char[64]);

s.Append('[');

if (key != null)
{
s.Append(key);
s.Append(key.ToString());
}

s.Append(", ");

if (value != null)
{
s.Append(value);
s.Append(value.ToString());
}

s.Append(']');

return StringBuilderCache.GetStringAndRelease(s);
return s.ToString();
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/System.Private.CoreLib/shared/System/IO/Path.cs
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,8 @@ private static string GetRelativePath(string relativeTo, string path, StringComp
// C:\Foo\Bar C:\Bar\Bar L3, S2 -> ..\..\Bar\Bar
// C:\Foo\Foo C:\Foo\Bar L7, S1 -> ..\Bar

StringBuilder sb = StringBuilderCache.Acquire(Math.Max(relativeTo.Length, path.Length));
var sb = new ValueStringBuilder(stackalloc char[260]);
sb.EnsureCapacity(Math.Max(relativeTo.Length, path.Length));

// Add parent segments for segments past the common on the "from" path
if (commonLength < relativeToLength)
Expand Down Expand Up @@ -894,10 +895,10 @@ private static string GetRelativePath(string relativeTo, string path, StringComp
sb.Append(DirectorySeparatorChar);
}

sb.Append(path, commonLength, differenceLength);
sb.Append(path.AsSpan(commonLength, differenceLength));
}

return StringBuilderCache.GetStringAndRelease(sb);
return sb.ToString();
}

/// <summary>Returns a comparison that can be used to compare file and directory names for equality.</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,20 +173,20 @@ public override string ToString()
ThrowHelper.ThrowForUnsupportedVectorBaseType<T>();

int lastElement = Count - 1;
StringBuilder sb = StringBuilderCache.Acquire();
var sb = new ValueStringBuilder(stackalloc char[64]);
CultureInfo invariant = CultureInfo.InvariantCulture;

sb.Append('<');
for (int i = 0; i < lastElement; i++)
{
sb.Append(((IFormattable)this.GetElement(i)).ToString("G", invariant))
.Append(',')
.Append(' ');
sb.Append(((IFormattable)this.GetElement(i)).ToString("G", invariant));
sb.Append(',');
sb.Append(' ');
}
sb.Append(((IFormattable)this.GetElement(lastElement)).ToString("G", invariant))
.Append('>');
sb.Append(((IFormattable)this.GetElement(lastElement)).ToString("G", invariant));
sb.Append('>');

return StringBuilderCache.GetStringAndRelease(sb);
return sb.ToString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,20 +174,20 @@ public override string ToString()
ThrowHelper.ThrowForUnsupportedVectorBaseType<T>();

int lastElement = Count - 1;
StringBuilder sb = StringBuilderCache.Acquire();
var sb = new ValueStringBuilder(stackalloc char[64]);
CultureInfo invariant = CultureInfo.InvariantCulture;

sb.Append('<');
for (int i = 0; i < lastElement; i++)
{
sb.Append(((IFormattable)this.GetElement(i)).ToString("G", invariant))
.Append(',')
.Append(' ');
sb.Append(((IFormattable)this.GetElement(i)).ToString("G", invariant));
sb.Append(',');
sb.Append(' ');
}
sb.Append(((IFormattable)this.GetElement(lastElement)).ToString("G", invariant))
.Append('>');
sb.Append(((IFormattable)this.GetElement(lastElement)).ToString("G", invariant));
sb.Append('>');

return StringBuilderCache.GetStringAndRelease(sb);
return sb.ToString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,20 +127,20 @@ public override string ToString()
ThrowHelper.ThrowForUnsupportedVectorBaseType<T>();

int lastElement = Count - 1;
StringBuilder sb = StringBuilderCache.Acquire();
var sb = new ValueStringBuilder(stackalloc char[64]);
CultureInfo invariant = CultureInfo.InvariantCulture;

sb.Append('<');
for (int i = 0; i < lastElement; i++)
{
sb.Append(((IFormattable)this.GetElement(i)).ToString("G", invariant))
.Append(',')
.Append(' ');
sb.Append(((IFormattable)this.GetElement(i)).ToString("G", invariant));
sb.Append(',');
sb.Append(' ');
}
sb.Append(((IFormattable)this.GetElement(lastElement)).ToString("G", invariant))
.Append('>');
sb.Append(((IFormattable)this.GetElement(lastElement)).ToString("G", invariant));
sb.Append('>');

return StringBuilderCache.GetStringAndRelease(sb);
return sb.ToString();
}
}
}
46 changes: 25 additions & 21 deletions src/System.Private.CoreLib/shared/System/String.Manipulation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,15 @@ public static string Concat<T>(IEnumerable<T> values)

// Create the StringBuilder, add the chars we've already enumerated,
// add the rest, and then get the resulting string.
StringBuilder result = StringBuilderCache.Acquire();
var result = new ValueStringBuilder(stackalloc char[256]);
result.Append(c); // first value
do
{
c = en.Current;
result.Append(c);
}
while (en.MoveNext());
return StringBuilderCache.GetStringAndRelease(result);
return result.ToString();
}
}
else
Expand Down Expand Up @@ -195,7 +195,7 @@ public static string Concat<T>(IEnumerable<T> values)
return firstString ?? string.Empty;
}

StringBuilder result = StringBuilderCache.Acquire();
var result = new ValueStringBuilder(stackalloc char[256]);

result.Append(firstString);

Expand All @@ -210,7 +210,7 @@ public static string Concat<T>(IEnumerable<T> values)
}
while (en.MoveNext());

return StringBuilderCache.GetStringAndRelease(result);
return result.ToString();
}
}
}
Expand All @@ -232,7 +232,8 @@ public static string Concat(IEnumerable<string?> values)
return firstValue ?? string.Empty;
}

StringBuilder result = StringBuilderCache.Acquire();
var result = new ValueStringBuilder(stackalloc char[256]);

result.Append(firstValue);

do
Expand All @@ -241,7 +242,7 @@ public static string Concat(IEnumerable<string?> values)
}
while (en.MoveNext());

return StringBuilderCache.GetStringAndRelease(result);
return result.ToString();
}
}

Expand Down Expand Up @@ -522,10 +523,10 @@ private static string FormatHelper(IFormatProvider? provider, string format, Par
if (format == null)
throw new ArgumentNullException(nameof(format));

return StringBuilderCache.GetStringAndRelease(
StringBuilderCache
.Acquire(format.Length + args.Length * 8)
.AppendFormatHelper(provider, format, args));
var sb = new ValueStringBuilder(stackalloc char[256]);
sb.EnsureCapacity(format.Length + args.Length * 8);
sb.AppendFormatHelper(provider, format, args);
return sb.ToString();
}

public string Insert(int startIndex, string value)
Expand Down Expand Up @@ -646,7 +647,8 @@ public static string Join(string? separator, IEnumerable<string?> values)
}

// Null separator and values are handled by the StringBuilder
StringBuilder result = StringBuilderCache.Acquire();
var result = new ValueStringBuilder(stackalloc char[256]);

result.Append(firstValue);

do
Expand All @@ -656,7 +658,7 @@ public static string Join(string? separator, IEnumerable<string?> values)
}
while (en.MoveNext());

return StringBuilderCache.GetStringAndRelease(result);
return result.ToString();
}
}

Expand Down Expand Up @@ -691,7 +693,8 @@ private static unsafe string JoinCore(char* separator, int separatorLength, obje
return firstString ?? string.Empty;
}

StringBuilder result = StringBuilderCache.Acquire();
var result = new ValueStringBuilder(stackalloc char[256]);

result.Append(firstString);

for (int i = 1; i < values.Length; i++)
Expand All @@ -704,7 +707,7 @@ private static unsafe string JoinCore(char* separator, int separatorLength, obje
}
}

return StringBuilderCache.GetStringAndRelease(result);
return result.ToString();
}

private static unsafe string JoinCore<T>(char* separator, int separatorLength, IEnumerable<T> values)
Expand Down Expand Up @@ -739,7 +742,7 @@ private static unsafe string JoinCore<T>(char* separator, int separatorLength, I
return firstString ?? string.Empty;
}

StringBuilder result = StringBuilderCache.Acquire();
var result = new ValueStringBuilder(stackalloc char[256]);

result.Append(firstString);

Expand All @@ -755,7 +758,7 @@ private static unsafe string JoinCore<T>(char* separator, int separatorLength, I
}
while (en.MoveNext());

return StringBuilderCache.GetStringAndRelease(result);
return result.ToString();
}
}

Expand Down Expand Up @@ -1003,7 +1006,8 @@ private unsafe string ReplaceCore(string oldValue, string? newValue, CultureInfo
newValue ??= string.Empty;

CultureInfo referenceCulture = culture ?? CultureInfo.CurrentCulture;
StringBuilder result = StringBuilderCache.Acquire();
var result = new ValueStringBuilder(stackalloc char[256]);
result.EnsureCapacity(this.Length);

int startIndex = 0;
int index = 0;
Expand All @@ -1019,7 +1023,7 @@ private unsafe string ReplaceCore(string oldValue, string? newValue, CultureInfo
if (index >= 0)
{
// append the unmodified portion of string
result.Append(this, startIndex, index - startIndex);
result.Append(this.AsSpan(startIndex, index - startIndex));

// append the replacement
result.Append(newValue);
Expand All @@ -1032,16 +1036,16 @@ private unsafe string ReplaceCore(string oldValue, string? newValue, CultureInfo
// small optimization,
// if we have not done any replacements,
// we will return the original string
StringBuilderCache.Release(result);
result.Dispose();
return this;
}
else
{
result.Append(this, startIndex, this.Length - startIndex);
result.Append(this.AsSpan(startIndex, this.Length - startIndex));
}
} while (index >= 0);

return StringBuilderCache.GetStringAndRelease(result);
return result.ToString();
}

// Replaces all instances of oldChar with newChar.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1579,8 +1579,8 @@ internal StringBuilder AppendFormatHelper(IFormatProvider? provider, string form
FormatError();
}
}
// Is it a opening brace?
if (ch == '{')
// Is it an opening brace?
else if (ch == '{')
{
// Check next character (if there is one) to see if it is escaped. eg {{
if (pos < len && format[pos] == '{')
Expand All @@ -1594,7 +1594,7 @@ internal StringBuilder AppendFormatHelper(IFormatProvider? provider, string form
break;
}
}
// If it neither then treat the character as just text.
// If it's neither then treat the character as just text.
Append(ch);
}

Expand Down
Loading

0 comments on commit 22818e4

Please sign in to comment.