|
| 1 | +using System.Text; |
| 2 | +using BenchmarkDotNet.Attributes; |
| 3 | +using TurboHttp.Utilities; |
| 4 | + |
| 5 | +namespace TurboHttp.Benchmarks; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Benchmarks comparing <see cref="SimdCrlfFinder"/> against the naive scalar scan |
| 9 | +/// that was previously inlined in <c>Http11Decoder</c>. |
| 10 | +/// |
| 11 | +/// Targets: >20% throughput improvement for SIMD over scalar on typical HTTP responses. |
| 12 | +/// </summary> |
| 13 | +[Config(typeof(MicroBenchmarkConfig))] |
| 14 | +public class SimdCrlfFinderBenchmarks |
| 15 | +{ |
| 16 | + // Realistic HTTP/1.1 response header section ending with \r\n\r\n |
| 17 | + private static readonly byte[] ShortResponse = Encoding.ASCII.GetBytes( |
| 18 | + "HTTP/1.1 200 OK\r\nContent-Length: 13\r\nContent-Type: text/plain\r\n\r\n"); |
| 19 | + |
| 20 | + // Longer response with many headers, CRLF near the end |
| 21 | + private static readonly byte[] LongResponse = Encoding.ASCII.GetBytes( |
| 22 | + "HTTP/1.1 200 OK\r\n" + |
| 23 | + "Date: Tue, 01 Jan 2026 00:00:00 GMT\r\n" + |
| 24 | + "Server: TurboHttp/1.0\r\n" + |
| 25 | + "Cache-Control: no-cache, no-store, must-revalidate\r\n" + |
| 26 | + "Pragma: no-cache\r\n" + |
| 27 | + "Expires: 0\r\n" + |
| 28 | + "Content-Type: application/json; charset=utf-8\r\n" + |
| 29 | + "Content-Length: 1024\r\n" + |
| 30 | + "X-Request-Id: 123e4567-e89b-12d3-a456-426614174000\r\n" + |
| 31 | + "X-Trace-Id: 00-abc123def456abc123def456abc12-abcdef123456-01\r\n" + |
| 32 | + "Strict-Transport-Security: max-age=31536000; includeSubDomains\r\n" + |
| 33 | + "\r\n"); |
| 34 | + |
| 35 | + // Single status line — CRLF very close to end (tests tail path) |
| 36 | + private static readonly byte[] SingleLine = Encoding.ASCII.GetBytes("HTTP/1.1 200 OK\r\n"); |
| 37 | + |
| 38 | + // Large buffer: CRLF at position 256 (forces multiple SIMD passes) |
| 39 | + private static readonly byte[] LargeBuffer = BuildLargeBuffer(256); |
| 40 | + |
| 41 | + private static byte[] BuildLargeBuffer(int crlfOffset) |
| 42 | + { |
| 43 | + var buf = new byte[crlfOffset + 4]; |
| 44 | + for (var i = 0; i < crlfOffset; i++) buf[i] = (byte)'A'; |
| 45 | + buf[crlfOffset] = (byte)'\r'; |
| 46 | + buf[crlfOffset + 1] = (byte)'\n'; |
| 47 | + buf[crlfOffset + 2] = (byte)'\r'; |
| 48 | + buf[crlfOffset + 3] = (byte)'\n'; |
| 49 | + return buf; |
| 50 | + } |
| 51 | + |
| 52 | + // ── IndexOfCrlf ───────────────────────────────────────────────────────────── |
| 53 | + |
| 54 | + [Benchmark(Description = "SIMD IndexOfCrlf — short response", OperationsPerInvoke = 1000)] |
| 55 | + public int SimdIndexOfCrlf_Short() |
| 56 | + { |
| 57 | + var result = 0; |
| 58 | + for (var i = 0; i < 1000; i++) |
| 59 | + { |
| 60 | + result = SimdCrlfFinder.IndexOfCrlf(ShortResponse); |
| 61 | + } |
| 62 | + |
| 63 | + return result; |
| 64 | + } |
| 65 | + |
| 66 | + [Benchmark(Description = "Scalar IndexOfCrlf — short response", OperationsPerInvoke = 1000)] |
| 67 | + public int ScalarIndexOfCrlf_Short() |
| 68 | + { |
| 69 | + var result = 0; |
| 70 | + for (var i = 0; i < 1000; i++) |
| 71 | + { |
| 72 | + result = NaiveIndexOfCrlf(ShortResponse); |
| 73 | + } |
| 74 | + |
| 75 | + return result; |
| 76 | + } |
| 77 | + |
| 78 | + [Benchmark(Description = "SIMD IndexOfCrlf — long response", OperationsPerInvoke = 1000)] |
| 79 | + public int SimdIndexOfCrlf_Long() |
| 80 | + { |
| 81 | + var result = 0; |
| 82 | + for (var i = 0; i < 1000; i++) |
| 83 | + { |
| 84 | + result = SimdCrlfFinder.IndexOfCrlf(LongResponse); |
| 85 | + } |
| 86 | + |
| 87 | + return result; |
| 88 | + } |
| 89 | + |
| 90 | + [Benchmark(Description = "Scalar IndexOfCrlf — long response", OperationsPerInvoke = 1000)] |
| 91 | + public int ScalarIndexOfCrlf_Long() |
| 92 | + { |
| 93 | + var result = 0; |
| 94 | + for (var i = 0; i < 1000; i++) |
| 95 | + { |
| 96 | + result = NaiveIndexOfCrlf(LongResponse); |
| 97 | + } |
| 98 | + |
| 99 | + return result; |
| 100 | + } |
| 101 | + |
| 102 | + [Benchmark(Description = "SIMD IndexOfCrlf — large buffer (CRLF at 256)", OperationsPerInvoke = 1000)] |
| 103 | + public int SimdIndexOfCrlf_Large() |
| 104 | + { |
| 105 | + var result = 0; |
| 106 | + for (var i = 0; i < 1000; i++) |
| 107 | + { |
| 108 | + result = SimdCrlfFinder.IndexOfCrlf(LargeBuffer); |
| 109 | + } |
| 110 | + |
| 111 | + return result; |
| 112 | + } |
| 113 | + |
| 114 | + [Benchmark(Description = "Scalar IndexOfCrlf — large buffer (CRLF at 256)", OperationsPerInvoke = 1000)] |
| 115 | + public int ScalarIndexOfCrlf_Large() |
| 116 | + { |
| 117 | + var result = 0; |
| 118 | + for (var i = 0; i < 1000; i++) |
| 119 | + { |
| 120 | + result = NaiveIndexOfCrlf(LargeBuffer); |
| 121 | + } |
| 122 | + |
| 123 | + return result; |
| 124 | + } |
| 125 | + |
| 126 | + // ── IndexOfDoubleCrlf ──────────────────────────────────────────────────────── |
| 127 | + |
| 128 | + [Benchmark(Description = "SIMD IndexOfDoubleCrlf — short response", OperationsPerInvoke = 1000)] |
| 129 | + public int SimdIndexOfDoubleCrlf_Short() |
| 130 | + { |
| 131 | + var result = 0; |
| 132 | + for (var i = 0; i < 1000; i++) |
| 133 | + { |
| 134 | + result = SimdCrlfFinder.IndexOfDoubleCrlf(ShortResponse); |
| 135 | + } |
| 136 | + |
| 137 | + return result; |
| 138 | + } |
| 139 | + |
| 140 | + [Benchmark(Description = "Scalar IndexOfDoubleCrlf — short response", OperationsPerInvoke = 1000)] |
| 141 | + public int ScalarIndexOfDoubleCrlf_Short() |
| 142 | + { |
| 143 | + var result = 0; |
| 144 | + for (var i = 0; i < 1000; i++) |
| 145 | + { |
| 146 | + result = NaiveIndexOfDoubleCrlf(ShortResponse); |
| 147 | + } |
| 148 | + |
| 149 | + return result; |
| 150 | + } |
| 151 | + |
| 152 | + [Benchmark(Description = "SIMD IndexOfDoubleCrlf — long response", OperationsPerInvoke = 1000)] |
| 153 | + public int SimdIndexOfDoubleCrlf_Long() |
| 154 | + { |
| 155 | + var result = 0; |
| 156 | + for (var i = 0; i < 1000; i++) |
| 157 | + { |
| 158 | + result = SimdCrlfFinder.IndexOfDoubleCrlf(LongResponse); |
| 159 | + } |
| 160 | + |
| 161 | + return result; |
| 162 | + } |
| 163 | + |
| 164 | + [Benchmark(Description = "Scalar IndexOfDoubleCrlf — long response", OperationsPerInvoke = 1000)] |
| 165 | + public int ScalarIndexOfDoubleCrlf_Long() |
| 166 | + { |
| 167 | + var result = 0; |
| 168 | + for (var i = 0; i < 1000; i++) |
| 169 | + { |
| 170 | + result = NaiveIndexOfDoubleCrlf(LongResponse); |
| 171 | + } |
| 172 | + |
| 173 | + return result; |
| 174 | + } |
| 175 | + |
| 176 | + [Benchmark(Description = "SIMD IndexOfDoubleCrlf — large buffer (CRLF at 256)", OperationsPerInvoke = 1000)] |
| 177 | + public int SimdIndexOfDoubleCrlf_Large() |
| 178 | + { |
| 179 | + var result = 0; |
| 180 | + for (var i = 0; i < 1000; i++) |
| 181 | + { |
| 182 | + result = SimdCrlfFinder.IndexOfDoubleCrlf(LargeBuffer); |
| 183 | + } |
| 184 | + |
| 185 | + return result; |
| 186 | + } |
| 187 | + |
| 188 | + [Benchmark(Description = "Scalar IndexOfDoubleCrlf — large buffer (CRLF at 256)", OperationsPerInvoke = 1000)] |
| 189 | + public int ScalarIndexOfDoubleCrlf_Large() |
| 190 | + { |
| 191 | + var result = 0; |
| 192 | + for (var i = 0; i < 1000; i++) |
| 193 | + { |
| 194 | + result = NaiveIndexOfDoubleCrlf(LargeBuffer); |
| 195 | + } |
| 196 | + |
| 197 | + return result; |
| 198 | + } |
| 199 | + |
| 200 | + // ── Naive reference implementations ───────────────────────────────────────── |
| 201 | + |
| 202 | + private static int NaiveIndexOfCrlf(ReadOnlySpan<byte> span) |
| 203 | + { |
| 204 | + for (var i = 0; i < span.Length - 1; i++) |
| 205 | + { |
| 206 | + if (span[i] == '\r' && span[i + 1] == '\n') return i; |
| 207 | + } |
| 208 | + |
| 209 | + return -1; |
| 210 | + } |
| 211 | + |
| 212 | + private static int NaiveIndexOfDoubleCrlf(ReadOnlySpan<byte> span) |
| 213 | + { |
| 214 | + for (var i = 0; i <= span.Length - 4; i++) |
| 215 | + { |
| 216 | + if (span[i] == '\r' && span[i + 1] == '\n' && |
| 217 | + span[i + 2] == '\r' && span[i + 3] == '\n') |
| 218 | + { |
| 219 | + return i; |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + return -1; |
| 224 | + } |
| 225 | +} |
0 commit comments