| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using System.Runtime.CompilerServices; |
| | | 3 | | using NGql.Core.Abstractions; |
| | | 4 | | using NGql.Core.Builders; |
| | | 5 | | |
| | | 6 | | namespace NGql.Core.Extensions; |
| | | 7 | | |
| | | 8 | | [SuppressMessage("Minor Code Smell", "S3267:Loops should be simplified with \"LINQ\" expressions")] |
| | | 9 | | internal static class SpanExtensions |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Try to get value from a dictionary using a span key without allocating string |
| | | 13 | | /// </summary> |
| | | 14 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 15 | | public static bool TryGetValue(this Dictionary<string, FieldDefinition> dictionary, ReadOnlySpan<char> key, out Fiel |
| | | 16 | | { |
| | 8451 | 17 | | value = null; |
| | | 18 | | |
| | | 19 | | // Fast path: check if any key matches the span length first to avoid unnecessary comparisons |
| | 8451 | 20 | | var keyLength = key.Length; |
| | 17883 | 21 | | foreach (var kvp in dictionary) |
| | | 22 | | { |
| | 864 | 23 | | if (kvp.Key.Length != keyLength || !kvp.Key.AsSpan().SequenceEqual(key)) |
| | | 24 | | { |
| | | 25 | | continue; |
| | | 26 | | } |
| | | 27 | | |
| | 747 | 28 | | value = kvp.Value; |
| | 747 | 29 | | return true; |
| | | 30 | | } |
| | | 31 | | |
| | 7704 | 32 | | return false; |
| | 747 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Set value in a dictionary using a span key, converting to string only when needed |
| | | 37 | | /// </summary> |
| | | 38 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 39 | | public static void SetValue(this Dictionary<string, FieldDefinition> dictionary, ReadOnlySpan<char> key, FieldDefini |
| | | 40 | | { |
| | 8094 | 41 | | var keyString = key.ToString(); |
| | 8094 | 42 | | dictionary[keyString] = value; |
| | 8094 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Get or add a simple field using a span key with optimized path building — FieldChildren variant |
| | | 47 | | /// </summary> |
| | | 48 | | public static FieldDefinition GetOrAddSimpleField(this FieldChildren children, ReadOnlySpan<char> fieldName, ReadOnl |
| | | 49 | | { |
| | 2190 | 50 | | if (children.TryGetValue(fieldName, out var existingField) && existingField is not null) |
| | | 51 | | { |
| | 21 | 52 | | existingField = MergeArgumentsAndMetadata(existingField, arguments, metadata); |
| | 21 | 53 | | children.Set(fieldName, existingField); |
| | 21 | 54 | | return existingField; |
| | | 55 | | } |
| | | 56 | | |
| | 2169 | 57 | | return string.IsNullOrWhiteSpace(parentPath) |
| | 2169 | 58 | | ? AppendNew(children, fieldName, fieldType, arguments, fieldName, metadata) |
| | 2169 | 59 | | : AppendNewWithParentPath(children, fieldName, fieldType, arguments, parentPath, metadata); |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Get or add a simple field using a span key with optimized path building |
| | | 64 | | /// </summary> |
| | | 65 | | internal static FieldDefinition GetOrAddSimpleField(this Dictionary<string, FieldDefinition> fieldDefinitions, ReadO |
| | | 66 | | { |
| | 7620 | 67 | | if (fieldDefinitions.TryGetValue(fieldName, out var existingField) && existingField is not null) |
| | | 68 | | { |
| | 84 | 69 | | existingField = MergeArgumentsAndMetadata(existingField, arguments, metadata); |
| | 84 | 70 | | fieldDefinitions.SetValue(fieldName, existingField); |
| | 84 | 71 | | return existingField; |
| | | 72 | | } |
| | | 73 | | |
| | 7536 | 74 | | return string.IsNullOrWhiteSpace(parentPath) |
| | 7536 | 75 | | ? StoreNew(fieldDefinitions, fieldName, fieldType, arguments, fieldName, metadata) |
| | 7536 | 76 | | : StoreNewWithParentPath(fieldDefinitions, fieldName, fieldType, arguments, parentPath, metadata); |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 80 | | private static FieldDefinition MergeArgumentsAndMetadata(FieldDefinition existing, IDictionary<string, object?>? arg |
| | | 81 | | { |
| | 105 | 82 | | if (arguments is { Count: > 0 }) |
| | | 83 | | { |
| | 27 | 84 | | existing = existing.MergeFieldArguments(arguments); |
| | | 85 | | } |
| | 105 | 86 | | if (metadata is { Count: > 0 }) |
| | | 87 | | { |
| | 6 | 88 | | var mergedMetadata = Helpers.MergeNullableMetadata(existing._metadata, metadata); |
| | 6 | 89 | | existing = existing with { Metadata = mergedMetadata }; |
| | | 90 | | } |
| | 105 | 91 | | return existing; |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 95 | | private static FieldDefinition AppendNew(FieldChildren children, ReadOnlySpan<char> fieldName, ReadOnlySpan<char> fi |
| | | 96 | | { |
| | 2169 | 97 | | var field = Helpers.CreateFieldDefinition(fieldName, fieldType, ReadOnlySpan<char>.Empty, arguments, path, metad |
| | 2169 | 98 | | children.Append(field); |
| | 2169 | 99 | | return field; |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | private static FieldDefinition AppendNewWithParentPath(FieldChildren children, ReadOnlySpan<char> fieldName, ReadOnl |
| | | 103 | | { |
| | 2094 | 104 | | var estimatedLength = parentPath.Length + 1 + fieldName.Length; |
| | 2094 | 105 | | if (estimatedLength <= 256) |
| | | 106 | | { |
| | 2076 | 107 | | Span<char> pathBuffer = stackalloc char[estimatedLength]; |
| | 2076 | 108 | | var pathBuilder = new SpanPathBuilder(pathBuffer); |
| | 2076 | 109 | | pathBuilder.Append(parentPath.AsSpan()); |
| | 2076 | 110 | | pathBuilder.Append(fieldName); |
| | 2076 | 111 | | return AppendNew(children, fieldName, fieldType, arguments, pathBuilder.AsSpan(), metadata); |
| | | 112 | | } |
| | | 113 | | |
| | 18 | 114 | | var fieldPath = $"{parentPath}.{fieldName}"; |
| | 18 | 115 | | return AppendNew(children, fieldName, fieldType, arguments, fieldPath.AsSpan(), metadata); |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 119 | | private static FieldDefinition StoreNew(Dictionary<string, FieldDefinition> fieldDefinitions, ReadOnlySpan<char> fie |
| | | 120 | | { |
| | 7536 | 121 | | var field = Helpers.CreateFieldDefinition(fieldName, fieldType, ReadOnlySpan<char>.Empty, arguments, path, metad |
| | 7536 | 122 | | fieldDefinitions.SetValue(fieldName, field); |
| | 7536 | 123 | | return field; |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | private static FieldDefinition StoreNewWithParentPath(Dictionary<string, FieldDefinition> fieldDefinitions, ReadOnly |
| | | 127 | | { |
| | 24 | 128 | | var estimatedLength = parentPath.Length + 1 + fieldName.Length; |
| | 24 | 129 | | if (estimatedLength <= 256) |
| | | 130 | | { |
| | 6 | 131 | | Span<char> pathBuffer = stackalloc char[estimatedLength]; |
| | 6 | 132 | | var pathBuilder = new SpanPathBuilder(pathBuffer); |
| | 6 | 133 | | pathBuilder.Append(parentPath.AsSpan()); |
| | 6 | 134 | | pathBuilder.Append(fieldName); |
| | 6 | 135 | | return StoreNew(fieldDefinitions, fieldName, fieldType, arguments, pathBuilder.AsSpan(), metadata); |
| | | 136 | | } |
| | | 137 | | |
| | 18 | 138 | | var fieldPath = $"{parentPath}.{fieldName}"; |
| | 18 | 139 | | return StoreNew(fieldDefinitions, fieldName, fieldType, arguments, fieldPath.AsSpan(), metadata); |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | /// <summary> |
| | | 143 | | /// Vectorized field classification - checks all conditions in one pass |
| | | 144 | | /// </summary> |
| | | 145 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 146 | | public static (bool HasSpaces, bool HasDots, bool HasColons) ClassifyFieldFast(this ReadOnlySpan<char> span) |
| | | 147 | | { |
| | 190773 | 148 | | bool hasSpaces = false, hasDots = false, hasColons = false; |
| | | 149 | | |
| | | 150 | | // ULTRA FAST PATH: Single pass through the span |
| | 2292369 | 151 | | foreach (var c in span) |
| | | 152 | | { |
| | | 153 | | switch (c) |
| | | 154 | | { |
| | | 155 | | case ' ': |
| | 921 | 156 | | hasSpaces = true; |
| | 921 | 157 | | break; |
| | | 158 | | case '.': |
| | 106092 | 159 | | hasDots = true; |
| | 106092 | 160 | | break; |
| | | 161 | | case ':': |
| | 3963 | 162 | | hasColons = true; |
| | | 163 | | break; |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | // Early exit if all conditions found |
| | 1082610 | 167 | | if (hasSpaces && hasDots && hasColons) break; |
| | | 168 | | } |
| | | 169 | | |
| | 63591 | 170 | | return (hasSpaces, hasDots, hasColons); |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 174 | | public static bool IsSimpleField(this ReadOnlySpan<char> span) |
| | | 175 | | { |
| | 45933 | 176 | | var (hasSpaces, hasDots, hasColons) = span.ClassifyFieldFast(); |
| | 45933 | 177 | | return !hasSpaces && !hasDots && !hasColons; |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 181 | | public static bool IsDottedField(this ReadOnlySpan<char> span) |
| | | 182 | | { |
| | 17637 | 183 | | var (hasSpaces, hasDots, hasColons) = span.ClassifyFieldFast(); |
| | 17637 | 184 | | return hasDots && !hasSpaces && !hasColons; |
| | | 185 | | } |
| | | 186 | | |
| | | 187 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 188 | | public static bool HasLetterOrDigit(this ReadOnlySpan<char> span) |
| | | 189 | | { |
| | 1605 | 190 | | foreach (var c in span) |
| | | 191 | | { |
| | 546 | 192 | | if (char.IsLetterOrDigit(c)) |
| | | 193 | | { |
| | 381 | 194 | | return true; |
| | | 195 | | } |
| | | 196 | | } |
| | 66 | 197 | | return false; |
| | | 198 | | } |
| | | 199 | | |
| | | 200 | | /// <summary> |
| | | 201 | | /// Efficient case-insensitive comparison for spans |
| | | 202 | | /// </summary> |
| | | 203 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 204 | | public static bool EqualsIgnoreCase(this ReadOnlySpan<char> span, ReadOnlySpan<char> other) |
| | | 205 | | { |
| | 15 | 206 | | return span.Equals(other, StringComparison.OrdinalIgnoreCase); |
| | | 207 | | } |
| | | 208 | | |
| | | 209 | | /// <summary> |
| | | 210 | | /// Trim whitespace and dots from span efficiently |
| | | 211 | | /// </summary> |
| | | 212 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 213 | | public static ReadOnlySpan<char> TrimEndDotsAndSpaces(this ReadOnlySpan<char> span) |
| | | 214 | | { |
| | 4923 | 215 | | var end = span.Length - 1; |
| | 5016 | 216 | | while (end >= 0 && (span[end] == '.' || char.IsWhiteSpace(span[end]))) |
| | | 217 | | { |
| | 93 | 218 | | end--; |
| | | 219 | | } |
| | 4923 | 220 | | return span[..(end + 1)]; |
| | | 221 | | } |
| | | 222 | | |
| | | 223 | | /// <summary> |
| | | 224 | | /// Extract the field name from a segment by taking everything after the last colon |
| | | 225 | | /// </summary> |
| | | 226 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 227 | | public static ReadOnlySpan<char> ExtractFieldName(this ReadOnlySpan<char> segment) |
| | | 228 | | { |
| | 15 | 229 | | var colonIndex = segment.LastIndexOf(':'); |
| | 15 | 230 | | var fieldName = colonIndex == -1 ? segment : segment[(colonIndex + 1)..]; |
| | 15 | 231 | | return fieldName.Trim(); |
| | | 232 | | } |
| | | 233 | | } |