|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Reflection; |
| 6 | +using System.Runtime.InteropServices; |
| 7 | + |
| 8 | +namespace ChessChallenge.Application |
| 9 | +{ |
| 10 | + public static class ObjectSizeHelper |
| 11 | + { |
| 12 | + static readonly long ObjectSize = IntPtr.Size == 8 ? 24 : 12; |
| 13 | + static readonly long PointerSize = IntPtr.Size; |
| 14 | + |
| 15 | + public static long GetSize(object? obj) => GetSize(obj, new HashSet<object>()); |
| 16 | + |
| 17 | + static long GetSize(object? obj, HashSet<object> seenObjects) |
| 18 | + { |
| 19 | + if (obj is null) |
| 20 | + { |
| 21 | + return 0; |
| 22 | + } |
| 23 | + |
| 24 | + var type = obj.GetType(); |
| 25 | + |
| 26 | + // ignore references to the API board and the API Timer |
| 27 | + if (typeof(API.Board) == type || typeof(API.Timer) == type) |
| 28 | + { |
| 29 | + return 0; |
| 30 | + } |
| 31 | + |
| 32 | + if (type.IsEnum) |
| 33 | + { |
| 34 | + if (!sizeOfTypeCache.TryGetValue(type, out var sizeOfType)) |
| 35 | + { |
| 36 | + var underlyingType = Enum.GetUnderlyingType(type); |
| 37 | + sizeOfTypeCache[type] = sizeOfType = Marshal.SizeOf(underlyingType); |
| 38 | + } |
| 39 | + |
| 40 | + return sizeOfType; |
| 41 | + } |
| 42 | + |
| 43 | + if (type.IsValueType) |
| 44 | + { |
| 45 | + // Marshal.SizeOf() does not work for structs with reference types |
| 46 | + // Marshal.SizeOf() also does not work with generics, so we need to use Unsafe.SizeOf() |
| 47 | + if (!fieldInfoCache.TryGetValue(type, out var fieldInfos)) |
| 48 | + { |
| 49 | + fieldInfoCache[type] = fieldInfos = type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); |
| 50 | + } |
| 51 | + if (!typeContainsClassCache.TryGetValue(type, out var typeContainsClass)) |
| 52 | + { |
| 53 | + typeContainsClassCache[type] = typeContainsClass = fieldInfos.Any(x => TypeIsClass(x.FieldType)); |
| 54 | + } |
| 55 | + return typeContainsClass |
| 56 | + ? GetFieldsMemorySize(obj, fieldInfos, seenObjects) |
| 57 | + : GetStructSize(type); |
| 58 | + } |
| 59 | + |
| 60 | + if (type == typeof(string)) |
| 61 | + { |
| 62 | + var str = (string)obj; |
| 63 | + return str.Length * 2 + 6 + ObjectSize; |
| 64 | + } |
| 65 | + |
| 66 | + if (obj is IList collection) |
| 67 | + { |
| 68 | + var totalSize = ObjectSize; |
| 69 | + |
| 70 | + // structs are counted easy |
| 71 | + var typeZero = collection.Count > 0 ? collection[0]?.GetType() : null; |
| 72 | + if (typeZero is not null && sizeOfTypeCache.TryGetValue(typeZero, out var sizeOfType)) |
| 73 | + { |
| 74 | + totalSize += collection.Count * sizeOfType; |
| 75 | + return totalSize; |
| 76 | + } |
| 77 | + |
| 78 | + for (var index = 0; index < collection.Count; index++) |
| 79 | + { |
| 80 | + var item = collection[index]; |
| 81 | + totalSize += GetObjectSize(item, item?.GetType() ?? typeof(object), seenObjects); |
| 82 | + } |
| 83 | + |
| 84 | + return totalSize; |
| 85 | + } |
| 86 | + |
| 87 | + if (TypeIsClass(type)) |
| 88 | + { |
| 89 | + if (!fieldInfoCache.TryGetValue(type, out var fieldInfos)) |
| 90 | + { |
| 91 | + fieldInfoCache[type] = fieldInfos = type.GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); |
| 92 | + } |
| 93 | + |
| 94 | + return ObjectSize + GetFieldsMemorySize(obj, fieldInfos, seenObjects); |
| 95 | + } |
| 96 | + |
| 97 | + throw new ArgumentException($"Unknown type {type.Name}", nameof(obj)); |
| 98 | + } |
| 99 | + |
| 100 | + static readonly Dictionary<Type, FieldInfo[]> fieldInfoCache = new(); |
| 101 | + static readonly Dictionary<Type, bool> typeContainsClassCache = new(); |
| 102 | + static readonly Dictionary<Type, bool> typeIsClassCache = new(); |
| 103 | + |
| 104 | + static readonly Dictionary<Type, int> sizeOfTypeCache = new(); |
| 105 | + private static long GetStructSize(Type type) |
| 106 | + { |
| 107 | + if (!sizeOfTypeCache.TryGetValue(type, out var sizeOfType)) |
| 108 | + { |
| 109 | + var genericSizeOf = typeof(System.Runtime.CompilerServices.Unsafe) |
| 110 | + .GetMethod(nameof(System.Runtime.CompilerServices.Unsafe.SizeOf)) |
| 111 | + .MakeGenericMethod(type); |
| 112 | + |
| 113 | + sizeOfTypeCache[type] = sizeOfType = (int)genericSizeOf.Invoke(null, null)!; |
| 114 | + } |
| 115 | + |
| 116 | + return sizeOfType; |
| 117 | + } |
| 118 | + |
| 119 | + private static long GetFieldsMemorySize(object obj, FieldInfo[] fieldInfos, HashSet<object> seenObjects) |
| 120 | + { |
| 121 | + var totalSize = 0L; |
| 122 | + |
| 123 | + for (var index = 0; index < fieldInfos.Length; index++) |
| 124 | + { |
| 125 | + var fieldInfo = fieldInfos[index]; |
| 126 | + var fieldValue = fieldInfo.GetValue(obj); |
| 127 | + totalSize += GetObjectSize(fieldValue, fieldInfo.FieldType, seenObjects); |
| 128 | + } |
| 129 | + |
| 130 | + return totalSize; |
| 131 | + } |
| 132 | + |
| 133 | + private static bool TypeIsClass(Type type) |
| 134 | + { |
| 135 | + if (!typeIsClassCache.TryGetValue(type, out var typeIsClass)) |
| 136 | + { |
| 137 | + typeIsClassCache[type] = typeIsClass = type.IsClass; |
| 138 | + } |
| 139 | + |
| 140 | + return typeIsClass; |
| 141 | + } |
| 142 | + |
| 143 | + private static long GetObjectSize(object? obj, Type type, HashSet<object> seenObjects) |
| 144 | + { |
| 145 | + var typeIsClass = TypeIsClass(type); |
| 146 | + |
| 147 | + if (obj is not null && typeIsClass && seenObjects.Contains(obj)) |
| 148 | + { |
| 149 | + return 0; |
| 150 | + } |
| 151 | + |
| 152 | + var size = GetSize(obj, seenObjects) + (type.IsClass ? PointerSize : 0); |
| 153 | + |
| 154 | + if (obj is not null && typeIsClass) |
| 155 | + { |
| 156 | + seenObjects.Add(obj); |
| 157 | + } |
| 158 | + |
| 159 | + return size; |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments