Rune extensions micro-optimizations (#3910)

* 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>
This commit is contained in:
Tonttu
2025-02-25 18:42:32 +02:00
committed by GitHub
parent ff353fc57c
commit e24bd67658
10 changed files with 417 additions and 23 deletions

20
Benchmarks/Program.cs Normal file
View File

@@ -0,0 +1,20 @@
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Running;
namespace Terminal.Gui.Benchmarks;
class Program
{
static void Main (string [] args)
{
var config = DefaultConfig.Instance;
// Uncomment for faster but less accurate intermediate iteration.
// Final benchmarks should be run with at least the default run length.
//config = config.AddJob (BenchmarkDotNet.Jobs.Job.ShortRun);
BenchmarkSwitcher
.FromAssembly (typeof (Program).Assembly)
.Run(args, config);
}
}