| | | 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.Collections.Generic; |
| | | 5 | | using System.Collections.Immutable; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Reflection.Internal; |
| | | 8 | | #if NET |
| | | 9 | | using System.Runtime.InteropServices; |
| | | 10 | | #endif |
| | | 11 | | |
| | | 12 | | namespace System.Reflection.Metadata.Ecma335 |
| | | 13 | | { |
| | | 14 | | [DebuggerDisplay("Count = {Count}")] |
| | | 15 | | internal readonly struct BlobDictionary |
| | | 16 | | { |
| | | 17 | | private readonly Dictionary<int, KeyValuePair<ImmutableArray<byte>, BlobHandle>> _dictionary; |
| | | 18 | | |
| | | 19 | | // A simple LCG. Constants taken from |
| | | 20 | | // https://github.com/imneme/pcg-c/blob/83252d9c23df9c82ecb42210afed61a7b42402d7/include/pcg_variants.h#L276-L28 |
| | | 21 | | private static int GetNextDictionaryKey(int dictionaryKey) => |
| | 0 | 22 | | (int)((uint)dictionaryKey * 747796405 + 2891336453); |
| | | 23 | | |
| | | 24 | | #if NET |
| | | 25 | | private unsafe ref KeyValuePair<ImmutableArray<byte>, BlobHandle> GetValueRefOrAddDefault(ReadOnlySpan<byte> key |
| | 0 | 26 | | { |
| | 0 | 27 | | int dictionaryKey = Hash.GetFNVHashCode(key); |
| | 0 | 28 | | while (true) |
| | 0 | 29 | | { |
| | 0 | 30 | | ref var entry = ref CollectionsMarshal.GetValueRefOrAddDefault(_dictionary, dictionaryKey, out exists); |
| | 0 | 31 | | if (!exists || entry.Key.AsSpan().SequenceEqual(key)) |
| | 0 | 32 | | { |
| | | 33 | | #pragma warning disable CS9082 // Local is returned by reference but was initialized to a value that cannot be returned |
| | | 34 | | // In .NET 6 the assembly of GetValueRefOrAddDefault was compiled with earlier ref safety rules |
| | | 35 | | // and caused an error, which was turned into a warning because of unsafe and was suppressed. |
| | 0 | 36 | | return ref entry; |
| | | 37 | | #pragma warning restore CS9082 |
| | | 38 | | } |
| | 0 | 39 | | dictionaryKey = GetNextDictionaryKey(dictionaryKey); |
| | 0 | 40 | | } |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | public BlobHandle GetOrAdd(ReadOnlySpan<byte> key, ImmutableArray<byte> immutableKey, BlobHandle value, out bool |
| | 0 | 44 | | { |
| | 0 | 45 | | ref var entry = ref GetValueRefOrAddDefault(key, out exists); |
| | 0 | 46 | | if (exists) |
| | 0 | 47 | | { |
| | 0 | 48 | | return entry.Value; |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | // If we are given an immutable array, do not allocate a new one. |
| | 0 | 52 | | if (immutableKey.IsDefault) |
| | 0 | 53 | | { |
| | 0 | 54 | | immutableKey = key.ToImmutableArray(); |
| | 0 | 55 | | } |
| | | 56 | | else |
| | 0 | 57 | | { |
| | 0 | 58 | | Debug.Assert(immutableKey.AsSpan().SequenceEqual(key)); |
| | 0 | 59 | | } |
| | | 60 | | |
| | 0 | 61 | | entry = new(immutableKey, value); |
| | 0 | 62 | | return value; |
| | 0 | 63 | | } |
| | | 64 | | #else |
| | | 65 | | public BlobHandle GetOrAdd(ReadOnlySpan<byte> key, ImmutableArray<byte> immutableKey, BlobHandle value, out bool |
| | | 66 | | { |
| | | 67 | | int dictionarykey = Hash.GetFNVHashCode(key); |
| | | 68 | | KeyValuePair<ImmutableArray<byte>, BlobHandle> entry; |
| | | 69 | | while (true) |
| | | 70 | | { |
| | | 71 | | if (!(exists = _dictionary.TryGetValue(dictionarykey, out entry)) |
| | | 72 | | || entry.Key.AsSpan().SequenceEqual(key)) |
| | | 73 | | { |
| | | 74 | | break; |
| | | 75 | | } |
| | | 76 | | dictionarykey = GetNextDictionaryKey(dictionarykey); |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | if (exists) |
| | | 80 | | { |
| | | 81 | | return entry.Value; |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | // If we are given an immutable array, do not allocate a new one. |
| | | 85 | | if (immutableKey.IsDefault) |
| | | 86 | | { |
| | | 87 | | immutableKey = key.ToImmutableArray(); |
| | | 88 | | } |
| | | 89 | | else |
| | | 90 | | { |
| | | 91 | | Debug.Assert(immutableKey.AsSpan().SequenceEqual(key)); |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | _dictionary.Add(dictionarykey, new(immutableKey, value)); |
| | | 95 | | return value; |
| | | 96 | | } |
| | | 97 | | #endif |
| | | 98 | | |
| | | 99 | | public BlobDictionary(int capacity = 0) |
| | 0 | 100 | | { |
| | 0 | 101 | | _dictionary = new(capacity); |
| | 0 | 102 | | } |
| | | 103 | | |
| | 0 | 104 | | public int Count => _dictionary.Count; |
| | | 105 | | |
| | | 106 | | public Dictionary<int, KeyValuePair<ImmutableArray<byte>, BlobHandle>>.Enumerator GetEnumerator() => |
| | 0 | 107 | | _dictionary.GetEnumerator(); |
| | | 108 | | } |
| | | 109 | | } |
| | | 110 | | |