From 9c4eff1e17934b17bc508b0a4c459326b8dde681 Mon Sep 17 00:00:00 2001 From: Steve Dunn Date: Mon, 11 Oct 2021 21:22:41 +0100 Subject: [PATCH 1/3] Implementation --- .../src/System/String.cs | 36 +++---------------- 1 file changed, 4 insertions(+), 32 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/String.cs b/src/libraries/System.Private.CoreLib/src/System/String.cs index 7ca88a4b29c79..e0ccc1b0e9a5e 100644 --- a/src/libraries/System.Private.CoreLib/src/System/String.cs +++ b/src/libraries/System.Private.CoreLib/src/System/String.cs @@ -320,38 +320,10 @@ string Ctor(char c, int count) throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NegativeCount); } - string result = FastAllocateString(count); - - if (c != '\0') // Fast path null char string - { - unsafe - { - fixed (char* dest = &result._firstChar) - { - uint cc = (uint)((c << 16) | c); - uint* dmem = (uint*)dest; - if (count >= 4) - { - count -= 4; - do - { - dmem[0] = cc; - dmem[1] = cc; - dmem += 2; - count -= 4; - } while (count >= 0); - } - if ((count & 2) != 0) - { - *dmem = cc; - dmem++; - } - if ((count & 1) != 0) - ((char*)dmem)[0] = c; - } - } - } - return result; + char[] arr = new char[count]; + Span s = new(arr); + s.Fill(c); + return new(s); } [MethodImpl(MethodImplOptions.InternalCall)] From a05de4b2a4d26b671a07e83a3e73d519926b155b Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Mon, 15 Nov 2021 14:49:14 +0100 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Jeremy Koritzinsky --- .../System.Private.CoreLib/src/System/String.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/String.cs b/src/libraries/System.Private.CoreLib/src/System/String.cs index e0ccc1b0e9a5e..f5c701b2ca6cd 100644 --- a/src/libraries/System.Private.CoreLib/src/System/String.cs +++ b/src/libraries/System.Private.CoreLib/src/System/String.cs @@ -320,10 +320,12 @@ string Ctor(char c, int count) throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NegativeCount); } - char[] arr = new char[count]; - Span s = new(arr); - s.Fill(c); - return new(s); + string result = FastAllocateString(count); + if (c != '\0') + { + MemoryMarshal.CreateSpan(ref result._firstChar, count).Fill(c); + } + return result; } [MethodImpl(MethodImplOptions.InternalCall)] From 8d033b3c4fbf3d3d50fffd03e2e9f694c5e06dd5 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Mon, 15 Nov 2021 14:59:31 +0100 Subject: [PATCH 3/3] Apply suggestions from code review --- src/libraries/System.Private.CoreLib/src/System/String.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/String.cs b/src/libraries/System.Private.CoreLib/src/System/String.cs index f5c701b2ca6cd..76048da7b75c2 100644 --- a/src/libraries/System.Private.CoreLib/src/System/String.cs +++ b/src/libraries/System.Private.CoreLib/src/System/String.cs @@ -323,7 +323,7 @@ string Ctor(char c, int count) string result = FastAllocateString(count); if (c != '\0') { - MemoryMarshal.CreateSpan(ref result._firstChar, count).Fill(c); + SpanHelpers.Fill(ref result._firstChar, (uint)count, c); } return result; }