| | | 1 | | //======================================================================= |
| | | 2 | | // ChronicleHashChronicler.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | using System.Collections.Generic; |
| | | 10 | | using System.Runtime.CompilerServices; |
| | | 11 | | using System.Text; |
| | | 12 | | |
| | | 13 | | namespace Chronicler.Hashing; |
| | | 14 | | |
| | | 15 | | internal sealed class ChronicleHashChronicler : IChronicler |
| | | 16 | | { |
| | | 17 | | private enum FieldKind : byte |
| | | 18 | | { |
| | | 19 | | Value = 1, |
| | | 20 | | DeepClass = 2, |
| | | 21 | | DeepStruct = 3, |
| | | 22 | | NullableDeepStruct = 4, |
| | | 23 | | Link = 5 |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | private enum LeafKind : byte |
| | | 27 | | { |
| | | 28 | | Bool = 1, |
| | | 29 | | Byte = 2, |
| | | 30 | | SByte = 3, |
| | | 31 | | Int16 = 4, |
| | | 32 | | UInt16 = 5, |
| | | 33 | | Int32 = 6, |
| | | 34 | | UInt32 = 7, |
| | | 35 | | Int64 = 8, |
| | | 36 | | UInt64 = 9, |
| | | 37 | | Char = 10, |
| | | 38 | | String = 11, |
| | | 39 | | Enum = 12 |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | [ThreadStatic] |
| | | 43 | | private static ChronicleHashChronicler? _cached; |
| | | 44 | | |
| | 1 | 45 | | private static readonly Dictionary<Type, LeafKind> LeafKindsByType = new() |
| | 1 | 46 | | { |
| | 1 | 47 | | [typeof(bool)] = LeafKind.Bool, |
| | 1 | 48 | | [typeof(byte)] = LeafKind.Byte, |
| | 1 | 49 | | [typeof(sbyte)] = LeafKind.SByte, |
| | 1 | 50 | | [typeof(short)] = LeafKind.Int16, |
| | 1 | 51 | | [typeof(ushort)] = LeafKind.UInt16, |
| | 1 | 52 | | [typeof(int)] = LeafKind.Int32, |
| | 1 | 53 | | [typeof(uint)] = LeafKind.UInt32, |
| | 1 | 54 | | [typeof(long)] = LeafKind.Int64, |
| | 1 | 55 | | [typeof(ulong)] = LeafKind.UInt64, |
| | 1 | 56 | | [typeof(char)] = LeafKind.Char, |
| | 1 | 57 | | [typeof(string)] = LeafKind.String |
| | 1 | 58 | | }; |
| | | 59 | | |
| | 1 | 60 | | private static readonly Dictionary<Type, string> StableTypeNames = new(); |
| | 1 | 61 | | private static readonly object StableTypeNameSync = new(); |
| | | 62 | | |
| | | 63 | | private ChronicleHashWriter _writer; |
| | | 64 | | private ChronicleContext? _context; |
| | | 65 | | private bool _isInUse; |
| | | 66 | | |
| | 5 | 67 | | private ChronicleHashChronicler() |
| | | 68 | | { |
| | 5 | 69 | | } |
| | | 70 | | |
| | | 71 | | public ChronicleContext Context |
| | | 72 | | { |
| | | 73 | | get |
| | | 74 | | { |
| | 13 | 75 | | if (_context == null) |
| | 2 | 76 | | throw new InvalidOperationException("Record hash chronicler context is not active."); |
| | | 77 | | |
| | 11 | 78 | | return _context; |
| | | 79 | | } |
| | | 80 | | } |
| | | 81 | | |
| | 37000 | 82 | | public SerializationMode Mode => SerializationMode.Saving; |
| | | 83 | | |
| | | 84 | | public static void Contribute(IRecordable target, ChronicleContext context, ref ChronicleHashWriter writer) |
| | | 85 | | { |
| | 73996 | 86 | | ChronicleHashChronicler chronicler = Rent(); |
| | | 87 | | |
| | 73996 | 88 | | chronicler._writer = writer; |
| | 73996 | 89 | | chronicler._context = context; |
| | | 90 | | |
| | | 91 | | try |
| | | 92 | | { |
| | 73996 | 93 | | chronicler.WriteRecordStart(target.GetType()); |
| | 73996 | 94 | | target.RecordData(chronicler); |
| | 73991 | 95 | | chronicler.WriteRecordEnd(); |
| | 73991 | 96 | | writer = chronicler._writer; |
| | 73991 | 97 | | } |
| | | 98 | | finally |
| | | 99 | | { |
| | 73996 | 100 | | chronicler._writer = default; |
| | 73996 | 101 | | chronicler._context = null; |
| | 73996 | 102 | | chronicler._isInUse = false; |
| | 73996 | 103 | | } |
| | 73991 | 104 | | } |
| | | 105 | | |
| | | 106 | | public void LookValue<T>(ref T value, string name, T? defaultValue = default) |
| | | 107 | | { |
| | 222960 | 108 | | WriteFieldHeader(name, FieldKind.Value, typeof(T)); |
| | 222959 | 109 | | LeafKind kind = WriteLeafKind(typeof(T), name); |
| | 222958 | 110 | | WriteLeafValue(ref value, kind); |
| | | 111 | | |
| | 222958 | 112 | | T declaredDefault = defaultValue!; |
| | 222958 | 113 | | WriteLeafValue(ref declaredDefault, kind); |
| | 222958 | 114 | | } |
| | | 115 | | |
| | | 116 | | public void LookDeep<T>(ref T value, string name) |
| | | 117 | | where T : class, IRecordable |
| | | 118 | | { |
| | 36874 | 119 | | WriteFieldHeader(name, FieldKind.DeepClass, typeof(T)); |
| | | 120 | | |
| | 36874 | 121 | | if (value == null) |
| | | 122 | | { |
| | 1 | 123 | | _writer.WriteBool(false); |
| | 1 | 124 | | return; |
| | | 125 | | } |
| | | 126 | | |
| | 36873 | 127 | | _writer.WriteBool(true); |
| | 36873 | 128 | | WriteRecordStart(typeof(T)); |
| | 36873 | 129 | | value.RecordData(this); |
| | 36873 | 130 | | WriteRecordEnd(); |
| | 36873 | 131 | | } |
| | | 132 | | |
| | | 133 | | public void LookDeepStruct<T>(ref T value, string name) |
| | | 134 | | where T : struct, IRecordable |
| | | 135 | | { |
| | 37288 | 136 | | WriteFieldHeader(name, FieldKind.DeepStruct, typeof(T)); |
| | 37288 | 137 | | WriteRecordStart(typeof(T)); |
| | 37288 | 138 | | value.RecordData(this); |
| | 37288 | 139 | | WriteRecordEnd(); |
| | 37288 | 140 | | } |
| | | 141 | | |
| | | 142 | | public void LookNullableDeep<T>(ref T? value, string name) |
| | | 143 | | where T : struct, IRecordable |
| | | 144 | | { |
| | 36876 | 145 | | WriteFieldHeader(name, FieldKind.NullableDeepStruct, typeof(T)); |
| | | 146 | | |
| | 36876 | 147 | | if (!value.HasValue) |
| | | 148 | | { |
| | 3 | 149 | | _writer.WriteBool(false); |
| | 3 | 150 | | return; |
| | | 151 | | } |
| | | 152 | | |
| | 36873 | 153 | | _writer.WriteBool(true); |
| | 36873 | 154 | | T nestedValue = value.Value; |
| | 36873 | 155 | | WriteRecordStart(typeof(T)); |
| | 36873 | 156 | | nestedValue.RecordData(this); |
| | 36873 | 157 | | WriteRecordEnd(); |
| | 36873 | 158 | | } |
| | | 159 | | |
| | | 160 | | public void LookLink<T>( |
| | | 161 | | ref T value, |
| | | 162 | | string name, |
| | | 163 | | string? slot = null, |
| | | 164 | | RecordLinkResolveMode resolveMode = RecordLinkResolveMode.Immediate, |
| | | 165 | | Action<T>? assignLoadedValue = null) |
| | | 166 | | { |
| | 10 | 167 | | string? normalizedSlot = string.IsNullOrEmpty(slot) ? null : slot; |
| | 10 | 168 | | WriteFieldHeader(name, FieldKind.Link, typeof(T)); |
| | 10 | 169 | | _writer.WriteString(normalizedSlot); |
| | 10 | 170 | | _writer.WriteEnum(resolveMode); |
| | | 171 | | |
| | 10 | 172 | | if (value is null) |
| | | 173 | | { |
| | 1 | 174 | | _writer.WriteString(null); |
| | 1 | 175 | | return; |
| | | 176 | | } |
| | | 177 | | |
| | 9 | 178 | | if (!Context.Links.TryGetReferenceId(value, out string? id, normalizedSlot)) |
| | | 179 | | { |
| | 2 | 180 | | throw new InvalidOperationException( |
| | 2 | 181 | | $"Unable to hash link '{name}' of type {typeof(T).Name} because no stable id could be produced{FormatSlo |
| | | 182 | | } |
| | | 183 | | |
| | 7 | 184 | | _writer.WriteString(id); |
| | 7 | 185 | | } |
| | | 186 | | |
| | | 187 | | private static ChronicleHashChronicler Rent() |
| | | 188 | | { |
| | 73996 | 189 | | ChronicleHashChronicler? cached = _cached; |
| | 73996 | 190 | | if (cached == null) |
| | | 191 | | { |
| | 4 | 192 | | cached = new ChronicleHashChronicler(); |
| | 4 | 193 | | _cached = cached; |
| | | 194 | | } |
| | | 195 | | |
| | 73996 | 196 | | if (cached._isInUse) |
| | 1 | 197 | | return new ChronicleHashChronicler { _isInUse = true }; |
| | | 198 | | |
| | 73995 | 199 | | cached._isInUse = true; |
| | 73995 | 200 | | return cached; |
| | | 201 | | } |
| | | 202 | | |
| | | 203 | | private void WriteRecordStart(Type declaredType) |
| | | 204 | | { |
| | 185030 | 205 | | _writer.WriteSection("chronicler.record", 1); |
| | 185030 | 206 | | _writer.WriteString(GetStableTypeName(declaredType)); |
| | 185030 | 207 | | } |
| | | 208 | | |
| | | 209 | | private void WriteRecordEnd() |
| | | 210 | | { |
| | 185025 | 211 | | _writer.WriteSection("chronicler.record.end", 1); |
| | 185025 | 212 | | } |
| | | 213 | | |
| | | 214 | | private void WriteFieldHeader(string name, FieldKind kind, Type declaredType) |
| | | 215 | | { |
| | 334008 | 216 | | if (name == null) |
| | 1 | 217 | | throw new ArgumentNullException(nameof(name)); |
| | | 218 | | |
| | 334007 | 219 | | _writer.WriteString(name); |
| | 334007 | 220 | | _writer.WriteByte((byte)kind); |
| | 334007 | 221 | | _writer.WriteString(GetStableTypeName(declaredType)); |
| | 334007 | 222 | | } |
| | | 223 | | |
| | | 224 | | private LeafKind WriteLeafKind(Type declaredType, string name) |
| | | 225 | | { |
| | 222959 | 226 | | LeafKind kind = GetLeafKind(declaredType, name); |
| | 222958 | 227 | | _writer.WriteByte((byte)kind); |
| | | 228 | | |
| | 222958 | 229 | | if (kind == LeafKind.Enum) |
| | 36944 | 230 | | _writer.WriteString(GetStableTypeName(Enum.GetUnderlyingType(declaredType))); |
| | | 231 | | |
| | 222958 | 232 | | return kind; |
| | | 233 | | } |
| | | 234 | | |
| | | 235 | | private static LeafKind GetLeafKind(Type declaredType, string name) |
| | | 236 | | { |
| | 222959 | 237 | | if (declaredType.IsEnum) |
| | 36944 | 238 | | return LeafKind.Enum; |
| | | 239 | | |
| | 186015 | 240 | | if (LeafKindsByType.TryGetValue(declaredType, out LeafKind kind)) |
| | 186014 | 241 | | return kind; |
| | | 242 | | |
| | 1 | 243 | | throw new NotSupportedException($"Unsupported record-hash leaf value '{name}' of type {GetStableTypeName(declare |
| | | 244 | | } |
| | | 245 | | |
| | | 246 | | private void WriteLeafValue<T>(ref T value, LeafKind kind) |
| | | 247 | | { |
| | 445916 | 248 | | if (kind == LeafKind.String) |
| | | 249 | | { |
| | 73780 | 250 | | _writer.WriteString(value as string); |
| | 73780 | 251 | | return; |
| | | 252 | | } |
| | | 253 | | |
| | 372136 | 254 | | _writer.WriteBool(true); |
| | | 255 | | |
| | | 256 | | switch (kind) |
| | | 257 | | { |
| | | 258 | | case LeafKind.Bool: |
| | 147508 | 259 | | _writer.WriteBool(Unsafe.As<T, bool>(ref value)); |
| | 147508 | 260 | | break; |
| | | 261 | | case LeafKind.Byte: |
| | 26 | 262 | | _writer.WriteByte(Unsafe.As<T, byte>(ref value)); |
| | 26 | 263 | | break; |
| | | 264 | | case LeafKind.SByte: |
| | 26 | 265 | | _writer.WriteSByte(Unsafe.As<T, sbyte>(ref value)); |
| | 26 | 266 | | break; |
| | | 267 | | case LeafKind.Int16: |
| | 26 | 268 | | _writer.WriteInt16(Unsafe.As<T, short>(ref value)); |
| | 26 | 269 | | break; |
| | | 270 | | case LeafKind.UInt16: |
| | 26 | 271 | | _writer.WriteUInt16(Unsafe.As<T, ushort>(ref value)); |
| | 26 | 272 | | break; |
| | | 273 | | case LeafKind.Int32: |
| | 148632 | 274 | | _writer.WriteInt32(Unsafe.As<T, int>(ref value)); |
| | 148632 | 275 | | break; |
| | | 276 | | case LeafKind.UInt32: |
| | 850 | 277 | | _writer.WriteUInt32(Unsafe.As<T, uint>(ref value)); |
| | 850 | 278 | | break; |
| | | 279 | | case LeafKind.Int64: |
| | 1102 | 280 | | _writer.WriteInt64(Unsafe.As<T, long>(ref value)); |
| | 1102 | 281 | | break; |
| | | 282 | | case LeafKind.UInt64: |
| | 26 | 283 | | _writer.WriteUInt64(Unsafe.As<T, ulong>(ref value)); |
| | 26 | 284 | | break; |
| | | 285 | | case LeafKind.Char: |
| | 26 | 286 | | _writer.WriteChar(Unsafe.As<T, char>(ref value)); |
| | 26 | 287 | | break; |
| | | 288 | | default: |
| | | 289 | | // GetLeafKind validates the type; Enum is the only remaining kind. |
| | 73888 | 290 | | WriteEnumValue(ref value); |
| | | 291 | | break; |
| | | 292 | | } |
| | 73888 | 293 | | } |
| | | 294 | | |
| | | 295 | | private void WriteEnumValue<T>(ref T value) |
| | | 296 | | { |
| | 73888 | 297 | | if (Unsafe.SizeOf<T>() == 1) |
| | | 298 | | { |
| | 40 | 299 | | _writer.WriteByte(Unsafe.As<T, byte>(ref value)); |
| | 40 | 300 | | return; |
| | | 301 | | } |
| | | 302 | | |
| | 73848 | 303 | | if (Unsafe.SizeOf<T>() == 2) |
| | | 304 | | { |
| | 40 | 305 | | _writer.WriteUInt16(Unsafe.As<T, ushort>(ref value)); |
| | 40 | 306 | | return; |
| | | 307 | | } |
| | | 308 | | |
| | 73808 | 309 | | if (Unsafe.SizeOf<T>() == 4) |
| | | 310 | | { |
| | 73768 | 311 | | _writer.WriteUInt32(Unsafe.As<T, uint>(ref value)); |
| | 73768 | 312 | | return; |
| | | 313 | | } |
| | | 314 | | |
| | 40 | 315 | | _writer.WriteUInt64(Unsafe.As<T, ulong>(ref value)); |
| | 40 | 316 | | } |
| | | 317 | | |
| | | 318 | | private static string GetStableTypeName(Type type) |
| | | 319 | | { |
| | 555991 | 320 | | lock (StableTypeNameSync) |
| | | 321 | | { |
| | 555991 | 322 | | if (StableTypeNames.TryGetValue(type, out string? name)) |
| | 555941 | 323 | | return name; |
| | | 324 | | |
| | 50 | 325 | | name = BuildStableTypeName(type); |
| | 50 | 326 | | StableTypeNames[type] = name; |
| | 50 | 327 | | return name; |
| | | 328 | | } |
| | 555991 | 329 | | } |
| | | 330 | | |
| | | 331 | | private static string BuildStableTypeName(Type type) |
| | | 332 | | { |
| | 50 | 333 | | if (type.IsArray) |
| | 2 | 334 | | return BuildStableArrayTypeName(type); |
| | | 335 | | |
| | 48 | 336 | | if (!type.IsGenericType) |
| | | 337 | | // Runtime records and typeof(T) supply closed types, never generic parameters. |
| | 44 | 338 | | return type.FullName!; |
| | | 339 | | |
| | 4 | 340 | | return BuildStableGenericTypeName(type); |
| | | 341 | | } |
| | | 342 | | |
| | | 343 | | private static string BuildStableArrayTypeName(Type type) |
| | | 344 | | { |
| | 2 | 345 | | Type elementType = type.GetElementType()!; |
| | 2 | 346 | | int rank = type.GetArrayRank(); |
| | 2 | 347 | | string suffix = rank == 1 |
| | 2 | 348 | | ? "[]" |
| | 2 | 349 | | : "[" + new string(',', rank - 1) + "]"; |
| | | 350 | | |
| | 2 | 351 | | return GetStableTypeName(elementType) + suffix; |
| | | 352 | | } |
| | | 353 | | |
| | | 354 | | private static string BuildStableGenericTypeName(Type type) |
| | | 355 | | { |
| | 4 | 356 | | Type genericTypeDefinition = type.GetGenericTypeDefinition(); |
| | 4 | 357 | | string baseName = genericTypeDefinition.FullName!; |
| | 4 | 358 | | Type[] arguments = type.GetGenericArguments(); |
| | | 359 | | |
| | 4 | 360 | | var builder = new StringBuilder(baseName.Length + (arguments.Length * 16) + 2); |
| | 4 | 361 | | builder.Append(baseName); |
| | 4 | 362 | | builder.Append('<'); |
| | | 363 | | |
| | 22 | 364 | | for (int i = 0; i < arguments.Length; i++) |
| | | 365 | | { |
| | 7 | 366 | | if (i > 0) |
| | 3 | 367 | | builder.Append(','); |
| | | 368 | | |
| | 7 | 369 | | builder.Append(GetStableTypeName(arguments[i])); |
| | | 370 | | } |
| | | 371 | | |
| | 4 | 372 | | builder.Append('>'); |
| | 4 | 373 | | return builder.ToString(); |
| | | 374 | | } |
| | | 375 | | |
| | | 376 | | private static string FormatSlot(string? slot) |
| | | 377 | | { |
| | 2 | 378 | | return string.IsNullOrEmpty(slot) |
| | 2 | 379 | | ? string.Empty |
| | 2 | 380 | | : $" in slot '{slot}'"; |
| | | 381 | | } |
| | | 382 | | } |