StripCRLF.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System.Text;
  2. using BenchmarkDotNet.Attributes;
  3. using Tui = Terminal.Gui;
  4. namespace Terminal.Gui.Benchmarks.Text.TextFormatter;
  5. /// <summary>
  6. /// Benchmarks for <see cref="Tui.TextFormatter.StripCRLF"/> performance fine-tuning.
  7. /// </summary>
  8. [MemoryDiagnoser]
  9. [BenchmarkCategory (nameof (Tui.TextFormatter))]
  10. public class StripCRLF
  11. {
  12. /// <summary>
  13. /// Benchmark for previous implementation.
  14. /// </summary>
  15. /// <param name="str"></param>
  16. /// <param name="keepNewLine"></param>
  17. /// <returns></returns>
  18. [Benchmark]
  19. [ArgumentsSource (nameof (DataSource))]
  20. public string Previous (string str, bool keepNewLine)
  21. {
  22. return RuneListToString (str, keepNewLine);
  23. }
  24. /// <summary>
  25. /// Benchmark for current implementation with StringBuilder and char span index of search.
  26. /// </summary>
  27. [Benchmark (Baseline = true)]
  28. [ArgumentsSource (nameof (DataSource))]
  29. public string Current (string str, bool keepNewLine)
  30. {
  31. return Tui.TextFormatter.StripCRLF (str, keepNewLine);
  32. }
  33. /// <summary>
  34. /// Previous implementation with intermediate rune list.
  35. /// </summary>
  36. private static string RuneListToString (string str, bool keepNewLine = false)
  37. {
  38. List<Rune> runes = str.ToRuneList ();
  39. for (var i = 0; i < runes.Count; i++)
  40. {
  41. switch ((char)runes [i].Value)
  42. {
  43. case '\n':
  44. if (!keepNewLine)
  45. {
  46. runes.RemoveAt (i);
  47. }
  48. break;
  49. case '\r':
  50. if (i + 1 < runes.Count && runes [i + 1].Value == '\n')
  51. {
  52. runes.RemoveAt (i);
  53. if (!keepNewLine)
  54. {
  55. runes.RemoveAt (i);
  56. }
  57. i++;
  58. }
  59. else
  60. {
  61. if (!keepNewLine)
  62. {
  63. runes.RemoveAt (i);
  64. }
  65. }
  66. break;
  67. }
  68. }
  69. return StringExtensions.ToString (runes);
  70. }
  71. public IEnumerable<object []> DataSource ()
  72. {
  73. string[] textPermutations = [
  74. // Extreme newline scenario
  75. "E\r\nx\r\nt\r\nr\r\ne\r\nm\r\ne\r\nn\r\ne\r\nw\r\nl\r\ni\r\nn\r\ne\r\ns\r\nc\r\ne\r\nn\r\na\r\nr\r\ni\r\no\r\n",
  76. // Long text with few line endings
  77. """
  78. Ĺόŕéḿ íṕśúḿ d́όĺόŕ śít́ áḿét́, ćόńśéćt́ét́úŕ ád́íṕíśćíńǵ éĺít́. Ṕŕáéśéńt́ q́úíś ĺúćt́úś éĺít́. Íńt́éǵéŕ út́ áŕćú éǵét́ d́όĺόŕ śćéĺéŕíśq́úé ḿát́t́íś áć ét́ d́íáḿ.
  79. Ṕéĺĺéńt́éśq́úé śéd́ d́áṕíb́úś ḿáśśá, v́éĺ t́ŕíśt́íq́úé d́úí. Śéd́ v́ít́áé ńéq́úé éú v́éĺít́ όŕńáŕé áĺíq́úét́. Út́ q́úíś όŕćí t́éḿṕόŕ, t́éḿṕόŕ t́úŕṕíś íd́, t́éḿṕúś ńéq́úé.
  80. Ṕŕáéśéńt́ śáṕíéń t́úŕṕíś, όŕńáŕé v́éĺ ḿáúŕíś át́, v́áŕíúś śúśćíṕít́ áńt́é. Út́ ṕúĺv́íńáŕ t́úŕṕíś ḿáśśá, q́úíś ćúŕśúś áŕćú f́áúćíb́úś íń.
  81. Óŕćí v́áŕíúś ńát́όq́úé ṕéńát́íb́úś ét́ ḿáǵńíś d́íś ṕáŕt́úŕíéńt́ ḿόńt́éś, ńáśćét́úŕ ŕíd́íćúĺúś ḿúś. F́úśćé át́ éx́ b́ĺáńd́ít́, ćόńv́áĺĺíś q́úáḿ ét́, v́úĺṕút́át́é ĺáćúś.
  82. Śúśṕéńd́íśśé śít́ áḿét́ áŕćú út́ áŕćú f́áúćíb́úś v́áŕíúś. V́ív́áḿúś śít́ áḿét́ ḿáx́íḿúś d́íáḿ. Ńáḿ éx́ ĺéό, ṕh́áŕét́ŕá éú ĺόb́όŕt́íś át́, t́ŕíśt́íq́úé út́ f́éĺíś.
  83. """
  84. // Consistent line endings between systems for more consistent performance evaluation.
  85. .ReplaceLineEndings ("\r\n"),
  86. // Long text without line endings
  87. "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sed euismod metus. Phasellus lectus metus, ultricies a commodo quis, facilisis vitae nulla. " +
  88. "Curabitur mollis ex nisl, vitae mattis nisl consequat at. Aliquam dolor lectus, tincidunt ac nunc eu, elementum molestie lectus. Donec lacinia eget dolor a scelerisque. " +
  89. "Aenean elementum molestie rhoncus. Duis id ornare lorem. Nam eget porta sapien. Etiam rhoncus dignissim leo, ac suscipit magna finibus eu. Curabitur hendrerit elit erat, sit amet suscipit felis condimentum ut. " +
  90. "Nullam semper tempor mi, nec semper quam fringilla eu. Aenean sit amet pretium augue, in posuere ante. Aenean convallis porttitor purus, et posuere velit dictum eu."
  91. ];
  92. bool[] newLinePermutations = [true, false];
  93. foreach (string text in textPermutations)
  94. {
  95. foreach (bool keepNewLine in newLinePermutations)
  96. {
  97. yield return [text, keepNewLine];
  98. }
  99. }
  100. }
  101. }