mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-26 15:57:56 +01:00
* Add benchmarks for potentially optimizable RuneExtensions * Add new RuneExtensions.DecodeSurrogatePair benchmark implementation Avoids intermediate heap array allocations which is especially nice when the rune is not surrogate pair because then array heap allocations are completely avoided. * Enable nullable reference types in RuneExtensions * Make RuneExtensions.MaxUnicodeCodePoint readonly Makes sure no one can accidentally change the value. Ideally would be const value. * Optimize RuneExtensions.DecodeSurrogatePair * Remove duplicate Rune.GetUnicodeCategory call * Add new RuneExtensions.IsSurrogatePair benchmark implementation Avoids intermediate heap allocations by using stack allocated buffer. * Optimize RuneExtensions.IsSurrogatePair * Add RuneExtensions.GetEncodingLength tests * Optimize RuneExtensions.GetEncodingLength * Optimize RuneExtensions.Encode * Print encoding name in benchmark results * Rename variable to better match return description * Add RuneExtensions.EncodeSurrogatePair benchmark --------- Co-authored-by: Tig <tig@users.noreply.github.com>
75 lines
2.2 KiB
C#
75 lines
2.2 KiB
C#
using System.Text;
|
||
using BenchmarkDotNet.Attributes;
|
||
using Tui = Terminal.Gui;
|
||
|
||
namespace Terminal.Gui.Benchmarks.Text.RuneExtensions;
|
||
|
||
/// <summary>
|
||
/// Benchmarks for <see cref="Tui.RuneExtensions.GetEncodingLength"/> performance fine-tuning.
|
||
/// </summary>
|
||
[MemoryDiagnoser]
|
||
[BenchmarkCategory (nameof (Tui.RuneExtensions))]
|
||
public class GetEncodingLength
|
||
{
|
||
/// <summary>
|
||
/// Benchmark for previous implementation.
|
||
/// </summary>
|
||
[Benchmark]
|
||
[ArgumentsSource (nameof (DataSource))]
|
||
public int Previous (Rune rune, PrettyPrintedEncoding encoding)
|
||
{
|
||
return WithEncodingGetBytesArray (rune, encoding);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Benchmark for current implementation.
|
||
/// </summary>
|
||
[Benchmark (Baseline = true)]
|
||
[ArgumentsSource (nameof (DataSource))]
|
||
public int Current (Rune rune, PrettyPrintedEncoding encoding)
|
||
{
|
||
return Tui.RuneExtensions.GetEncodingLength (rune, encoding);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Previous implementation with intermediate byte array, string, and char array allocation.
|
||
/// </summary>
|
||
private static int WithEncodingGetBytesArray (Rune rune, Encoding? encoding = null)
|
||
{
|
||
encoding ??= Encoding.UTF8;
|
||
byte [] bytes = encoding.GetBytes (rune.ToString ().ToCharArray ());
|
||
var offset = 0;
|
||
|
||
if (bytes [^1] == 0)
|
||
{
|
||
offset++;
|
||
}
|
||
|
||
return bytes.Length - offset;
|
||
}
|
||
|
||
public static IEnumerable<object []> DataSource ()
|
||
{
|
||
PrettyPrintedEncoding[] encodings = [ new(Encoding.UTF8), new(Encoding.Unicode), new(Encoding.UTF32) ];
|
||
Rune[] runes = [ new Rune ('a'), "𝔹".EnumerateRunes ().Single () ];
|
||
|
||
foreach (var encoding in encodings)
|
||
{
|
||
foreach (Rune rune in runes)
|
||
{
|
||
yield return [rune, encoding];
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// <see cref="System.Text.Encoding"/> wrapper to display proper encoding name in benchmark results.
|
||
/// </summary>
|
||
public record PrettyPrintedEncoding (Encoding Encoding)
|
||
{
|
||
public static implicit operator Encoding (PrettyPrintedEncoding ppe) => ppe.Encoding;
|
||
|
||
public override string ToString () => Encoding.HeaderName;
|
||
}
|
||
}
|