Files
Umbraco-CMS/tests/Umbraco.Tests.Benchmarks/Utf8ToAsciiConverterBenchmarks.cs
yv01p c5a09233aa perf(strings): add final benchmarks and performance comparison for Utf8ToAsciiConverter
- Create Utf8ToAsciiConverterBenchmarks.cs for new SIMD implementation
- Update baseline benchmarks to use OldUtf8ToAsciiConverter
- Document final benchmark results showing 12-157x speedup for ASCII
- Document 1.3-2.2x speedup for mixed content
- Document 60-100% memory reduction across all scenarios
- Create comprehensive comparison document with analysis

Results:
- Pure ASCII: 12-157x faster with zero allocations (fast-path optimization)
- Mixed content: 1.3-2.2x faster with 73% memory reduction
- New Span API: 95% memory reduction for advanced scenarios
- Worst case (Cyrillic): Similar performance, 60% memory reduction

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-13 03:47:31 +00:00

69 lines
2.5 KiB
C#

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Jobs;
using Microsoft.Extensions.Hosting.Internal;
using Microsoft.Extensions.Logging.Abstractions;
using Umbraco.Cms.Core.Strings;
namespace Umbraco.Tests.Benchmarks;
[MemoryDiagnoser]
[RankColumn]
[StatisticalTestColumn]
public class Utf8ToAsciiConverterBenchmarks
{
private static readonly string TinyAscii = BenchmarkTextGenerator.GeneratePureAscii(10);
private static readonly string TinyMixed = BenchmarkTextGenerator.GenerateMixed(10);
private static readonly string SmallAscii = BenchmarkTextGenerator.GeneratePureAscii(100);
private static readonly string SmallMixed = BenchmarkTextGenerator.GenerateMixed(100);
private static readonly string MediumAscii = BenchmarkTextGenerator.GeneratePureAscii(1024);
private static readonly string MediumMixed = BenchmarkTextGenerator.GenerateMixed(1024);
private static readonly string LargeAscii = BenchmarkTextGenerator.GeneratePureAscii(100 * 1024);
private static readonly string LargeMixed = BenchmarkTextGenerator.GenerateMixed(100 * 1024);
private static readonly string LargeWorstCase = BenchmarkTextGenerator.GenerateWorstCase(100 * 1024);
private IUtf8ToAsciiConverter _converter = null!;
[GlobalSetup]
public void Setup()
{
var hostEnv = new HostingEnvironment { ContentRootPath = AppContext.BaseDirectory };
var loader = new CharacterMappingLoader(hostEnv, NullLogger<CharacterMappingLoader>.Instance);
_converter = new Utf8ToAsciiConverter(loader);
}
[Benchmark]
public string Tiny_Ascii() => _converter.Convert(TinyAscii);
[Benchmark]
public string Tiny_Mixed() => _converter.Convert(TinyMixed);
[Benchmark]
public string Small_Ascii() => _converter.Convert(SmallAscii);
[Benchmark]
public string Small_Mixed() => _converter.Convert(SmallMixed);
[Benchmark]
public string Medium_Ascii() => _converter.Convert(MediumAscii);
[Benchmark]
public string Medium_Mixed() => _converter.Convert(MediumMixed);
[Benchmark]
public string Large_Ascii() => _converter.Convert(LargeAscii);
[Benchmark]
public string Large_Mixed() => _converter.Convert(LargeMixed);
[Benchmark]
public string Large_WorstCase() => _converter.Convert(LargeWorstCase);
[Benchmark]
public int Span_Medium_Mixed()
{
Span<char> buffer = stackalloc char[4096];
return _converter.Convert(MediumMixed.AsSpan(), buffer);
}
}