Skip to content

Avoid allocation in PathString.Add #28855

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 28, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 15 additions & 27 deletions src/Http/Http.Abstractions/src/PathString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Microsoft.AspNetCore.Http
/// <summary>
/// Represents the empty path. This field is read-only.
/// </summary>
public static readonly PathString Empty = new PathString(string.Empty);
public static readonly PathString Empty = new(string.Empty);

/// <summary>
/// Initialize the path string with a given value. This value must be in unescaped format. Use
Expand Down Expand Up @@ -103,11 +103,7 @@ private static string ToEscapedUriComponent(string value, int i)
if (requiresEscaping)
{
// the current segment requires escape
if (buffer == null)
{
buffer = new StringBuilder(value.Length * 3);
}

buffer ??= new StringBuilder(value.Length * 3);
buffer.Append(Uri.EscapeDataString(value.Substring(start, count)));

requiresEscaping = false;
Expand All @@ -131,11 +127,7 @@ private static string ToEscapedUriComponent(string value, int i)
if (!requiresEscaping)
{
// the current segment doesn't require escape
if (buffer == null)
{
buffer = new StringBuilder(value.Length * 3);
}

buffer ??= new StringBuilder(value.Length * 3);
buffer.Append(value, start, count);

requiresEscaping = true;
Expand All @@ -156,10 +148,7 @@ private static string ToEscapedUriComponent(string value, int i)
{
if (count > 0)
{
if (buffer == null)
{
buffer = new StringBuilder(value.Length * 3);
}
buffer ??= new StringBuilder(value.Length * 3);

if (requiresEscaping)
{
Expand Down Expand Up @@ -259,7 +248,7 @@ public bool StartsWithSegments(PathString other, StringComparison comparisonType
{
if (value1.Length == value2.Length || value1[value2.Length] == '/')
{
remaining = new PathString(value1.Substring(value2.Length));
remaining = new PathString(value1[value2.Length..]);
return true;
}
}
Expand Down Expand Up @@ -298,7 +287,7 @@ public bool StartsWithSegments(PathString other, StringComparison comparisonType
if (value1.Length == value2.Length || value1[value2.Length] == '/')
{
matched = new PathString(value1.Substring(0, value2.Length));
remaining = new PathString(value1.Substring(value2.Length));
remaining = new PathString(value1[value2.Length..]);
return true;
}
}
Expand All @@ -315,11 +304,12 @@ public PathString Add(PathString other)
{
if (HasValue &&
other.HasValue &&
Value![Value.Length - 1] == '/')
Value[^1] == '/')
{
// If the path string has a trailing slash and the other string has a leading slash, we need
// to trim one of them.
return new PathString(Value + other.Value!.Substring(1));
var combined = string.Concat(Value.AsSpan(), other.Value.AsSpan(1));
return new PathString(combined);
}

return new PathString(Value + other.Value);
Expand Down Expand Up @@ -366,11 +356,11 @@ public bool Equals(PathString other, StringComparison comparisonType)
/// <returns>True if both PathString values are equal</returns>
public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj))
if (obj is null)
{
return !HasValue;
}
return obj is PathString && Equals((PathString)obj);
return obj is PathString pathString && Equals(pathString);
}

/// <summary>
Expand Down Expand Up @@ -471,18 +461,16 @@ internal static PathString ConvertFromString(string? s)
internal sealed class PathStringConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
=> sourceType == typeof(string)
? true
: base.CanConvertFrom(context, sourceType);
=> sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);

public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
=> value is string
? PathString.ConvertFromString((string)value)
=> value is string @string
? PathString.ConvertFromString(@string)
: base.ConvertFrom(context, culture, value);

public override object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture, object value, Type destinationType)
=> destinationType == typeof(string)
=> destinationType == typeof(string)
? value.ToString() ?? string.Empty
: base.ConvertTo(context, culture, value, destinationType);
}
Expand Down