From 68b05d4468c664315fb568b85303c68e9ea1d4f4 Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Sat, 15 Feb 2020 18:54:51 +0100 Subject: [PATCH] Avoid using stackalloc in a loop (#32375) --- src/libraries/System.Private.Uri/src/System/IriHelper.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Private.Uri/src/System/IriHelper.cs b/src/libraries/System.Private.Uri/src/System/IriHelper.cs index bb18ff9485c94..c2d6a0b91703c 100644 --- a/src/libraries/System.Private.Uri/src/System/IriHelper.cs +++ b/src/libraries/System.Private.Uri/src/System/IriHelper.cs @@ -112,6 +112,8 @@ internal static unsafe string EscapeUnescapeIri(char* pInput, int start, int end int next = start; char ch; + Span maxUtf8EncodedSpan = stackalloc byte[4]; + for (; next < end; ++next) { if ((ch = pInput[next]) == '%') @@ -235,8 +237,6 @@ internal static unsafe string EscapeUnescapeIri(char* pInput, int start, int end if (escape) { - Span encodedBytes = stackalloc byte[4]; - Rune rune; if (surrogatePair) { @@ -247,8 +247,8 @@ internal static unsafe string EscapeUnescapeIri(char* pInput, int start, int end rune = Rune.ReplacementChar; } - int bytesWritten = rune.EncodeToUtf8(encodedBytes); - encodedBytes = encodedBytes.Slice(0, bytesWritten); + int bytesWritten = rune.EncodeToUtf8(maxUtf8EncodedSpan); + Span encodedBytes = maxUtf8EncodedSpan.Slice(0, bytesWritten); foreach (byte b in encodedBytes) {