| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.Numerics; |
| | | 6 | | using System.Runtime.CompilerServices; |
| | | 7 | | using System.Runtime.InteropServices; |
| | | 8 | | using System.Runtime.Intrinsics; |
| | | 9 | | using System.Runtime.Intrinsics.Arm; |
| | | 10 | | using System.Runtime.Intrinsics.Wasm; |
| | | 11 | | using System.Runtime.Intrinsics.X86; |
| | | 12 | | |
| | | 13 | | namespace System.Buffers |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Provides a set of initialization methods for instances of the <see cref="SearchValues{T}"/> class. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <remarks> |
| | | 19 | | /// SearchValues are optimized for situations where the same set of values is frequently used for searching at runti |
| | | 20 | | /// </remarks> |
| | | 21 | | public static class SearchValues |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// Creates an optimized representation of <paramref name="values"/> used for efficient searching. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="values">The set of values.</param> |
| | | 27 | | /// <returns>The optimized representation of <paramref name="values"/> used for efficient searching.</returns> |
| | | 28 | | public static SearchValues<byte> Create(params ReadOnlySpan<byte> values) |
| | | 29 | | { |
| | 8924 | 30 | | if (values.IsEmpty) |
| | | 31 | | { |
| | 72 | 32 | | return new EmptySearchValues<byte>(); |
| | | 33 | | } |
| | | 34 | | |
| | 8852 | 35 | | if (values.Length == 1) |
| | | 36 | | { |
| | 291 | 37 | | return new Any1SearchValues<byte, byte>(values); |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | // RangeByteSearchValues is slower than SingleByteSearchValues, but faster than Any2ByteSearchValues |
| | 8561 | 41 | | if (TryGetSingleRange(values, out byte minInclusive, out byte maxInclusive)) |
| | | 42 | | { |
| | 999 | 43 | | return new RangeByteSearchValues(minInclusive, maxInclusive); |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | // Depending on the hardware, UniqueLowNibble can be faster than even range or 2 values. |
| | | 47 | | // It's currently consistently faster than 4/5 values on all tested platforms (Arm, Avx2, Avx512). |
| | 7562 | 48 | | if (values.Length >= 4 && IndexOfAnyAsciiSearcher.CanUseUniqueLowNibbleSearch(values, maxInclusive)) |
| | | 49 | | { |
| | 289 | 50 | | return new AsciiByteSearchValues<TrueConst>(values); |
| | | 51 | | } |
| | | 52 | | |
| | 7273 | 53 | | if (values.Length <= 5) |
| | | 54 | | { |
| | 2600 | 55 | | Debug.Assert(values.Length is 2 or 3 or 4 or 5); |
| | 2600 | 56 | | return values.Length switch |
| | 2600 | 57 | | { |
| | 348 | 58 | | 2 => new Any2SearchValues<byte, byte>(values), |
| | 513 | 59 | | 3 => new Any3SearchValues<byte, byte>(values), |
| | 683 | 60 | | 4 => new Any4SearchValues<byte, byte>(values), |
| | 1056 | 61 | | _ => new Any5SearchValues<byte, byte>(values), |
| | 2600 | 62 | | }; |
| | | 63 | | } |
| | | 64 | | |
| | 4673 | 65 | | if (IndexOfAnyAsciiSearcher.IsVectorizationSupported && maxInclusive < 128) |
| | | 66 | | { |
| | 1849 | 67 | | return new AsciiByteSearchValues<FalseConst>(values); |
| | | 68 | | } |
| | | 69 | | |
| | 2824 | 70 | | return new AnyByteSearchValues(values); |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Creates an optimized representation of <paramref name="values"/> used for efficient searching. |
| | | 75 | | /// </summary> |
| | | 76 | | /// <param name="values">The set of values.</param> |
| | | 77 | | /// <returns>The optimized representation of <paramref name="values"/> used for efficient searching.</returns> |
| | | 78 | | public static unsafe SearchValues<char> Create(params ReadOnlySpan<char> values) |
| | | 79 | | { |
| | 8925 | 80 | | if (values.IsEmpty) |
| | | 81 | | { |
| | 363 | 82 | | return new EmptySearchValues<char>(); |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | // Vector128<char> isn't valid. Treat the values as shorts instead. |
| | 8562 | 86 | | ReadOnlySpan<short> shortValues = MemoryMarshal.Cast<char, short>(values); |
| | | 87 | | |
| | 8562 | 88 | | if (values.Length == 1) |
| | | 89 | | { |
| | 1153 | 90 | | char value = values[0]; |
| | | 91 | | |
| | 1153 | 92 | | return PackedSpanHelpers.PackedIndexOfIsSupported && PackedSpanHelpers.CanUsePackedIndexOf(value) |
| | 1153 | 93 | | ? new Any1CharPackedSearchValues(value) |
| | 1153 | 94 | | : new Any1SearchValues<char, short>(shortValues); |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | // RangeCharSearchValues is slower than SingleCharSearchValues, but faster than Any2CharSearchValues |
| | 7409 | 98 | | if (TryGetSingleRange(values, out char minInclusive, out char maxInclusive)) |
| | | 99 | | { |
| | 1033 | 100 | | return PackedSpanHelpers.PackedIndexOfIsSupported && PackedSpanHelpers.CanUsePackedIndexOf(minInclusive) |
| | 1033 | 101 | | ? new RangeCharSearchValues<TrueConst>(minInclusive, maxInclusive) |
| | 1033 | 102 | | : new RangeCharSearchValues<FalseConst>(minInclusive, maxInclusive); |
| | | 103 | | } |
| | | 104 | | |
| | 6376 | 105 | | if (values.Length == 2) |
| | | 106 | | { |
| | 1906 | 107 | | char value0 = values[0]; |
| | 1906 | 108 | | char value1 = values[1]; |
| | | 109 | | |
| | 1906 | 110 | | if (PackedSpanHelpers.PackedIndexOfIsSupported && PackedSpanHelpers.CanUsePackedIndexOf(value0) && Packe |
| | | 111 | | { |
| | | 112 | | // If the two values are the same ASCII letter with both cases, we can use an approach that |
| | | 113 | | // reduces the number of comparisons by masking off the bit that differs between lower and upper cas |
| | | 114 | | // While this most commonly applies to ASCII letters, it also works for other values that differ by |
| | 487 | 115 | | return (value0 ^ value1) == 0x20 |
| | 487 | 116 | | ? new Any1CharPackedIgnoreCaseSearchValues((char)Math.Max(value0, value1)) |
| | 487 | 117 | | : new Any2CharPackedSearchValues(value0, value1); |
| | | 118 | | } |
| | | 119 | | |
| | 1419 | 120 | | return new Any2SearchValues<char, short>(shortValues); |
| | | 121 | | } |
| | | 122 | | |
| | 4470 | 123 | | if (values.Length == 3) |
| | | 124 | | { |
| | 820 | 125 | | char value0 = values[0]; |
| | 820 | 126 | | char value1 = values[1]; |
| | 820 | 127 | | char value2 = values[2]; |
| | | 128 | | |
| | 820 | 129 | | return PackedSpanHelpers.PackedIndexOfIsSupported && PackedSpanHelpers.CanUsePackedIndexOf(value0) && Pa |
| | 820 | 130 | | ? new Any3CharPackedSearchValues(value0, value1, value2) |
| | 820 | 131 | | : new Any3SearchValues<char, short>(shortValues); |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | // If the values are sets of 2 ASCII letters with both cases, we can use an approach that |
| | | 135 | | // reduces the number of comparisons by masking off the bit that differs between lower and upper case (0x20) |
| | | 136 | | // While this most commonly applies to ASCII letters, it also works for other values that differ by 0x20 (e. |
| | 3650 | 137 | | if (IndexOfAnyAsciiSearcher.IsVectorizationSupported && PackedSpanHelpers.PackedIndexOfIsSupported && |
| | 3650 | 138 | | maxInclusive < 128 && values.Length == 4 && minInclusive > 0) |
| | | 139 | | { |
| | 435 | 140 | | Span<char> copy = ['\0', '\0', '\0', '\0']; |
| | 435 | 141 | | values.CopyTo(copy); |
| | 435 | 142 | | copy.Sort(); |
| | | 143 | | |
| | 435 | 144 | | if ((copy[0] ^ copy[2]) == 0x20 && |
| | 435 | 145 | | (copy[1] ^ copy[3]) == 0x20) |
| | | 146 | | { |
| | | 147 | | // We pick the higher two values (with the 0x20 bit set). "AaBb" => 'a', 'b' |
| | 360 | 148 | | return new Any2CharPackedIgnoreCaseSearchValues(copy[2], copy[3]); |
| | | 149 | | } |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | // Depending on the hardware, UniqueLowNibble can be faster than most implementations we currently prefer ab |
| | | 153 | | // It's currently consistently faster than 4/5 values or Ascii on all tested platforms (Arm, Avx2, Avx512). |
| | 3290 | 154 | | if (IndexOfAnyAsciiSearcher.CanUseUniqueLowNibbleSearch(values, maxInclusive)) |
| | | 155 | | { |
| | 43 | 156 | | return (Ssse3.IsSupported || PackedSimd.IsSupported) && minInclusive == 0 |
| | 43 | 157 | | ? new AsciiCharSearchValues<IndexOfAnyAsciiSearcher.Ssse3AndWasmHandleZeroInNeedle, TrueConst>(value |
| | 43 | 158 | | : new AsciiCharSearchValues<IndexOfAnyAsciiSearcher.Default, TrueConst>(values); |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | // IndexOfAnyAsciiSearcher for chars is slower than Any3CharSearchValues, but faster than Any4SearchValues |
| | 3247 | 162 | | if (IndexOfAnyAsciiSearcher.IsVectorizationSupported && maxInclusive < 128) |
| | | 163 | | { |
| | 662 | 164 | | return (Ssse3.IsSupported || PackedSimd.IsSupported) && minInclusive == 0 |
| | 662 | 165 | | ? new AsciiCharSearchValues<IndexOfAnyAsciiSearcher.Ssse3AndWasmHandleZeroInNeedle, FalseConst>(valu |
| | 662 | 166 | | : new AsciiCharSearchValues<IndexOfAnyAsciiSearcher.Default, FalseConst>(values); |
| | | 167 | | } |
| | | 168 | | |
| | 2585 | 169 | | if (values.Length == 4) |
| | | 170 | | { |
| | 153 | 171 | | return new Any4SearchValues<char, short>(shortValues); |
| | | 172 | | } |
| | | 173 | | |
| | 2432 | 174 | | if (values.Length == 5) |
| | | 175 | | { |
| | 87 | 176 | | return new Any5SearchValues<char, short>(shortValues); |
| | | 177 | | } |
| | | 178 | | |
| | 2345 | 179 | | if (IndexOfAnyAsciiSearcher.IsVectorizationSupported && minInclusive < 128) |
| | | 180 | | { |
| | | 181 | | // If we have both ASCII and non-ASCII characters, use an implementation that |
| | | 182 | | // does an optimistic ASCII fast-path and then falls back to the ProbabilisticMap. |
| | | 183 | | |
| | 1395 | 184 | | return (Ssse3.IsSupported || PackedSimd.IsSupported) && minInclusive == 0 |
| | 1395 | 185 | | ? new ProbabilisticWithAsciiCharSearchValues<IndexOfAnyAsciiSearcher.Ssse3AndWasmHandleZeroInNeedle> |
| | 1395 | 186 | | : new ProbabilisticWithAsciiCharSearchValues<IndexOfAnyAsciiSearcher.Default>(values, maxInclusive); |
| | | 187 | | } |
| | | 188 | | |
| | 950 | 189 | | if (ShouldUseProbabilisticMap(values.Length, maxInclusive)) |
| | | 190 | | { |
| | 436 | 191 | | return new ProbabilisticCharSearchValues(values, maxInclusive); |
| | | 192 | | } |
| | | 193 | | |
| | | 194 | | // This will also match ASCII values when IndexOfAnyAsciiSearcher is not supported. |
| | 514 | 195 | | return new BitmapCharSearchValues(values, maxInclusive); |
| | | 196 | | |
| | | 197 | | static bool ShouldUseProbabilisticMap(int valuesLength, int maxInclusive) |
| | | 198 | | { |
| | | 199 | | // *Rough estimates*. The current implementation uses 256 bits for the bloom filter. |
| | | 200 | | // If the implementation is vectorized we can get away with a decent false positive rate. |
| | | 201 | | const int MaxValuesForProbabilisticMap = 256; |
| | | 202 | | |
| | 950 | 203 | | if (valuesLength > MaxValuesForProbabilisticMap) |
| | | 204 | | { |
| | | 205 | | // If the number of values is too high, we won't see any benefits from the 'probabilistic' part. |
| | 514 | 206 | | return false; |
| | | 207 | | } |
| | | 208 | | |
| | 436 | 209 | | if (Sse41.IsSupported || AdvSimd.Arm64.IsSupported) |
| | | 210 | | { |
| | | 211 | | // If the probabilistic map is vectorized, we prefer it. |
| | 436 | 212 | | return true; |
| | | 213 | | } |
| | | 214 | | |
| | | 215 | | // The probabilistic map is more memory efficient for spare sets, while the bitmap is more efficient for |
| | 0 | 216 | | int bitmapFootprintBytesEstimate = 64 + (maxInclusive / 8); |
| | 0 | 217 | | int probabilisticFootprintBytesEstimate = 128 + (valuesLength * 4); |
| | | 218 | | |
| | | 219 | | // The bitmap is a bit faster than the perfect hash checks employed by the probabilistic map. |
| | | 220 | | // Sacrifice some memory usage for faster lookups. |
| | | 221 | | const int AcceptableSizeMultiplier = 2; |
| | | 222 | | |
| | 0 | 223 | | return AcceptableSizeMultiplier * probabilisticFootprintBytesEstimate < bitmapFootprintBytesEstimate; |
| | | 224 | | } |
| | | 225 | | } |
| | | 226 | | |
| | | 227 | | /// <summary> |
| | | 228 | | /// Creates an optimized representation of <paramref name="values"/> used for efficient searching. |
| | | 229 | | /// </summary> |
| | | 230 | | /// <param name="values">The set of values.</param> |
| | | 231 | | /// <param name="comparisonType">Specifies whether to use <see cref="StringComparison.Ordinal"/> or <see cref="S |
| | | 232 | | /// <returns>The optimized representation of <paramref name="values"/> used for efficient searching.</returns> |
| | | 233 | | /// <remarks>Only <see cref="StringComparison.Ordinal"/> or <see cref="StringComparison.OrdinalIgnoreCase"/> may |
| | | 234 | | public static SearchValues<string> Create(ReadOnlySpan<string> values, StringComparison comparisonType) |
| | | 235 | | { |
| | 0 | 236 | | if (comparisonType is not (StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase)) |
| | | 237 | | { |
| | 0 | 238 | | throw new ArgumentException(SR.Argument_SearchValues_UnsupportedStringComparison, nameof(comparisonType) |
| | | 239 | | } |
| | | 240 | | |
| | 0 | 241 | | return StringSearchValues.Create(values, ignoreCase: comparisonType == StringComparison.OrdinalIgnoreCase); |
| | | 242 | | } |
| | | 243 | | |
| | | 244 | | private static bool TryGetSingleRange<T>(ReadOnlySpan<T> values, out T minInclusive, out T maxInclusive) |
| | | 245 | | where T : struct, INumber<T> |
| | | 246 | | { |
| | 15970 | 247 | | minInclusive = values.Min(); |
| | 15970 | 248 | | maxInclusive = values.Max(); |
| | | 249 | | |
| | 15970 | 250 | | uint range = uint.CreateChecked(maxInclusive - minInclusive) + 1; |
| | 15970 | 251 | | if (range > values.Length) |
| | | 252 | | { |
| | 12587 | 253 | | return false; |
| | | 254 | | } |
| | | 255 | | |
| | 3383 | 256 | | Span<bool> seenValues = range <= 256 ? stackalloc bool[256] : new bool[range]; |
| | 3383 | 257 | | seenValues = seenValues.Slice(0, (int)range); |
| | 3383 | 258 | | seenValues.Clear(); |
| | | 259 | | |
| | 1082374 | 260 | | foreach (T value in values) |
| | | 261 | | { |
| | 537804 | 262 | | int offset = int.CreateChecked(value - minInclusive); |
| | 537804 | 263 | | seenValues[offset] = true; |
| | | 264 | | } |
| | | 265 | | |
| | 3383 | 266 | | if (seenValues.Contains(false)) |
| | | 267 | | { |
| | 1351 | 268 | | return false; |
| | | 269 | | } |
| | | 270 | | |
| | 2032 | 271 | | return true; |
| | | 272 | | } |
| | | 273 | | |
| | | 274 | | internal interface IRuntimeConst |
| | | 275 | | { |
| | | 276 | | static abstract bool Value { get; } |
| | | 277 | | } |
| | | 278 | | |
| | | 279 | | internal readonly struct TrueConst : IRuntimeConst |
| | | 280 | | { |
| | 141934 | 281 | | public static bool Value => true; |
| | | 282 | | } |
| | | 283 | | |
| | | 284 | | internal readonly struct FalseConst : IRuntimeConst |
| | | 285 | | { |
| | 81187 | 286 | | public static bool Value => false; |
| | | 287 | | } |
| | | 288 | | |
| | | 289 | | /// <summary> |
| | | 290 | | /// Same as <see cref="Vector128.ShuffleNative(Vector128{byte}, Vector128{byte})"/>, except that we guarantee th |
| | | 291 | | /// Some logic in <see cref="SearchValues"/> relies on this exact behavior (implicit AND 0xF, and zeroing when t |
| | | 292 | | /// </summary> |
| | | 293 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 294 | | [CompExactlyDependsOn(typeof(Ssse3))] |
| | | 295 | | [CompHasFallback] |
| | | 296 | | internal static Vector128<byte> ShuffleNativeModified(Vector128<byte> vector, Vector128<byte> indices) |
| | | 297 | | { |
| | 15764 | 298 | | if (Ssse3.IsSupported) |
| | | 299 | | { |
| | 15764 | 300 | | return Ssse3.Shuffle(vector, indices); |
| | | 301 | | } |
| | | 302 | | |
| | 0 | 303 | | return Vector128.Shuffle(vector, indices); |
| | | 304 | | } |
| | | 305 | | } |
| | | 306 | | } |
| | | 307 | | |