Skip to content

Commit

Permalink
PERFORMANCE: ICU4N.Impl.LocaleIDParser: Converted to ref struct using…
Browse files Browse the repository at this point in the history
… ValueStringBuilder internally to reduce allocations
  • Loading branch information
NightOwl888 committed Nov 16, 2023
1 parent 85d95d9 commit d15cd2b
Show file tree
Hide file tree
Showing 14 changed files with 1,534 additions and 553 deletions.
6 changes: 5 additions & 1 deletion src/ICU4N/Impl/ICULocaleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,11 @@ private T[] GetCultures<T>(UCultureTypes types, Func<string, T> factory)
else
{
List<T> locales = new List<T>(visIDs.Count);
var parser = new LocaleIDParser(string.Empty);
#if FEATURE_SPAN
using var parser = new LocaleIDParser(stackalloc char[16], ReadOnlySpan<char>.Empty);
#else
using var parser = new LocaleIDParser(string.Empty);
#endif
foreach (string id in visIDs)
{
// Filter the culture type before allocating the object
Expand Down
21 changes: 20 additions & 1 deletion src/ICU4N/Impl/Locale/AsciiUtil.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using J2N.Text;
using ICU4N.Support.Text;
using J2N.Text;
using System;
using System.Text;

namespace ICU4N.Impl.Locale
{
public sealed class AsciiUtil
{
private const int CharStackBufferSize = 128;

public static bool CaseIgnoreMatch(string s1, string s2)
{
//if (Utility.SameObjects(s1, s2))
Expand Down Expand Up @@ -75,7 +79,12 @@ public static string ToLower(string s) // ICU4N specific - renamed from ToLowerS
{
return s;
}
#if FEATURE_SPAN
ValueStringBuilder buf = s.Length <= CharStackBufferSize ? new ValueStringBuilder(stackalloc char[s.Length]) : new ValueStringBuilder(s.Length);
buf.Append(s.AsSpan(0, idx - 0)); // ICU4N: Checked 2nd parameter
#else
StringBuilder buf = new StringBuilder(s.Substring(0, idx - 0)); // ICU4N: Checked 2nd parameter
#endif
for (; idx < s.Length; idx++)
{
buf.Append(ToLower(s[idx]));
Expand All @@ -98,7 +107,12 @@ public static string ToUpper(string s) // ICU4N specific - renamed from ToUpperS
{
return s;
}
#if FEATURE_SPAN
ValueStringBuilder buf = s.Length <= CharStackBufferSize ? new ValueStringBuilder(stackalloc char[s.Length]) : new ValueStringBuilder(s.Length);
buf.Append(s.AsSpan(0, idx - 0)); // ICU4N: Checked 2nd parameter
#else
StringBuilder buf = new StringBuilder(s.Substring(0, idx - 0)); // ICU4N: Checked 2nd parameter
#endif
for (; idx < s.Length; idx++)
{
buf.Append(ToUpper(s[idx]));
Expand Down Expand Up @@ -128,7 +142,12 @@ public static string ToTitle(string s) // ICU4N specific - renamed from ToTitleS
{
return s;
}
#if FEATURE_SPAN
ValueStringBuilder buf = s.Length <= CharStackBufferSize ? new ValueStringBuilder(stackalloc char[s.Length]) : new ValueStringBuilder(s.Length);
buf.Append(s.AsSpan(0, idx - 0)); // ICU4N: Checked 2nd parameter
#else
StringBuilder buf = new StringBuilder(s.Substring(0, idx - 0)); // ICU4N: Checked 2nd parameter
#endif
if (idx == 0)
{
buf.Append(ToUpper(s[idx]));
Expand Down
Loading

0 comments on commit d15cd2b

Please sign in to comment.