Skip to content

Commit 525c43e

Browse files
authored
Remove string.Insert/Remove usage from Uri.GetCanonicalPath (#47924)
For a rare path, GetCanonicalPath is removing one character from a string and then inserting a replacement character. Thanks to span, we can replace the two string allocations with a single char[] allocation and avoid these Remove/Insert calls, which are the only ones keeping string.Remove and string.Insert from being trimmed out of a default Blazor wasm app.
1 parent 64090bb commit 525c43e

File tree

1 file changed

+5
-4
lines changed
  • src/libraries/System.Private.Uri/src/System

1 file changed

+5
-4
lines changed

src/libraries/System.Private.Uri/src/System/Uri.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -4421,17 +4421,18 @@ private unsafe void GetCanonicalPath(ref ValueStringBuilder dest, UriFormat form
44214421
//Note: we may produce non escaped Uri characters on the wire
44224422
if (InFact(Flags.E_PathNotCanonical) && NotAny(Flags.UserEscaped))
44234423
{
4424-
string str = _string;
4424+
ReadOnlySpan<char> str = _string;
44254425

44264426
// Check on not canonical disk designation like C|\, should be rare, rare case
44274427
if (dosPathIdx != 0 && str[dosPathIdx + _info.Offset.Path - 1] == '|')
44284428
{
4429-
str = str.Remove(dosPathIdx + _info.Offset.Path - 1, 1);
4430-
str = str.Insert(dosPathIdx + _info.Offset.Path - 1, ":");
4429+
char[] chars = str.ToArray();
4430+
chars[dosPathIdx + _info.Offset.Path - 1] = ':';
4431+
str = chars;
44314432
}
44324433

44334434
UriHelper.EscapeString(
4434-
str.AsSpan(_info.Offset.Path, _info.Offset.Query - _info.Offset.Path),
4435+
str.Slice(_info.Offset.Path, _info.Offset.Query - _info.Offset.Path),
44354436
ref dest, checkExistingEscaped: !IsImplicitFile, '?', '#');
44364437
}
44374438
else

0 commit comments

Comments
 (0)