JumpTableSingleEntryBenchmark.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright (c) .NET Foundation. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. using System.Runtime.InteropServices;
  6. using BenchmarkDotNet.Attributes;
  7. namespace Microsoft.AspNetCore.Routing.Matching
  8. {
  9. public class JumpTableSingleEntryBenchmark
  10. {
  11. private JumpTable _default;
  12. private JumpTable _trie;
  13. private JumpTable _vectorTrie;
  14. private JumpTable _ascii;
  15. private string[] _strings;
  16. private PathSegment[] _segments;
  17. [GlobalSetup]
  18. public void Setup()
  19. {
  20. _default = new SingleEntryJumpTable(0, -1, "hello-world", 1);
  21. _trie = new ILEmitTrieJumpTable(0, -1, new[] { ("hello-world", 1), }, vectorize: false, _default);
  22. _vectorTrie = new ILEmitTrieJumpTable(0, -1, new[] { ("hello-world", 1), }, vectorize: true, _default);
  23. _ascii = new SingleEntryAsciiJumpTable(0, -1, "hello-world", 1);
  24. _strings = new string[]
  25. {
  26. "index/foo/2",
  27. "index/hello-world1/2",
  28. "index/hello-world/2",
  29. "index//2",
  30. "index/hillo-goodbye/2",
  31. };
  32. _segments = new PathSegment[]
  33. {
  34. new PathSegment(6, 3),
  35. new PathSegment(6, 12),
  36. new PathSegment(6, 11),
  37. new PathSegment(6, 0),
  38. new PathSegment(6, 13),
  39. };
  40. }
  41. [Benchmark(Baseline = true, OperationsPerInvoke = 5)]
  42. public int Baseline()
  43. {
  44. var strings = _strings;
  45. var segments = _segments;
  46. int destination = 0;
  47. for (var i = 0; i < strings.Length; i++)
  48. {
  49. var @string = strings[i];
  50. var segment = segments[i];
  51. if (segment.Length == 0)
  52. {
  53. destination = -1;
  54. }
  55. else if (segment.Length != "hello-world".Length)
  56. {
  57. destination = 1;
  58. }
  59. else
  60. {
  61. destination = string.Compare(
  62. @string,
  63. segment.Start,
  64. "hello-world",
  65. 0,
  66. segment.Length,
  67. StringComparison.OrdinalIgnoreCase);
  68. }
  69. }
  70. return destination;
  71. }
  72. [Benchmark(OperationsPerInvoke = 5)]
  73. public int Default()
  74. {
  75. var strings = _strings;
  76. var segments = _segments;
  77. var destination = 0;
  78. for (var i = 0; i < strings.Length; i++)
  79. {
  80. destination = _default.GetDestination(strings[i], segments[i]);
  81. }
  82. return destination;
  83. }
  84. [Benchmark(OperationsPerInvoke = 5)]
  85. public int Ascii()
  86. {
  87. var strings = _strings;
  88. var segments = _segments;
  89. var destination = 0;
  90. for (var i = 0; i < strings.Length; i++)
  91. {
  92. destination = _ascii.GetDestination(strings[i], segments[i]);
  93. }
  94. return destination;
  95. }
  96. [Benchmark(OperationsPerInvoke = 5)]
  97. public int Trie()
  98. {
  99. var strings = _strings;
  100. var segments = _segments;
  101. var destination = 0;
  102. for (var i = 0; i < strings.Length; i++)
  103. {
  104. destination = _trie.GetDestination(strings[i], segments[i]);
  105. }
  106. return destination;
  107. }
  108. [Benchmark(OperationsPerInvoke = 5)]
  109. public int VectorTrie()
  110. {
  111. var strings = _strings;
  112. var segments = _segments;
  113. var destination = 0;
  114. for (var i = 0; i < strings.Length; i++)
  115. {
  116. destination = _vectorTrie.GetDestination(strings[i], segments[i]);
  117. }
  118. return destination;
  119. }
  120. }
  121. }