DecodeSurrogatePair.cs 2.2 KB

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