|
1 | 1 | // Copyright © https://myCSharp.de - all rights reserved |
2 | 2 |
|
| 3 | +using System.Diagnostics; |
3 | 4 | using System.Diagnostics.CodeAnalysis; |
4 | 5 | using System.Runtime.CompilerServices; |
| 6 | +using System.Runtime.InteropServices; |
| 7 | +using System.Runtime.Intrinsics; |
5 | 8 |
|
6 | 9 | namespace MyCSharp.HttpUserAgentParser; |
7 | 10 |
|
@@ -206,45 +209,133 @@ private static bool TryExtractVersion(ReadOnlySpan<char> haystack, out Range ran |
206 | 209 | { |
207 | 210 | range = default; |
208 | 211 |
|
209 | | - // Limit search window to avoid scanning entire UA string unnecessarily |
210 | | - const int Window = 128; |
211 | | - if (haystack.Length > Window) |
212 | | - { |
213 | | - haystack = haystack.Slice(0, Window); |
214 | | - } |
| 212 | + // Vectorization is used in a optimistic way and specialized to common (trimmed down) user agents. |
| 213 | + // When the first two char-vectors don't yield any success, we fall back to the scalar path. |
| 214 | + // This penalized not found versions, but has an advantage for found versions. |
| 215 | + // Vector512 is left out, because there are no common inputs with length 128 or more. |
| 216 | + // |
| 217 | + // Two short (same size as char) vectors are read, then packed to byte vectors on which the |
| 218 | + // operation is done. For short / chart the higher byte is not of interest and zero or outside |
| 219 | + // the target characters, thus with bytes we can process twice as much elements at once. |
215 | 220 |
|
216 | | - // Find first digit |
217 | | - int start = -1; |
218 | | - for (int i = 0; i < haystack.Length; i++) |
| 221 | + if (Vector256.IsHardwareAccelerated && haystack.Length >= 2 * Vector256<short>.Count) |
219 | 222 | { |
220 | | - char c = haystack[i]; |
221 | | - if (c >= '0' && c <= '9') |
| 223 | + ref char ptr = ref MemoryMarshal.GetReference(haystack); |
| 224 | + |
| 225 | + Vector256<byte> vec = ptr.ReadVector256AsBytes(0); |
| 226 | + Vector256<byte> between0and9 = Vector256.LessThan(vec - Vector256.Create((byte)'0'), Vector256.Create((byte)('9' - '0' + 1))); |
| 227 | + |
| 228 | + if (between0and9 == Vector256<byte>.Zero) |
222 | 229 | { |
223 | | - start = i; |
224 | | - break; |
| 230 | + goto Scalar; |
225 | 231 | } |
226 | | - } |
227 | 232 |
|
228 | | - if (start < 0) |
| 233 | + uint bitMask = between0and9.ExtractMostSignificantBits(); |
| 234 | + int idx = (int)uint.TrailingZeroCount(bitMask); |
| 235 | + Debug.Assert(idx is >= 0 and <= 32); |
| 236 | + int start = idx; |
| 237 | + |
| 238 | + Vector256<byte> byteMask = between0and9 | Vector256.Equals(vec, Vector256.Create((byte)'.')); |
| 239 | + byteMask = ~byteMask; |
| 240 | + |
| 241 | + if (byteMask == Vector256<byte>.Zero) |
| 242 | + { |
| 243 | + goto Scalar; |
| 244 | + } |
| 245 | + |
| 246 | + bitMask = byteMask.ExtractMostSignificantBits(); |
| 247 | + bitMask >>= start; |
| 248 | + |
| 249 | + idx = start + (int)uint.TrailingZeroCount(bitMask); |
| 250 | + Debug.Assert(idx is >= 0 and <= 32); |
| 251 | + int end = idx; |
| 252 | + |
| 253 | + range = new Range(start, end); |
| 254 | + return true; |
| 255 | + } |
| 256 | + else if (Vector128.IsHardwareAccelerated && haystack.Length >= 2 * Vector128<short>.Count) |
229 | 257 | { |
230 | | - // No digit found => no version |
231 | | - return false; |
| 258 | + ref char ptr = ref MemoryMarshal.GetReference(haystack); |
| 259 | + |
| 260 | + Vector128<byte> vec = ptr.ReadVector128AsBytes(0); |
| 261 | + Vector128<byte> between0and9 = Vector128.LessThan(vec - Vector128.Create((byte)'0'), Vector128.Create((byte)('9' - '0' + 1))); |
| 262 | + |
| 263 | + if (between0and9 == Vector128<byte>.Zero) |
| 264 | + { |
| 265 | + goto Scalar; |
| 266 | + } |
| 267 | + |
| 268 | + uint bitMask = between0and9.ExtractMostSignificantBits(); |
| 269 | + int idx = (int)uint.TrailingZeroCount(bitMask); |
| 270 | + Debug.Assert(idx is >= 0 and <= 16); |
| 271 | + int start = idx; |
| 272 | + |
| 273 | + Vector128<byte> byteMask = between0and9 | Vector128.Equals(vec, Vector128.Create((byte)'.')); |
| 274 | + byteMask = ~byteMask; |
| 275 | + |
| 276 | + if (byteMask == Vector128<byte>.Zero) |
| 277 | + { |
| 278 | + goto Scalar; |
| 279 | + } |
| 280 | + |
| 281 | + bitMask = byteMask.ExtractMostSignificantBits(); |
| 282 | + bitMask >>= start; |
| 283 | + |
| 284 | + idx = start + (int)uint.TrailingZeroCount(bitMask); |
| 285 | + Debug.Assert(idx is >= 0 and <= 16); |
| 286 | + int end = idx; |
| 287 | + |
| 288 | + range = new Range(start, end); |
| 289 | + return true; |
232 | 290 | } |
233 | 291 |
|
234 | | - // Consume digits and dots after first digit |
235 | | - int end = start + 1; |
236 | | - while (end < haystack.Length) |
| 292 | + Scalar: |
237 | 293 | { |
238 | | - char c = haystack[end]; |
239 | | - if (!((c >= '0' && c <= '9') || c == '.')) |
| 294 | + // Limit search window to avoid scanning entire UA string unnecessarily |
| 295 | + const int Windows = 128; |
| 296 | + if (haystack.Length > Windows) |
240 | 297 | { |
241 | | - break; |
| 298 | + haystack = haystack.Slice(0, Windows); |
| 299 | + } |
| 300 | + |
| 301 | + int start = -1; |
| 302 | + int i = 0; |
| 303 | + |
| 304 | + for (; i < haystack.Length; ++i) |
| 305 | + { |
| 306 | + char c = haystack[i]; |
| 307 | + if (char.IsBetween(c, '0', '9')) |
| 308 | + { |
| 309 | + start = i; |
| 310 | + break; |
| 311 | + } |
| 312 | + } |
| 313 | + |
| 314 | + if (start < 0) |
| 315 | + { |
| 316 | + // No digit found => no version |
| 317 | + return false; |
| 318 | + } |
| 319 | + |
| 320 | + haystack = haystack.Slice(i + 1); |
| 321 | + for (i = 0; i < haystack.Length; ++i) |
| 322 | + { |
| 323 | + char c = haystack[i]; |
| 324 | + if (!(char.IsBetween(c, '0', '9') || c == '.')) |
| 325 | + { |
| 326 | + break; |
| 327 | + } |
242 | 328 | } |
243 | | - end++; |
244 | | - } |
245 | 329 |
|
246 | | - // Create exclusive end range |
247 | | - range = new Range(start, end); |
248 | | - return true; |
| 330 | + i += start + 1; // shift back the previous domain |
| 331 | + |
| 332 | + if (i == start) |
| 333 | + { |
| 334 | + return false; |
| 335 | + } |
| 336 | + |
| 337 | + range = new Range(start, i); |
| 338 | + return true; |
| 339 | + } |
249 | 340 | } |
250 | 341 | } |
0 commit comments