| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using System.Diagnostics.CodeAnalysis; |
| | | 3 | | using System.Reflection; |
| | | 4 | | using System.Runtime.CompilerServices; |
| | | 5 | | |
| | | 6 | | namespace NGql.Core.Caching; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Simple cache for common GraphQL types with memory and CPU optimizations |
| | | 10 | | /// </summary> |
| | | 11 | | [SuppressMessage("Minor Code Smell", "S3267:Loops should be simplified with \"LINQ\" expressions")] |
| | | 12 | | internal static class TypeCache |
| | | 13 | | { |
| | 6 | 14 | | private static readonly ConcurrentDictionary<string, string> CustomTypes = new(); |
| | | 15 | | |
| | | 16 | | // Pre-intern the most common GraphQL types (ordered by frequency) |
| | 6 | 17 | | private static readonly string[] CommonTypes = |
| | 6 | 18 | | [ |
| | 6 | 19 | | Constants.DefaultFieldType, // "String" - most common |
| | 6 | 20 | | "Int", "Boolean", "ID", // Other common scalars |
| | 6 | 21 | | Constants.ObjectFieldType, // "object" - for nested fields |
| | 6 | 22 | | "String!", "Int!", "Boolean!", // Non-null variants |
| | 6 | 23 | | "Float", "Float!", // Less common but still frequent |
| | 6 | 24 | | Constants.ArrayTypeMarker // "[]" - array marker |
| | 6 | 25 | | ]; |
| | | 26 | | |
| | | 27 | | // Pre-intern common nullable type patterns |
| | 6 | 28 | | private static readonly string[] CommonNullableTypes = |
| | 6 | 29 | | [ |
| | 6 | 30 | | "String?", "Int?", "Boolean?", "ID?", "Float?" |
| | 6 | 31 | | ]; |
| | | 32 | | |
| | | 33 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 34 | | public static string GetInternedType(ReadOnlySpan<char> type) |
| | | 35 | | { |
| | | 36 | | // ULTRA FAST PATH: Empty type defaults to String |
| | 46746 | 37 | | if (type.IsEmpty) |
| | | 38 | | { |
| | 6 | 39 | | return Constants.DefaultFieldType; |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | // OPTIMIZED PATH: Single combined check for both common and nullable types |
| | | 43 | | // This eliminates the O(n+m) double-loop pattern and does a single pass |
| | 46740 | 44 | | var allTypes = CombinedCommonTypes.Value; |
| | 318489 | 45 | | foreach (var commonType in allTypes) |
| | | 46 | | { |
| | 135405 | 47 | | if (type.SequenceEqual(commonType.AsSpan())) |
| | | 48 | | { |
| | 45801 | 49 | | return commonType; // Already interned |
| | | 50 | | } |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | // Standard path for other types |
| | 939 | 54 | | var typeString = type.ToString(); |
| | 939 | 55 | | return CustomTypes.GetOrAdd(typeString, typeString); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | // Lazy-initialized combined array to avoid allocation at static init time |
| | 6 | 59 | | private static readonly Lazy<string[]> CombinedCommonTypes = new(() => |
| | 6 | 60 | | { |
| | 6 | 61 | | var combined = new string[CommonTypes.Length + CommonNullableTypes.Length]; |
| | 6 | 62 | | CommonTypes.CopyTo(combined, 0); |
| | 6 | 63 | | CommonNullableTypes.CopyTo(combined, CommonTypes.Length); |
| | 6 | 64 | | return combined; |
| | 6 | 65 | | }); |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Interns a type string for memory efficiency - alias for GetInternedType |
| | | 69 | | /// </summary> |
| | | 70 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 6 | 71 | | public static string InternType(ReadOnlySpan<char> type) => GetInternedType(type); |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Caches reflection metadata for type introspection |
| | | 76 | | /// </summary> |
| | | 77 | | internal static class TypeMetadataCache |
| | | 78 | | { |
| | | 79 | | /// <summary> |
| | | 80 | | /// Caches PropertyInfo pairs (Key, Value) for KeyValuePair<,> generic types. |
| | | 81 | | /// Caller must guarantee the cached type is a closed KeyValuePair<TKey,TValue> — those |
| | | 82 | | /// always expose Key and Value properties, so the cached pair is non-nullable. |
| | | 83 | | /// </summary> |
| | | 84 | | internal static readonly ConcurrentDictionary<Type, (PropertyInfo Key, PropertyInfo Value)> KvpPropertyCache = new() |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Caches PropertyInfo[] per object type for the default WriteObject reflection branch. |
| | | 88 | | /// </summary> |
| | | 89 | | internal static readonly ConcurrentDictionary<Type, PropertyInfo[]> ObjectPropertyCache = new(); |
| | | 90 | | } |