DecodeSurrogatePair.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System.Text;
  2. using BenchmarkDotNet.Attributes;
  3. using Terminal.Gui.Text;
  4. using Tui = Terminal.Gui.Text;
  5. namespace Terminal.Gui.Benchmarks.Text.RuneExtensions;
  6. /// <summary>
  7. /// Benchmarks for <see cref="DecodeSurrogatePair"/> performance fine-tuning.
  8. /// </summary>
  9. [MemoryDiagnoser]
  10. [BenchmarkCategory (nameof (Tui.RuneExtensions))]
  11. public class DecodeSurrogatePair
  12. {
  13. /// <summary>
  14. /// Benchmark for previous implementation.
  15. /// </summary>
  16. /// <param name="rune"></param>
  17. /// <returns></returns>
  18. [Benchmark]
  19. [ArgumentsSource (nameof (DataSource))]
  20. public char []? Previous (Rune rune)
  21. {
  22. _ = RuneToStringToCharArray (rune, out char []? chars);
  23. return chars;
  24. }
  25. /// <summary>
  26. /// Benchmark for current implementation.
  27. ///
  28. /// Utilizes Rune methods that take Span argument avoiding intermediate heap array allocation when combined with stack allocated intermediate buffer.
  29. /// When rune is not surrogate pair there will be no heap allocation.
  30. ///
  31. /// Final surrogate pair array allocation cannot be avoided due to the current method signature design.
  32. /// Changing the method signature, or providing an alternative method, to take a destination Span would allow further optimizations by allowing caller to reuse buffer for consecutive calls.
  33. /// </summary>
  34. [Benchmark (Baseline = true)]
  35. [ArgumentsSource (nameof (DataSource))]
  36. public char []? Current (Rune rune)
  37. {
  38. _ = Tui.RuneExtensions.DecodeSurrogatePair (rune, out char []? chars);
  39. return chars;
  40. }
  41. /// <summary>
  42. /// Previous implementation with intermediate string allocation.
  43. ///
  44. /// The IsSurrogatePair implementation at the time had hidden extra string allocation so there were intermediate heap allocations even if rune is not surrogate pair.
  45. /// </summary>
  46. private static bool RuneToStringToCharArray (Rune rune, out char []? chars)
  47. {
  48. if (rune.IsSurrogatePair ())
  49. {
  50. chars = rune.ToString ().ToCharArray ();
  51. return true;
  52. }
  53. chars = null;
  54. return false;
  55. }
  56. public static IEnumerable<object> DataSource ()
  57. {
  58. yield return new Rune ('a');
  59. yield return "𝔹".EnumerateRunes ().Single ();
  60. }
  61. }