Skip to content

Commit 61ebcc7

Browse files
committed
Add SimpleCaseFoldTest
1 parent 8f2521c commit 61ebcc7

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

src/SimpleCaseFolding.cs

+22
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,28 @@ public static char SimpleCaseFold(char c)
4040
return ch == 0 ? c : ch;
4141
}
4242

43+
/// <summary>
44+
/// Simple case fold the char.
45+
/// </summary>
46+
/// <param name="c">Source char.</param>
47+
/// <returns>
48+
/// Returns folded char.
49+
/// </returns>
50+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
51+
public static char SimpleCaseFoldTest(char c)
52+
{
53+
if (c < MapBelow5FF.Length)
54+
{
55+
return MapBelow5FF[c];
56+
}
57+
58+
// Still slow due to border checks.
59+
ushort v = Unsafe.Add(ref s_MapLevel1, c >> 8);
60+
char ch = Unsafe.Add(ref s_refMapData, v + (c & 0xFF));
61+
62+
return ch == 0 ? c : ch;
63+
}
64+
4365
// Mapping for chars > 0x5ff slowly due to 2-level mapping.
4466
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4567
private static int SimpleCaseFoldCompareAbove05ff(char c1, char c2, ref ushort refMapLevel1, ref char refMapData)

tests/Benchmarks/StringFolding/Perf.StringFolding.cs

+39-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
using System.Security.Cryptography;
1111

1212
using BenchmarkDotNet.Attributes;
13+
using BenchmarkDotNet.Configs;
14+
using BenchmarkDotNet.Jobs;
15+
using BenchmarkDotNet.Toolchains.CsProj;
16+
using BenchmarkDotNet.Toolchains.DotNetCli;
1317
using BenchmarkDotNet.Running;
1418

1519
namespace System.Text.CaseFolding
@@ -18,12 +22,46 @@ public class Program
1822
{
1923
public static void Main(string[] args)
2024
{
21-
var summary = BenchmarkRunner.Run<StringFoldingBenchmark>();
25+
var summary = BenchmarkRunner.Run<StringFoldingBenchmark_SimpleCaseFold>();
2226
Console.WriteLine("Result: {0}", SimpleCaseFolding.SimpleCaseFold("cASEfOLDING2"));
2327
Console.WriteLine("Result: {0}", SimpleCaseFolding.SimpleCaseFold("яЯяЯяЯяЯяЯя2"));
2428
}
2529
}
2630

31+
[DisassemblyDiagnoser(printAsm: true, printSource: true, recursiveDepth: 2)]
32+
[Config(typeof(ConfigWithCustomEnvVars))]
33+
public class StringFoldingBenchmark_SimpleCaseFold
34+
{
35+
private class ConfigWithCustomEnvVars : ManualConfig
36+
{
37+
private const string JitNoInline = "COMPlus_TieredCompilation";
38+
39+
public ConfigWithCustomEnvVars()
40+
{
41+
Add(Job.Core
42+
.With(new[] { new EnvironmentVariable(JitNoInline, "1") })
43+
.WithTargetCount(20)
44+
.WithUnrollFactor(20)
45+
.With(CsProjCoreToolchain.From(NetCoreAppSettings.NetCoreApp30)));
46+
}
47+
}
48+
49+
[Params('A', '\u0600')]
50+
public char TestChar { get; set; }
51+
52+
[Benchmark(Baseline = true)]
53+
public char SimpleCaseFold()
54+
{
55+
return SimpleCaseFolding.SimpleCaseFold(TestChar);
56+
}
57+
58+
[Benchmark]
59+
public char SimpleCaseFoldTest()
60+
{
61+
return SimpleCaseFolding.SimpleCaseFoldTest(TestChar);
62+
}
63+
}
64+
2765
[DisassemblyDiagnoser(printAsm: true, printSource: true, recursiveDepth: 3)]
2866
public class StringFoldingBenchmark
2967
{

0 commit comments

Comments
 (0)